Re: Create a Celery task with Django

2019-02-13 Thread Luis Zárate
See http://django.pyexcel.org/en/latest/

Clean code could be made with this library.

El miércoles, 13 de febrero de 2019, valentin jungbluth <
valentin.a...@gmail.com> escribió:
> Thank you Andréas !
>
> I understand your comment, but it could be possible to illustrate it with
my code ? I spent 1 week and up to now I don't find any way to solve my
issue and execute my Celery task :/
>
> --
> You received this message because you are subscribed to the Google Groups
"Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/c15b2681-a630-4691-960d-2897f914a553%40googlegroups.com
.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
"La utopía sirve para caminar" Fernando Birri

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAG%2B5VyMdaQ3s7ZDegwQxY_oYCxaxBPQooNg7X1VYgEhuKqiLJA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Create a Celery task with Django

2019-02-13 Thread Luis Zárate
As Andreas suggest I try to create a function that receive a file object
and save the excel content there, Django response works like a file so when
you have a less than 7 then pass the http response and change the
headers.  With celery you can store your firters on str as json file and
pass to celery delay, then create a temporary file and pass to the excel
function with the filters decoded.

Django support Media Fields, so you can create a special file and add to a
model and send to a view that check and return de media url.  Also
important to delete the file after a period of time.

El miércoles, 13 de febrero de 2019, valentin jungbluth <
valentin.a...@gmail.com> escribió:
> Thank you Andréas !
>
> I understand your comment, but it could be possible to illustrate it with
my code ? I spent 1 week and up to now I don't find any way to solve my
issue and execute my Celery task :/
>
> --
> You received this message because you are subscribed to the Google Groups
"Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/c15b2681-a630-4691-960d-2897f914a553%40googlegroups.com
.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
"La utopía sirve para caminar" Fernando Birri

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAG%2B5VyMFMnUWWF1fk6jAjddiNvqjRyh5PEY-VsG1RL1eFriLNQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Create a Celery task with Django

2019-02-13 Thread valentin jungbluth
Thank you Andréas !

I understand your comment, but it could be possible to illustrate it with 
my code ? I spent 1 week and up to now I don't find any way to solve my 
issue and execute my Celery task :/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c15b2681-a630-4691-960d-2897f914a553%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Create a Celery task with Django

2019-02-13 Thread C. Kirby
Not directly relevant to your celery question, but I would like to point 
you to a project of mine - 
https://django-modelqueryform.readthedocs.io/en/latest/ It lets you build 
search form that generates complex Q objects. It would replace a lot of you 
GET field checking and filtering on that view, and I  think make it much 
more maintainable.



