def createLog(self, actor = None, component = '', log_action = '', log_resource = '', context = {}): if actor.is_authenticated: log = Log() log.user = str(actor) log.user_id = actor.id log.user_email = actor.email if self.log_context is not None: log.context = self.log_context else: log.context = context if self.log_component is not None: log.component = self.log_component else: log.component = component if self.log_action is not None: log.action = self.log_action else: log.action = log_action if self.log_resource is not None: log.resource = self.log_resource else: log.resource = log_resource log.save() if log.component == 'resources' and not actor.is_staff: resource = log.context[log.resource + '_id'] if log.resource + '_id' in log.context else 0 if not resource == 0: pendency = Pendencies.objects.filter(action = log.action, resource__id = resource, resource__visible = True).order_by('-id') if pendency.exists(): pendency = pendency.get() if ((not pendency.end_date or timezone.now() <= pendency.end_date) or (pendency.limit_date and timezone.now() <= pendency.limit_date) or (not pendency.end_date or (not pendency.limit_date and timezone.now() > pendency.end_date))): if actor in pendency.resource.students.all() or (pendency.resource.all_students and actor in pendency.resource.topic.subject.students.all()): if not PendencyDone.objects.filter(pendency = pendency, student = actor).exists(): pendencyDone = PendencyDone() pendencyDone.pendency = pendency pendencyDone.student = actor pendencyDone.done_date = timezone.now() if not pendency.begin_date or not pendency.end_date: pendencyDone.late = False elif pendency.begin_date > timezone.now(): pendencyDone.late = False elif pendency.begin_date <= timezone.now() <= pendency.end_date: pendencyDone.late = False elif (pendency.limit_date and pendency.end_date < timezone.now() <= pendency.limit_date) or (not pendency.limit_date and pendency.end_date < timezone.now()): pendencyDone.late = True pendencyDone.save()
def set_notifications(): pendencies = Pendencies.objects.filter( Q(begin_date__date__lte=timezone.now()) & Q(resource__visible=True) & (Q(end_date__date__gte=timezone.now()) | (Q(limit_date__isnull=False) & Q(limit_date__date__gte=timezone.now())))) for pendency in pendencies: users = get_resource_users(pendency.resource) subject_begin_date = pendency.resource.topic.subject.init_date pend_action = pendency.action resource_type = pendency.resource._my_subclass resource_key = resource_type + "_id" resource_id = pendency.resource.id for user in users: prev_notify = Notification.objects.filter( user=user, task=pendency).order_by("-creation_date") notify_type = 1 meta = None if prev_notify.count() > 0: last_notify = prev_notify[0] if last_notify.creation_date == date.today(): continue if last_notify.meta: if last_notify.creation_date < date.today( ) < last_notify.meta.date(): continue meta = last_notify.meta notify_type = 2 if not PendencyDone.objects.filter(pendency=pendency, student=user).exists(): has_action = Log.objects.filter( user_id=user.id, action=pend_action, resource=resource_type, context__contains={resource_key: resource_id}, datetime__date__gte=subject_begin_date) if not has_action.exists(): if pendency.end_date: if timezone.now() > pendency.end_date: notify_type = 3 if pendency.limit_date: if timezone.now() > pendency.limit_date: notify_type = 4 notification = Notification() notification.user = user notification.level = notify_type notification.task = pendency notification.meta = meta notification.save() else: has_action = has_action.order_by('datetime').first() done = PendencyDone() if pendency.begin_date <= has_action.datetime and ( has_action.datetime <= pendency.end_date or (pendency.limit_date and has_action.datetime <= pendency.limit_date)): done.pendency = pendency done.student = user done.done_date = has_action.datetime if pendency.limit_date and pendency.end_date < has_action.datetime <= pendency.limit_date: done.late = True else: done.late = False done.save() notificate()
def _decorator(request, *args, **kwargs): user = None #Get user before logout if request.user.is_authenticated: user = request.user response = view_function(request, *args, **kwargs) #Get user after login if user is None and request.user.is_authenticated: user = request.user log_context = {} if hasattr(request, 'log_context'): log_context = request.log_context if user: log = Log() log.user = str(user) log.user_id = user.id log.user_email = user.email log.component = log_component log.context = log_context log.action = log_action log.resource = log_resource log.save() if log.component == 'resources' and not user.is_staff: resource = log.context[ log.resource + '_id'] if log.resource + '_id' in log.context else 0 if not resource == 0: pendency = Pendencies.objects.filter( action=log.action, resource__id=resource, begin_date__date__lte=timezone.now(), resource__visible=True).order_by('-id') if pendency.exists(): pendency = pendency.get() if pendency.begin_date <= timezone.now() and ( timezone.now() <= pendency.end_date or (pendency.limit_date and timezone.now() <= pendency.limit_date) or (not pendency.limit_date and timezone.now() > pendency.end_date)): if user in pendency.resource.students.all( ) or (pendency.resource.all_students and user in pendency.resource.topic. subject.students.all()): if not PendencyDone.objects.filter( pendency=pendency, student=user).exists(): pendencyDone = PendencyDone() pendencyDone.pendency = pendency pendencyDone.student = user pendencyDone.done_date = timezone.now() if pendency.begin_date <= timezone.now( ) <= pendency.end_date: pendencyDone.late = False elif (pendency.limit_date and pendency.end_date < timezone.now() <= pendency.limit_date) or ( not pendency.limit_date and pendency.end_date < timezone.now()): pendencyDone.late = True pendencyDone.save() return response
def answer(request): question = request.POST.get("question") answer = request.POST.get("answer") question = get_object_or_404(UserAnswer, id=question) answer = get_object_or_404(Alternative, id=answer) log_action = "finish" insert_log = False userquest = question.user_quest if not UserAnswer.objects.filter(user_quest=userquest, answer__isnull=False).exists(): insert_log = True log_action = "start" question.answer = answer question.is_correct = answer.is_correct question.save() userquest.last_update = datetime.now() userquest.save() # add request context to log questionary_data = userquest.questionary request.log_context = {} request.log_context["question_id"] = userquest.questionary.id request.log_context["is_correct"] = question.is_correct request.log_context["time_to_answer"] = ( question.created_at - question.question.created_at).total_seconds() request.log_context["subject_id"] = questionary_data.topic.subject.id request.log_context[ "category_id"] = questionary_data.topic.subject.category.id request.log_context["topic_id"] = questionary_data.topic.id request.log_context["topic_slug"] = questionary_data.topic.slug request.log_context["topic_name"] = questionary_data.topic.name if (not UserAnswer.objects.filter(user_quest=userquest, answer__isnull=True).exists() or insert_log): log = Log() log.user = str(request.user) log.user_id = request.user.id log.user_email = request.user.email log.component = "resources" log.action = log_action log.resource = "questionary" log.context = {} log.context["subject_id"] = questionary_data.topic.subject.id log.context["category_id"] = questionary_data.topic.subject.category.id log.context["topic_id"] = questionary_data.topic.id log.context["topic_slug"] = questionary_data.topic.slug log.context["topic_name"] = questionary_data.topic.name log.context["questionary_id"] = questionary_data.id log.context["questionary_name"] = questionary_data.name log.context["questionary_slug"] = questionary_data.slug log.save() pendency = Pendencies.objects.filter( action=log.action, resource__id=questionary_data.id, begin_date__date__lte=timezone.now(), resource__visible=True, ).order_by("-id") if pendency.exists(): pendency = pendency.get() if pendency.begin_date <= timezone.now() and ( timezone.now() <= pendency.end_date or (pendency.limit_date and timezone.now() <= pendency.limit_date)): if request.user in pendency.resource.students.all() or ( pendency.resource.all_students and request.user in pendency.resource.topic.subject.students.all()): if not PendencyDone.objects.filter( pendency=pendency, student=request.user).exists(): pendencyDone = PendencyDone() pendencyDone.pendency = pendency pendencyDone.student = request.user pendencyDone.done_date = timezone.now() if pendency.begin_date <= timezone.now( ) <= pendency.end_date: pendencyDone.late = False elif (pendency.limit_date and pendency.end_date < timezone.now() <= pendency.limit_date): pendencyDone.late = True pendencyDone.save() return JsonResponse({ "last_update": formats.date_format(userquest.last_update, "SHORT_DATETIME_FORMAT"), "answered": userquest.useranswer_userquest.filter(answer__isnull=False).count(), })