Пример #1
0
def create_session(user, request):
    return UserSession.objects.create(
        user=user,
        token=generate_session_token(),
        ip_address=get_client_ip(request),
        user_agent=get_client_user_agent(request),
    )
Пример #2
0
 def dispatch(self, request, *args, **kwargs):
     instance_user = self.request.user
     if not self.user_passes_test(request):
         suspicious_user = Suspicious.objects.filter(user=instance_user)
         if suspicious_user.exists():
             suspicious_user_instance = Suspicious.objects.get(
                 user=instance_user)
             current_attempt = suspicious_user_instance.attempt
             total_attempt = current_attempt + 1
             update_time = datetime.datetime.now()
             suspicious_user.update(attempt=total_attempt,
                                    last_attempt=update_time)
         else:
             client_ip = get_client_ip(request)
             client_mac = get_mac()
             Suspicious.objects.get_or_create(user=instance_user,
                                              ip=client_ip,
                                              mac=client_mac)
         messages.add_message(
             self.request, messages.ERROR,
             "You are not allowed. Your account is being tracked for suspicious activity !"
         )
         return HttpResponseRedirect(reverse('home'))
     return super(SelectedContributionListView,
                  self).dispatch(request, *args, **kwargs)
Пример #3
0
 def dispatch(self, request, *args, **kwargs):
     instance_user = self.request.user
     today = datetime.date.today()
     if today.month == 12:
         messages.add_message(
             self.request, messages.WARNING,
             "Schedule is not modifiable on month December!")
         return HttpResponseRedirect(reverse('home'))
     if not self.user_passes_test(request):
         suspicious_user_filter = Suspicious.objects.filter(
             user=instance_user)
         if suspicious_user_filter.exists():
             suspicious_user_instance = suspicious_user_filter.first()
             current_attempt = suspicious_user_instance.attempt
             total_attempt = current_attempt + 1
             update_time = datetime.datetime.now()
             suspicious_user_filter.update(attempt=total_attempt,
                                           last_attempt=update_time)
         else:
             client_ip = get_client_ip(request)
             client_mac = get_mac()
             Suspicious.objects.get_or_create(user=instance_user,
                                              ip=client_ip,
                                              mac=client_mac)
         messages.add_message(
             self.request, messages.ERROR,
             "You are not allowed. Your account is being tracked for suspicious activity !"
         )
         return HttpResponseRedirect(reverse('home'))
     return super(DateUpdateView, self).dispatch(request, *args, **kwargs)
Пример #4
0
def contribution_delete(request):
    url = reverse('home')
    if request.method == "POST":
        slug = request.POST.get("slug")
        qs = Contribution.objects.filter(slug=slug)
        if qs.exists():
            contribution = qs.first()
            if contribution.user.user == request.user:
                today = datetime.datetime.now()
                academic_year = Date.objects.filter(
                    academic_year=str(today.year))
                if academic_year.exists():
                    closure_date = academic_year.first().closure_date
                    # today_format = today.strptime('2018-11-10 10:55:31', '%Y-%m-%d %H:%M:%S')
                    # print(closure_date)
                    # print(today_format)
                    if closure_date > today:
                        if contribution.is_selected == False:
                            qs.delete()
                            messages.add_message(
                                request, messages.SUCCESS,
                                "Contribution deleted successfully!")
                            return redirect("/")
                        else:
                            messages.add_message(
                                request, messages.WARNING,
                                "You cannot delete this contribution as it has been selected for the magazine!"
                            )
                    else:
                        messages.add_message(
                            request, messages.WARNING,
                            "Modification time has been expired ! You cannot delete this."
                        )
            else:
                instance_user = request.user
                suspicious_user = Suspicious.objects.filter(user=instance_user)
                if suspicious_user.exists():
                    suspicious_user_instance = Suspicious.objects.get(
                        user=instance_user)
                    current_attempt = suspicious_user_instance.attempt
                    total_attempt = current_attempt + 1
                    update_time = datetime.datetime.now()
                    suspicious_user.update(attempt=total_attempt,
                                           last_attempt=update_time)
                else:
                    client_ip = get_client_ip(request)
                    client_mac = get_mac()
                    Suspicious.objects.get_or_create(user=instance_user,
                                                     ip=client_ip,
                                                     mac=client_mac)
                messages.add_message(
                    request, messages.ERROR,
                    "You are not allowed. Your account is being tracked for suspicious activity !"
                )
        else:
            messages.add_message(request, messages.WARNING,
                                 "Contribution Doesn't Exists!!!")
    return HttpResponseRedirect(url)
Пример #5
0
def login(request, user):
    """
    登录,在原功能上加入了记录ip地址的功能
    """
    from accounts.utils import get_client_ip
    user.ip_address = get_client_ip(request)
    user.save()

    from django.contrib.auth import login as _login
    _login(request, user)
Пример #6
0
def comment_create(request, slug):
    url = reverse('home')
    if request.method == 'POST':
        user = request.user
        user_profile = UserProfile.objects.filter(user=user).first()
        comment = request.POST.get('comment')
        comment_area = request.POST.get('comment_area')
        if user_profile.role == 2 or user_profile.role == 4:
            contribution_qs = Contribution.objects.filter(slug=slug)
            if contribution_qs.exists():
                contribution = contribution_qs.first()
                today = datetime.datetime.now()
                is_special = False
                if contribution.is_commented == False:
                    day_difference = today - contribution.created_at
                    if day_difference.days > 14:
                        is_special = True
                Comment.objects.create(contribution=contribution,
                                       commented_by=user_profile,
                                       comment=comment,
                                       is_special=is_special)
                # messages.add_message(request, messages.SUCCESS, "Commented successfully!")
                if user_profile.role == 2:
                    contribution_qs.update(is_commented=True)
                if comment_area == "absolute_comment":
                    url = reverse('contribution:comment_view',
                                  kwargs={'slug': slug})
                else:
                    url = reverse('contribution:contribution_detail',
                                  kwargs={'slug': slug})
            else:
                messages.add_message(request, messages.WARNING,
                                     "Contribution doesn't exists !!!")
        else:
            instance_user = request.user
            suspicious_user = Suspicious.objects.filter(user=instance_user)
            if suspicious_user.exists():
                suspicious_user_instance = Suspicious.objects.get(
                    user=instance_user)
                current_attempt = suspicious_user_instance.attempt
                total_attempt = current_attempt + 1
                update_time = datetime.datetime.now()
                suspicious_user.update(attempt=total_attempt,
                                       last_attempt=update_time)
            else:
                client_ip = get_client_ip(request)
                client_mac = get_mac()
                Suspicious.objects.get_or_create(user=instance_user,
                                                 ip=client_ip,
                                                 mac=client_mac)
            messages.add_message(
                request, messages.ERROR,
                "You are not allowed. Your account is being tracked for suspicious activity !"
            )
    return HttpResponseRedirect(url)
Пример #7
0
 def dispatch(self, request, *args, **kwargs):
     instance_user = self.request.user
     user = UserProfile.objects.filter(user=instance_user).first()
     today = datetime.datetime.today()
     date_filter = Date.objects.filter(academic_year=today.year)
     if date_filter.exists():
         date = date_filter.first()
         submitted_contribution = Contribution.objects.filter(
             user=user, updated_at__year=today.year)
         if submitted_contribution.exists():
             submitted = True
         else:
             submitted = False
         if today > date.closure_date and submitted == False:
             messages.add_message(
                 self.request, messages.ERROR,
                 "Contribution submitting date has been expired! You are not allowed."
             )
             return HttpResponseRedirect(reverse('home'))
         if today > date.final_closure_date and submitted == True:
             messages.add_message(
                 self.request, messages.ERROR,
                 "Contribution updating date has been expired! You are not allowed."
             )
             return HttpResponseRedirect(reverse('home'))
     else:
         messages.add_message(
             self.request, messages.ERROR,
             "No Schedule Added for collecting contributions for magazine! Please wait for the announcement."
         )
         return HttpResponseRedirect(reverse('home'))
     if not self.user_passes_test(request):
         suspicious_user = Suspicious.objects.filter(user=instance_user)
         if suspicious_user.exists():
             suspicious_user_instance = Suspicious.objects.get(
                 user=instance_user)
             current_attempt = suspicious_user_instance.attempt
             total_attempt = current_attempt + 1
             update_time = datetime.datetime.now()
             suspicious_user.update(attempt=total_attempt,
                                    last_attempt=update_time)
         else:
             client_ip = get_client_ip(request)
             client_mac = get_mac()
             Suspicious.objects.get_or_create(user=instance_user,
                                              ip=client_ip,
                                              mac=client_mac)
         messages.add_message(
             self.request, messages.ERROR,
             "You are not allowed. Your account is being tracked for suspicious activity !"
         )
         return HttpResponseRedirect(reverse('home'))
     return super(ContributionUploadView,
                  self).dispatch(request, *args, **kwargs)
Пример #8
0
def mark_as_selected(request, slug):
    url = reverse('home')
    contribution_filter = Contribution.objects.filter(slug=slug)
    if contribution_filter.exists():
        contribution = contribution_filter.first()
        user_filter = UserProfile.objects.filter(user=request.user, role=2)
        if user_filter.exists() and user_filter.first(
        ).faculty == contribution.user.faculty:
            contribution_filter.update(is_selected=True)
            url = reverse('contribution:contribution_list')
            if request.method == 'POST':
                if not request.POST.get('default_comment') == "":
                    default_comment = request.POST.get('default_comment')
                    user_profile = user_filter.first()
                    comment = request.POST.get('comment')
                    if user_profile.role == 2:
                        Comment.objects.create(contribution=contribution,
                                               commented_by=user_profile,
                                               comment=default_comment)
                        if user_profile.role == 2:
                            contribution_filter.update(is_commented=True)
            messages.add_message(
                request, messages.SUCCESS,
                "Successfully marked as selected contribution!")
        else:
            instance_user = request.user
            suspicious_user = Suspicious.objects.filter(user=instance_user)
            if suspicious_user.exists():
                suspicious_user_instance = Suspicious.objects.get(
                    user=instance_user)
                current_attempt = suspicious_user_instance.attempt
                total_attempt = current_attempt + 1
                update_time = datetime.datetime.now()
                suspicious_user.update(attempt=total_attempt,
                                       last_attempt=update_time)
            else:
                client_ip = get_client_ip(request)
                client_mac = get_mac()
                Suspicious.objects.get_or_create(user=instance_user,
                                                 ip=client_ip,
                                                 mac=client_mac)
            messages.add_message(
                request, messages.ERROR,
                "You are not allowed. Your account is being tracked for suspicious activity !"
            )
    else:
        messages.add_message(request, messages.WARNING,
                             "Contribution doesn't exists !!!")
    return HttpResponseRedirect(url)
Пример #9
0
 def dispatch(self, request, *args, **kwargs):
     instance_user = self.request.user
     today = datetime.date.today()
     schedule_filter = Date.objects.filter(academic_year=today.year)
     if schedule_filter.exists():
         messages.add_message(
             self.request, messages.WARNING,
             "Already have a Schedule for this academic year! You cannot add more."
         )
         return HttpResponseRedirect(reverse('home'))
     if today.month == 12:
         messages.add_message(
             self.request, messages.WARNING,
             "New Schedule cannot be added on month December ! Please add New Schedule in next year."
         )
         return HttpResponseRedirect(reverse('home'))
     if not self.user_passes_test(request):
         suspicious_user_filter = Suspicious.objects.filter(
             user=instance_user)
         if suspicious_user_filter.exists():
             suspicious_user_instance = suspicious_user_filter.first()
             current_attempt = suspicious_user_instance.attempt
             total_attempt = current_attempt + 1
             update_time = datetime.datetime.now()
             suspicious_user_filter.update(attempt=total_attempt,
                                           last_attempt=update_time)
         else:
             client_ip = get_client_ip(request)
             client_mac = get_mac()
             Suspicious.objects.get_or_create(user=instance_user,
                                              ip=client_ip,
                                              mac=client_mac)
         messages.add_message(
             self.request, messages.ERROR,
             "You are not allowed. Your account is being tracked for suspicious activity !"
         )
         return HttpResponseRedirect(reverse('home'))
     return super(DateCreateView, self).dispatch(request, *args, **kwargs)