On Wednesday, February 13, 2019 at 6:34:17 AM UTC-5, valentin jungbluth 
wrote:
>
> Hello guys,
>
> I'm working on my Django project and I need to integrate Celery to do 
> something.
> I'm using Django 1.11.20.
>
> *My context :*
>
> I have a template page with a search form. Once you did a search, it 
> returns a table with search results.
> It's possible to export to .xls format with/without search.
>
> There are 2 possibilities :
>
>
>- If user wants to export *less than 70.000 objects*, the export 
>creates an HttpResponse with download link.
>- If user wants to export *more than 70.000 objects*, a Celery task 
>has to be initiate, it creates the file, save it into the MEDIA folder and 
>user gets an email with expirable download link.
>
> I'm trying to implement this second part.
>
> The check is done on template page with number of results according to the 
> search part. 
> If < 70.000 objects, export button calls *export_xls()* function, else it 
> calls the Celery task 
>
> *get_xls_export()HTML Template file :*
>
> {% if item_count > 0 and item_count < 7 %}
> {% if search_info_str %}
>   
>class="glyphicon glyphicon-export">
> {% else %}
>class="button btn btn-default print_items">
>   
> {% endif %}
> {% else %}
> {% if search_info_str %}
>   
>class="button btn btn-default" href="{% url 'ocabr:cron_export' model=model 
> search_info=search_info_str %}">
> {% else %}
>class="button btn btn-default print_items">
>class="button btn btn-default" href="{% url 'ocabr:cron_export' model=model 
> %}">
> {% endif %}
> {% endif %}
>
> This part calls the right function depending on search result.
>
> *Case 1 : Less than 70.000 objects*
>
> The view associated to the previous HTML template is :
>
> class FinalProductListView(PermissionRequiredMixin, EdqmListSearchView):
>
> model = FinalProduct
> page_title = _('Final Products')
> default_sort_params = ('manufacturer_name', 'asc')
> sort_params = ['pk']
>
> create_link = 'ocabr:finalproduct-add'
> update_link = 'ocabr:finalproduct-edit'
> delete_link = 'ocabr:finalproduct-delete'
> view_link = 'ocabr:finalproduct-view'
> print_link = 'ocabr:print-object'
>
> permission_required = 'ocabr.view_finalproduct'
>
> template_name = 'ocabr/final_product/final_product_search.html'
>
> paginate_by = getattr(PaginationMixin, 'paginate_by', None)
> headers = {
> 'releasing_body__short_name': 'Releasing Body/Institution',
> 'is_withdrawn': 'Is nullified',
> }
>
> # Configuration of fields
> fields = ['manufacturer_name', 'releasing_body__short_name', 
> 'product_trade_name', 'pheur_name', 'name', 'final_bulk',
>   'decision', 'date_decision', 'is_withdrawn', 'member_state']
>
> def get_queryset(self):
> queryset = super(FinalProductListView, self).get_queryset()
>
> date_format = settings.USER_DATE_FORMAT
> # Add filter from search form.
> if 'mah' in self.request.GET:
> if self.request.GET['mah'] != '':
> queryset = 
> queryset.filter(mah__icontains=self.request.GET['mah'].strip())
> if 'releasing_body' in self.request.GET:
> if self.request.GET['releasing_body']:
> queryset = 
> queryset.filter(releasing_body__id=int(self.request.GET['releasing_body']))
> if 'trade_name' in self.request.GET:
> if self.request.GET['trade_name'] != '':
> queryset = 
> queryset.filter(product_trade_name__icontains=self.request.GET['trade_name'].strip())
> if 'member_state' in self.request.GET:
> if self.request.GET['member_state']:
> queryset = 
> queryset.filter(member_state__id=int(self.request.GET['member_state']))
> if 'pheur_name' in self.request.GET:
> if self.request.GET['pheur_name'] != '':
> queryset = 
> queryset.filter(pheur_name__id=self.request.GET['pheur_name'])
> if 'decision' in self.request.GET:
> if self.request.GET['decision']:
> queryset = 
> queryset.filter(decision__id=int(self.request.GET['decision']))
> if 'final_bulk_num' in self.request.GET:
> if self.request.GET['final_bulk_num'] != '':
> queryset = 
> queryset.filter(final_bulk__icontains=self.request.GET['final_bulk_num'].strip())
> if 'domain' in self.request.GET:
> if self.request.GET['domain']:
> queryset = 
> 

Re: Create a Celery task with Django

2019-02-13 Thread Andréas Kühne
Hi,

FIrst of all - try to put all the code that is the same for both calls into
one method. This way you won't get any problems with that in the future
when you need to update the produced excel file. You can actually use the
same method in both cases because if you call the method WITHOUT delay() on
it, it will be called like a normal method.

To answer your question - all you need to do is have a view that calls the
get_xls_export.delay() task. What I would do is I would put all of the code
for the selection of which export to use into a view class. Then if the
count is less than 7 I would call the standard export without celery
and if it's more than 7 I would call the delayed function. So your View
could contain something like this:

def get():
  if count < 7:
 exported_file = export_xls()
  else:
 export_xls.delay()


Or something like that (this is just pseudocode :-))

Regards

Andréas


Den ons 13 feb. 2019 kl 12:34 skrev valentin jungbluth <
valentin.a...@gmail.com>:

> Hello guys,
>
> I'm working on my Django project and I need to integrate Celery to do
> something.
> I'm using Django 1.11.20.
>
> *My context :*
>
> I have a template page with a search form. Once you did a search, it
> returns a table with search results.
> It's possible to export to .xls format with/without search.
>
> There are 2 possibilities :
>
>
>- If user wants to export *less than 70.000 objects*, the export
>creates an HttpResponse with download link.
>- If user wants to export *more than 70.000 objects*, a Celery task
>has to be initiate, it creates the file, save it into the MEDIA folder and
>user gets an email with expirable download link.
>
> I'm trying to implement this second part.
>
> The check is done on template page with number of results according to the
> search part.
> If < 70.000 objects, export button calls *export_xls()* function, else it
> calls the Celery task
>
> *get_xls_export()HTML Template file :*
>
> {% if item_count > 0 and item_count < 7 %}
> {% if search_info_str %}
>   
>class="glyphicon glyphicon-export">
> {% else %}
>class="button btn btn-default print_items">
>   
> {% endif %}
> {% else %}
> {% if search_info_str %}
>   
>class="button btn btn-default" href="{% url 'ocabr:cron_export' model=model
> search_info=search_info_str %}">
> {% else %}
>class="button btn btn-default print_items">
>class="button btn btn-default" href="{% url 'ocabr:cron_export' model=model
> %}">
> {% endif %}
> {% endif %}
>
> This part calls the right function depending on search result.
>
> *Case 1 : Less than 70.000 objects*
>
> The view associated to the previous HTML template is :
>
> class FinalProductListView(PermissionRequiredMixin, EdqmListSearchView):
>
> model = FinalProduct
> page_title = _('Final Products')
> default_sort_params = ('manufacturer_name', 'asc')
> sort_params = ['pk']
>
> create_link = 'ocabr:finalproduct-add'
> update_link = 'ocabr:finalproduct-edit'
> delete_link = 'ocabr:finalproduct-delete'
> view_link = 'ocabr:finalproduct-view'
> print_link = 'ocabr:print-object'
>
> permission_required = 'ocabr.view_finalproduct'
>
> template_name = 'ocabr/final_product/final_product_search.html'
>
> paginate_by = getattr(PaginationMixin, 'paginate_by', None)
> headers = {
> 'releasing_body__short_name': 'Releasing Body/Institution',
> 'is_withdrawn': 'Is nullified',
> }
>
> # Configuration of fields
> fields = ['manufacturer_name', 'releasing_body__short_name',
> 'product_trade_name', 'pheur_name', 'name', 'final_bulk',
>   'decision', 'date_decision', 'is_withdrawn', 'member_state']
>
> def get_queryset(self):
> queryset = super(FinalProductListView, self).get_queryset()
>
> date_format = settings.USER_DATE_FORMAT
> # Add filter from search form.
> if 'mah' in self.request.GET:
> if self.request.GET['mah'] != '':
> queryset =
> queryset.filter(mah__icontains=self.request.GET['mah'].strip())
> if 'releasing_body' in self.request.GET:
> if self.request.GET['releasing_body']:
> queryset =
> queryset.filter(releasing_body__id=int(self.request.GET['releasing_body']))
> if 'trade_name' in self.request.GET:
> if self.request.GET['trade_name'] != '':
> queryset =
> queryset.filter(product_trade_name__icontains=self.request.GET['trade_name'].strip())
> if 'member_state' in self.request.GET:
> if self.request.GET['member_state']:
> queryset =
> queryset.filter(member_state__id=int(self.request.GET['member_state']))
> if 'pheur_name' in self.request.GET:
> if self.request.GET['pheur_name'] != '':
> queryset =
> queryset.filter(pheur_name__id=self.request.GET['pheur_name'])
> if 

Create a Celery task with Django

2019-02-13 Thread valentin jungbluth
Hello guys,

I'm working on my Django project and I need to integrate Celery to do 
something.
I'm using Django 1.11.20.

*My context :*

I have a template page with a search form. Once you did a search, it 
returns a table with search results.
It's possible to export to .xls format with/without search.

There are 2 possibilities :


   - If user wants to export *less than 70.000 objects*, the export creates 
   an HttpResponse with download link.
   - If user wants to export *more than 70.000 objects*, a Celery task has 
   to be initiate, it creates the file, save it into the MEDIA folder and user 
   gets an email with expirable download link.

