def save_model(self, request, obj, form, change): """ Save the model. Notify subscribers of new issue. :param request: The current request. :param obj: The model object. :param form: The parent form instance. :param change: True if the model is updated, not created. :return: None """ # Get extra args changes_comment = form.cleaned_data['changes_comment'] changes_author_ip_address = get_client_ip_address(request) changes_author = request.user # Update IP address field if new object if not change: obj.submitter_ip_address = get_client_ip_address(request) # Save the model obj.save(changes_comment=changes_comment, changes_author=changes_author, changes_author_ip_address=changes_author_ip_address, request=request) # Notify subscribers of new issue if not change: notify_of_new_issue(obj, request, obj.submitter)
def save_formset(self, request, form, formset, change): """ Save the formset for the inline comments. Notify subscribers of new comment. :param request: The current request. :param form: The parent form instance. :param formset: The formset instance. :param change: True if the form has change. :return: None """ # Overload save method for inline comments instances = formset.save(commit=False) for obj in formset.deleted_objects: obj.delete() for instance in instances: # Update IP address field if new object if not instance.pk: just_created = True instance.author_ip_address = get_client_ip_address(request) else: just_created = False # Save the model instance.save() # Notify subscribers of new comment if just_created: notify_of_new_comment(instance.issue, instance, request, instance.author) formset.save_m2m()
def save(self, request, parent_forum, author): """ Save the new thread. :param request: The current request. :param parent_forum: The parent forum. :param author: The current user, to be used has thread's author. :return: The new thread object. """ # Create the new thread and first post new_thread = ForumThread.objects.create_thread(parent_forum=parent_forum, title=self.cleaned_data['title'], author=author, pub_date=timezone.now(), content=self.cleaned_data['content'], author_ip_address=get_client_ip_address(request), closed=self.cleaned_data['closed'], resolved=self.cleaned_data['resolved']) # Handle attachments # self.handle_new_attachments(new_thread.first_post) # Add subscriber if necessary if self.cleaned_data['notify_of_reply']: ForumThreadSubscription.objects.subscribe_to_thread(author, new_thread) # Notify subscribers notify_of_new_forum_thread(new_thread, request, author) # Return the newly created object return new_thread
def save_formset(self, request, form, formset, change): """ Save inline forms (used for posts). Fix user's IP address on save() and notify subscribers of new post. :param request: The current request. :param form: The form instance. :param formset: The formset instance. :param change: True if form data has changed. :return: None """ # Overload save method for inline posts instances = formset.save(commit=False) for obj in formset.deleted_objects: obj.delete() for instance in instances: # Update IP address field if new object if not instance.pk: just_created = True instance.author_ip_address = get_client_ip_address(request) else: just_created = False # Save the model instance.save(current_user=request.user) # Notify subscribers of new comment if just_created: notify_of_new_thread_post(instance, request, instance.author) formset.save_m2m()
def process_request(self, request): """ Process the request, search for strike and do the job. :param request: The current request instance. """ # Super admin cannot be kicked out if request.user.is_superuser: return # Search for strike current_user = request.user if request.user.is_authenticated() else None ip_address = get_client_ip_address(request) strike = UserStrike.objects.search_for_strike(user=current_user, ip_address=ip_address) # Test if strike found if strike and strike.block_access: # Block access to the website context = { 'strike': strike, } return TemplateResponse(request, self.template_name, context, status=403) elif strike: # Warn user reason_msg = _('The reason of this warning is: "%s".') % strike.public_reason if strike.public_reason else '' expire_msg = _('This warning will expire at %s.') % strike.expiration_date.isoformat(' ') if strike.expiration_date else _('This warning is not time limited.') messages.add_message(request, messages.WARNING, '%s\n%s\n%s' % (_('You have received a warning from an administrator.'), reason_msg, expire_msg), extra_tags='strike_warning', fail_silently=True)
def save(self, *args, **kwargs): """ Save the form and related model. :param request: The current request. :param author: The current user, to be used as post's author. :param args: Any arguments for super() :param kwargs: Any keyword arguments for super() :return: None """ request = kwargs.pop('request', None) author = kwargs.pop('author', None) post = super(ForumThreadPostEditForm, self).save(commit=False) # Avoid oops assert request is not None assert author is not None # Handle deleted attachments # self.handle_deleted_attachments(post) # Handle new attachments # self.handle_new_attachments(post) # Manual update of runtime fields post.last_modification_by = author if post.author == author: # Update IP address if the original author edit the post # If another user edit the post (moderator, admin, ...), IP is not altered. post.author_ip_address = get_client_ip_address(request) # Save the model post.save(current_user=author) self.save_m2m()
def save(self, request, parent_thread, author): """ Save the new post. :param request: The current request. :param parent_thread: The parent thread instance. :param author: The current user, to be used as post's author. :return: The newly created post instance. """ # Create the post new_post = ForumThreadPost.objects.create(parent_thread=parent_thread, author=author, content=self.cleaned_data['content'], author_ip_address=get_client_ip_address(request)) # Handle attachments # self.handle_new_attachments(new_post) # Add subscriber if necessary if self.cleaned_data['notify_of_reply']: ForumThreadSubscription.objects.subscribe_to_thread(author, parent_thread) else: ForumThreadSubscription.objects.unsubscribe_from_thread(author, parent_thread) # Notify subscribers notify_of_new_thread_post(new_post, request, author) # Return the newly created post object return new_post
def save(self, *args, **kwargs): """ Save the model instance related to this form. :param request: The current request. :param author: The current user, to be use as author (of the modification). :param args: Extra arguments for super() :param kwargs: Extra keyword arguments for super() :return: None """ request = kwargs.pop('request', None) author = kwargs.pop('author', None) instance = super(ForumThreadEditionForm, self).save(*args, **kwargs) # Avoid oops assert request is not None assert author is not None # Handle deleted attachments first_post = instance.first_post # self.handle_deleted_attachments(first_post) # Handle new attachments # self.handle_new_attachments(first_post) # Save the first post's content first_post.content = self.cleaned_data['content'] if first_post.author == author: # Update IP address if the original author edit the post # If another user edit the post (moderator, admin, ...), IP is not altered. first_post.author_ip_address = get_client_ip_address(request) first_post.save(current_user=author)
def store_current_ip_address(user, request): """ Store the current IP address of the user. :param user: The currently logged-in user. :param request: The current request instance. """ ip_address = get_client_ip_address(request) user_profile = user.user_profile user_profile.last_login_ip_address = ip_address user_profile.save_no_rendering(update_fields=('last_login_ip_address',))
def _handle_user_login_success(sender, request, user, **kwargs): """ Handle user login in. :param sender: The sender class. :param request: The current request. :param user: The logged-in user. :param kwargs: Extra keywords arguments. :return: None """ # Log the event LogEvent.objects.create(type=LOG_EVENT_LOGIN_SUCCESS, username=user.username, ip_address=get_client_ip_address(request))
def _handle_user_logout(sender, request, user, **kwargs): """ Handle user logout. :param sender: The sender class. :param request: The current request. :param user: The logged-out user. :param kwargs: Extra keywords arguments. :return: None """ # Do nothing if the user was not logged-in if user is None: return # Log the event LogEvent.objects.create(type=LOG_EVENT_LOGOUT, username=user.username, ip_address=get_client_ip_address(request))
def save(self, request, content_obj, reporter): """ Save the form by creating a new ``ContentReport`` for the given content object. :param request: The current request. :param content_obj: The related content instance. :param reporter: The author of this report. """ # Create the report new_report = ContentReport.objects.create_report(content_object=content_obj, reporter=reporter, reporter_ip_address=get_client_ip_address(request), reason=self.cleaned_data['reason'], request=request, extra_notification_kwargs=self.get_extra_notification_kwargs()) # Return the newly created object return new_report
def save(self, request, submitter): """ Save the form by creating a new ``IssueTicket``. :param request: The current request. :param submitter: The ticket's submitter. :return The newly created ticket. """ new_obj = IssueTicket.objects.create(title=self.cleaned_data['title'], description=self.cleaned_data['description'], submitter=submitter, submitter_ip_address=get_client_ip_address(request)) # Add subscriber if necessary if self.cleaned_data['notify_of_reply']: IssueTicketSubscription.objects.subscribe_to_issue(submitter, new_obj) # Notify subscribers notify_of_new_issue(new_obj, request, submitter) # Return the newly created object return new_obj
def save(self, request, issue, author): """ Save the form by creating a new ``IssueComment`` for the given ``IssueTicket``. Drop a success flash message after saving. :param request: The current request. :param issue: The related issue instance. :param author: The author of this comment. """ new_obj = IssueComment.objects.create(issue=issue, author=author, body=self.cleaned_data['comment_body'], author_ip_address=get_client_ip_address(request)) # Add subscriber if necessary if self.cleaned_data['notify_of_reply']: IssueTicketSubscription.objects.subscribe_to_issue(author, new_obj.issue) else: IssueTicketSubscription.objects.unsubscribe_from_issue(author, new_obj.issue) # Notify subscribers notify_of_new_comment(issue, new_obj, request, author) # Return the newly created object return new_obj