0

I want to display the contents of a table called recommendation in the interface Django of the user in the morning of each day because I'm collecting recommendations and saving them in a table (each user have his own recommendations of course ) and I want to delete them after he check them.

So I'm doing this but that is going to display the recommendations directly if I refreshed the page.

HTML interface :

 <li class="word-bot-sent"><p>Do you want some recommendations today? </p></li>
                               {% for obj in Recommendation %}
                                       {% if obj.user == request.user %}
                                       <li class="word-bot-sent"><p>{{ obj.title }}</p></li>
                                       <li class="word-bot-sent"><p>{{ obj.text }}</p></li>
                                       {%endif%}
                                       {% empty %}
                               {% endfor %}

How can I tackle this?

halfer
  • 19,824
  • 17
  • 99
  • 186
so96
  • 11
  • 2
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Aug 09 '20 at 23:41

1 Answers1

0

You'll probably want to take a look at Django's time zones docs. You could do something like:

from django.utils import timezone
from django.views.generic import ListView

from .models import Recommendation


class RecommendationListView(ListView):
    model = Recommendation
    template_name = 'mytemplate.html'
    context_object_name = 'recommendations'

    def get_queryset(self):
        current_hour = timezone.now().hour
        if current_hour > 8 and current_hour < 11:
            queryset = Recommendation.objects.filter(user=self.request.user)
            # force the queryset to be evaluated
            eval_query = list(queryset)
            # delete the data from the database now that you have them in a list
            queryset.delete() 
            return eval_query
        else:
            return Recommendation.objects.none()

... but you will have to take into account if your users are in the same time zone as you, and if not, how you can set things up so that they work properly. You'll also have to consider that it's likely that users might try to abuse the system if you make the site timezone-aware/adjusted based on the users' timezones. e. g. they might try to pose as being in a different time zone to get the recommendations earlier.

Also, the above method of deletion isn't really appropriate because if for whatever reason (say if the user's internet connection is finicky) the user doesn't receive the server's response and tries to access the page again, the data have already been removed. I'm sure there is a better solution which avoids this issue. What you would need is some confirmation from the user that they've actually seen the page, or use AJAX to collect an indication of the user viewing the page. You can see some additional discussion on a similar topic in this SO thread.

datalowe
  • 589
  • 2
  • 7
  • i tried to add current_time to the context of the view and i put {% if current_hour > 20 and current_hour < 21 %} in the template but i didn't work – so96 Aug 09 '20 at 19:40
  • Is there a particular reason you want to insert that into the template? The method I suggested doesn't involve changing things in the template, it only affects what queryset/data is passed to the context (with the name 'recommendations' in the example I described). In the template you could loop over the data using {% for recommendation in recommendations %}...{% endfor %}. You don't need to add additional logic in the template, I believe. – datalowe Aug 09 '20 at 20:22
  • thank you for your solution i did exactly what you told me to do, i put the function in the home function and the queryset in the context of the template. but i got this error "ValueError: too many values to unpack (expected 2)" – so96 Aug 10 '20 at 09:56
  • If you changed things to exactly what I wrote (that wasn't really my intent, since I don't know what your code looks like otherwise - it was just an attempt to illustrate what I meant), then you would have to change your template loop to {% for obj in recommendations %}... And remove the {% if %} tags within the loop. It might be advisable to add a {% if recommendations %} check __outside__ of the loop however, to first check if the queryset is empty. – datalowe Aug 10 '20 at 10:38
  • I'm not sure what's causing the ValueError you descrbe - you can read about ValueError in general http://net-informations.com/python/err/value.htm or specifically in the context of django https://stackoverflow.com/questions/20820830/django-too-many-values-to-unpack-when-calling-user-objects-get/20830181 Make sure that all queryset method calls you use (from view code or when using template tags) refer to correct attributes. You might also try to add print statements at different points in your code and check your console to see what's going on and when. – datalowe Aug 10 '20 at 10:40