I'm trying to implement this second part.

The check is done on template page with number of results according to the 
search part. 
If < 70.000 objects, export button calls *export_xls()* function, else it 
calls the Celery task 

*get_xls_export()HTML Template file :*

{% if item_count > 0 and item_count < 7 %}
{% if search_info_str %}
  
  
{% else %}
  
  
{% endif %}
{% else %}
{% if search_info_str %}
  
  
{% else %}
  
  
{% endif %}
{% endif %}

This part calls the right function depending on search result.

*Case 1 : Less than 70.000 objects*

The view associated to the previous HTML template is :

class FinalProductListView(PermissionRequiredMixin, EdqmListSearchView):

model = FinalProduct
page_title = _('Final Products')
default_sort_params = ('manufacturer_name', 'asc')
sort_params = ['pk']

create_link = 'ocabr:finalproduct-add'
update_link = 'ocabr:finalproduct-edit'
delete_link = 'ocabr:finalproduct-delete'
view_link = 'ocabr:finalproduct-view'
print_link = 'ocabr:print-object'

permission_required = 'ocabr.view_finalproduct'

template_name = 'ocabr/final_product/final_product_search.html'

paginate_by = getattr(PaginationMixin, 'paginate_by', None)
headers = {
'releasing_body__short_name': 'Releasing Body/Institution',
'is_withdrawn': 'Is nullified',
}

# Configuration of fields
fields = ['manufacturer_name', 'releasing_body__short_name', 
'product_trade_name', 'pheur_name', 'name', 'final_bulk',
  'decision', 'date_decision', 'is_withdrawn', 'member_state']

def get_queryset(self):
queryset = super(FinalProductListView, self).get_queryset()

date_format = settings.USER_DATE_FORMAT
# Add filter from search form.
if 'mah' in self.request.GET:
if self.request.GET['mah'] != '':
queryset = 
queryset.filter(mah__icontains=self.request.GET['mah'].strip())
if 'releasing_body' in self.request.GET:
if self.request.GET['releasing_body']:
queryset = 
queryset.filter(releasing_body__id=int(self.request.GET['releasing_body']))
if 'trade_name' in self.request.GET:
if self.request.GET['trade_name'] != '':
queryset = 
queryset.filter(product_trade_name__icontains=self.request.GET['trade_name'].strip())
if 'member_state' in self.request.GET:
if self.request.GET['member_state']:
queryset = 
queryset.filter(member_state__id=int(self.request.GET['member_state']))
if 'pheur_name' in self.request.GET:
if self.request.GET['pheur_name'] != '':
queryset = 
queryset.filter(pheur_name__id=self.request.GET['pheur_name'])
if 'decision' in self.request.GET:
if self.request.GET['decision']:
queryset = 
queryset.filter(decision__id=int(self.request.GET['decision']))
if 'final_bulk_num' in self.request.GET:
if self.request.GET['final_bulk_num'] != '':
queryset = 
queryset.filter(final_bulk__icontains=self.request.GET['final_bulk_num'].strip())
if 'domain' in self.request.GET:
if self.request.GET['domain']:
queryset = 
queryset.filter(domain__id=int(self.request.GET['domain']))
if 'manufacturer_name' in self.request.GET:
if self.request.GET['manufacturer_name'] != '':
queryset = 
queryset.filter(manufacturer_name__id=self.request.GET['manufacturer_name'])
if 'date_from' in self.request.GET and 'date_to' in 
self.request.GET:
if self.request.GET['date_from'] and not 
self.request.GET['date_to']:
try:
queryset = 
queryset.filter(date_decision__gte=Utils.filter_date_format(self.request.GET['date_from'],
 
date_format))
except:
pass
if not self.request.GET['date_from'] and 
self.request.GET['date_to']:
try:
queryset = 
queryset.filter(date_decision__lte=Utils.filter_date_format(self.request.GET['date_to'],
 
date_format))
except:
pass
if self.request.GET['date_from'] and 
self.request.GET['date_to']: