GetTogether/totd/context_processors.py
2018-12-13 23:22:25 -05:00

26 lines
No EOL
759 B
Python

from django.db.models import Q
from .models import Tip
import datetime
def tips(request):
"""
Adds a list of tips for the current request
"""
if not request.user.is_authenticated:
return {}
#import pdb; pdb.set_trace()
try:
tips = Tip.objects.filter(run_start__lte=datetime.datetime.now())
tips = tips.filter(Q(run_end__isnull=True) | Q(run_end__gte=datetime.datetime.now()))
tips = tips.filter(Q(view='') | Q(view=request.resolver_match.url_name)).exclude(seen_by=request.user)
if len(tips) > 0:
tips[0].seen_by.add(request.user)
return {
'tip': tips[0],
}
except:
pass # Don't show a TotD if anything went wrong
return {}