How to use same django filter class(filters.py) in two different views - python-3.x

I have a filter class defined below.
filters.py
class CTAFilter(django_filters.FilterSet):
id = django_filters.NumberFilter(label="DSID")
class Meta:
model = CTA
fields = ['id', 'EmailID','id','Shift_timing']
Now I want to use this CTAFilter in normal template(table data)view and in download views.
I have observed that It is working fine for normal render view but when I am using it in my download views it is not working and I am getting all model data in the .xls file.
Please find the below questions which I have posted.
how to use Django filtered class data to 2 seperate view
I am not able to resolve this problem I have tried to check if I can define it globally so that the filter will work for all views(like RESTAPI).
Is there any way I can make my download view as a child view class of normal render view so that I will use the below code from the parent view(as it is working fine)?
cta_list = CTA.objects.all()
cta_filter = CTAFilter(request.GET, queryset=cta_list) allcta = cta_filter.qs
A>Normal View where the filter is working fine.
def retrievecta_view(request):
if request.method == 'GET':
allcta = CTA.objects.all()
allcta1 = allcta
allctagen = allcta1.filter(Shift_timing__exact='General')
allctamor = allcta1.filter(Shift_timing__exact='Morning')
allctseve = allcta1.filter(Shift_timing__exact='Evening')
allctatotal = allcta1.filter(Shift_timing__exact='Total')
# For filtering using 'django_filters',
cta_list = CTA.objects.all()
cta_filter = CTAFilter(request.GET, queryset=cta_list)
allcta = cta_filter.qs
paginator = Paginator(allcta, 50)
page_number = request.GET.get('page')
try:
allcts = paginator.page(page_number)
except PageNotAnInteger:
allcts = paginator.page(1)
except EmptyPage:
allcts = paginator.page(paginator.num_pages)
return render(request, 'abcd/cta.html', {'allcta': allcta, 'cta_filter': cta_filter, 'allcta1': allcta1,
'allctagen': allctagen, 'allctamor': allctamor,
'allctaeve': allctaeve,
'allctatotal': allctatotal})
b> Download view where I am trying to use the same filter but it is giving me all records.
def exportcts_data(request):
response = HttpResponse(content_type='application/ms-excel')
response['Content-Disposition'] = 'attachment; filename="CTA_ShiftTiming.xls"'
wb = xlwt.Workbook(encoding='utf-8')
ws = wb.add_sheet('CTS_ShiftChange Data') # this will make a sheet named Users Data
# Sheet header, first row
row_num = 0
font_style = xlwt.XFStyle()
font_style.font.bold = True
columns = ['id','idk','Shift_timing','EmailID','Vendor_Company','Project_name','SerialNumber','Reason','last_updated_time']
for col_num in range(len(columns)):
ws.write(row_num, col_num, columns[col_num], font_style) # at 0 row 0 column
# Sheet body, remaining rows
font_style = xlwt.XFStyle()
cts_list = CTA.objects.all()
cts_filter = CTAFilter(request.GET, queryset=cts_list)
allcts = cts_filter.qs
rows = allcts.values_list('id', 'idk', 'Shift_timing', 'EmailID', 'Vendor_Company', 'Project_name',
'SerialNumber', 'Reason', 'last_updated_time')
for row in rows:
row_num += 1
for col_num in range(len(row)):
ws.write(row_num, col_num, row[col_num], font_style)
wb.save(response)
return response

I'm not quite following why you want to have separate view for downloads which ultimately should be rendering the same data as the normal view if they are using the same filter. Maybe it is just my misunderstanding so I'm not sure if this will help you but let's see.
First off let me explain a little background. This is a task management application and in there I have an html page where the person logged in can view all of their completed tasks. (Nice and simple.) However the user may have tasks from many different projects so I have created a dropdown list that allows them to filter by a single project. They may also want to only see a specific period of tasks so I have allowed them to set a date range by providing a start and end date. (Nothing startling or earth shattering here.) Once the parameters are set, the user clicks a search button and the filtered results are displayed. The page also has an Export button which downloads the results of the filtered list to a .xls spreadsheet.
So how do I do this? Well first of all, I am using Django-Tables2 for rendering my tables. I simple predefine the table in tables.py and throw it the data I want from my views and it takes care of everything. Therefore my view code is minimal and very simple and looks like this.
from django_tables2.export.export import TableExport
from .tables import CompletedTable
def completedlist(request, page='0', name=''):
#Check to see if we have clicked a button inside the form
if request.method == 'POST':
return redirect ('tasks:tasklist')
else:
# Pre-filtering of user and Master = True etc is done in the MasterListFilter in filters.py
# Then we compile the list for Filtering by.
f = CompletedListFilter(request.GET, queryset=Task.objects.all(),request=request)
# Then we apply the complete list to the table, configure it and then render it.
completedtable = CompletedTable(f.qs)
rows = len(completedtable.rows)
if int(page) > 0:
RequestConfig(request, paginate={'page': page, 'per_page': 10}).configure(completedtable)
else:
RequestConfig(request, paginate={'page': 1, 'per_page': 10}).configure(completedtable)
export_format = request.GET.get('_export', None)
if TableExport.is_valid_format(export_format):
exporter = TableExport(export_format, completedtable)
return exporter.response('Completed Tasks.{}'.format(export_format))
return render (request,'tasks/completedlist.html',{'completedtable': completedtable, 'filter': f, 'rows': rows})
As you can see, every time the user hits either the search or export buttons, I am recompiling the queryset in variable f with the following line:
f = CompletedListFilter(request.GET, queryset=Task.objects.all(),request=request)
I have predefined the .xls format in the html page with this code:
<button class="btn btn-primary btn-xs" name="_export" value="xls" type="submit">Export</button>
So then I can test to see if the user clicked the Export button or not by getting the value of _export from the request like this:
export_format = request.GET.get('_export', None)
If the user did not click the export button, export_format will default to none. If they did, it will be .xls as defined in the html. Then I simply either export the data in line with the filters set by the user or I render the page with the same filtered list of data like this:
if TableExport.is_valid_format(export_format):
exporter = TableExport(export_format, completedtable)
return exporter.response('Completed Tasks.{}'.format(export_format))
return render (request,'tasks/completedlist.html',{'completedtable': completedtable, 'filter': f, 'rows': rows})
So there you have it. As you say your filter is working for the normal view I have not detailed my filter as that would seem to be unnecessary.
Maybe this solution is too simplistic for your requirements and yes, before I get shot down by other developers, there are several limitations, such as 'What if the user wants to use something other than .xls?' or 'What if they want to export more than one Project at a time?' Like everything, there is always room for improvement but when I'm bashing my head with an issue, I often find it helps to strip things back to basics and see what comes from that.

Related

Set different variables to the same Variable result [Discord.py, Google Sheet API]

What i am trying to do it's cell_find to look for specific member's logged bans in my Google sheet API and after that, It'll display it in an Discord Embed, but the problem basically is when the bot send the embed it only shows one result instead of the 3 ones that should display
cell_find = sheet.findall('Banned')
var = 0
for cell in cell_find:
var += 1
sheet_username = sheet.cell(cell.row,3).value
sheet_reason = sheet.cell(cell.row,7).value
if var == 4:
break
The embed i'd like to try would be something like
embed = discord.Embed(title=f'{sheet_username}'s Incidents')
embed.addfield(name="Incident#1", value=f'{sheet_reason}')
embed.addfield(name="Incident#2", value=f'{sheet_reason}')
embed.addfield(name="Incident#3", value=f'{sheet_reason}')
await ctx.send(embed = embed)
You could try iterating over it in a for loop:
sheet_username = sheet.cell(cell.row,3).value
embed = discord.Embed(title=f"{sheet_username}'s Incidents")
for count, cell in enumerate(cell_find, 1):
sheet_reason = sheet.cell(cell.row,7).value
embed.add_field(name=f"Incident#{count}", value=f"{sheet_reason}")
await ctx.send(embed=embed)
Without knowing how your google sheet is laid out, I'm not able to help on the columns, but you could use the count variable to add to the column perhaps? Something like 3 + count?
But I think as far as logic goes, this is the way to go about adding fields.
References:
Enumerating in Python

Openpyxl returns wrong hyperlink address after delete_rows()

Problem: I have a program that scrapes Twitter and returns the results in an excel file. Part of each entry is a column containing a hyperlink to the Tweet and image included in the Tweet if applicable. Entries and hyperlinks work fine except when I run the following code to remove duplicate posts:
#Remove duplicate posts.
values = []
i = 2
while i <= sheet.max_row:
if sheet.cell(row=i,column=3).value in values:
sheet.delete_rows(i,1)
else:
values.append(sheet.cell(row=i,column=3).value)
i+=1
After running the duplicate removal snippet the hyperlinks point to what I assume is the offset of deleted entries. Here is the code for creating a Twitter entry:
sheet.cell(row=row, column=8).hyperlink = "https://twitter.com/"+str(tweet.user.screen_name)+"/status/"+str(tweet.id)
sheet.cell(row=row, column=8).style = "Hyperlink"
Expected Results: Should be able to remove duplicate entries and keep the hyperlink pointed to the correct address.
The hyperlinks point to the correct addresses for whatever reason when I change the code to the this:
sheet.cell(row=row, column=8).value = "https://twitter.com/"+str(tweet.user.screen_name)+"/status/"+str(tweet.id)
sheet.cell(row=row, column=8).style = "Hyperlink"
Requires a rapid double click to work as a hyperlink in the excel sheet versus the one click when inserting using .hyperlink.
So fixed but not fixed.

Data table in kivy

I have created application in kivy using kivymd , when I used its data_table code I couldn't select multi rows to delete them one time, I tried to create a list and append each selected_id in this list but it returns an empty list.
this is the function which returns a selected_id :
def select_row(self, checkbox, checked, **kwargs):
if not self.is_all_checked:
for index, cell in enumerate(self.children):
if isinstance(cell, ItemCheckbox) and checked:
if cell.id != checkbox.id:
self.selected_id = checkbox.id
cell.active = False
elif self.is_all_checked and not checked:
self.select_all(False)
if not checked:
self.selected_id = ""
All what I need is to make a list of rows with checkboxes that allow to be check many of them at once to let user delete it! Could you help me do this?

Tkinter treeview text not visible until client restart

I have a program written in python 3.5 which scans a directory and stores the info in a SQLITE3 db. This info can be displayed in a ttk.treeview in the client.
The 2 main functions are 1) "scan" which adds the information to the db and 2) viewFiles which is below (it just grabs data from the db and displays it in the treeview).
During the scan function, a second tkinter window is opened and displays information on whats been scanned. If I try to use the viewFiles fucntion without a restart, the data displayed is invisible but I know the data is there because I can copy paste it into a text document). If I restart the client, the text is displayed correctly after pressing the button linked to the viewFiles function.
I think it must be something to do with the the focus shift thanks to this 2nd window opening during the scan process but I'm not sure and can't find any similar case to learn from.
def viewFiles(self):
global dataTreeView
results = []
foldersContentsToView = yieldFoldersToView()
for dpath in foldersContentsToView:
sqlstring= "SELECT * FROM DBDATA WHERE directorypath=:dpath"
values = {"dpath": dpath}
[results.append(i) for i in databaseFunctions.getDBobject().returnSelectQueryResults(sqlstring, values)]
for e in results:
dataTreeView.insert('', 'end', values=list(e))
Does anyone have any ideas?
The issue was that the columns were being set up in the moment the treeview was created by querying the fields in the database. However before the first scan, there were no records in the db and so the columns were not being configured properly until a restart, after which data existed and could be queried on treeview creation.
To fix the issue, I moved the column configure data to the viewFiles function like so:
def viewFiles(self):
global dataTreeView
audioDataTree['columns'] = returnListTableFields()
#Configure Columns
for column in listofdbtablefields():
dataTree.column(column,width=len(column)*10)
dataTree.heading(column, text=column)
dataTree.column(column, anchor='center')
#Hide First Column (TreeLabel)
dataTree.column('#0',width=0)
results = []
foldersContentsToView = yieldFoldersToView()
for dpath in foldersContentsToView:
sqlstring= "SELECT * FROM DBDATA WHERE directorypath=:dpath"
values = {"dpath": dpath}
[results.append(i) for i in databaseFunctions.getDBobject().returnSelectQueryResults(sqlstring, values)]
for e in results:
dataTreeView.insert('', 'end', values=list(e))

How to use content slide with RECORD objects in typoscript

On one of my sites, content (Videos) is inherited from the levels above if the content column is empty (in this case: colPos=3 / Border)
To create the output, I use
temp.myObject < styles.content.getBorder
temp.myObject {
slide = -1
}
Easy, because this is taken from a CONTENT object and slide is a built-in function.
Due to our system setup I need to do something similar with the RECORDS object. But the following typoscript doesn't work - it generates empty output:
temp.myObject = RECORDS
temp.myObject {
tables = tt_content
source.cObject = CONTENT
source.cObject {
slide = -1
table = tt_content
renderObj = TEXT
renderObj.field = uid
}
}
The same happens with this snippet:
temp.myObject = RECORDS
temp.myObject {
tables = tt_content
source.cObject = CONTENT
source.cObject {
table = tt_content
select {
pidInList.data = leveluid:-1,slide
}
renderObj = TEXT
renderObj.field = uid
}
}
[Note: The complicated source part above provides the ID of a content element from where we extract an image file from the flexform xml]
Can somebody help me to achieve a contentslide solution based on the RECORDS object?
If there are any problems understanding the questions, please ask.
CONTENT object doesn't have "slide" property.
Try simulate slide using stdWrap.ifEmpty.cObject.... for Your RECORDS object, as it could be done for slide simulation for TYPO3 3.8.x.
Example on TYPO3 wiki :
http://wiki.typo3.org/wiki/Content_Slide#Content_Sliding_in_TYPO3_3.8.x_by_TS_only

Resources