def rename(request, user): return_path = moderation_return_path(request, user) form = ChangeUsernameForm(user=user) if request.method == 'POST': old_username = user.username form = ChangeUsernameForm(request.POST, user=user) if form.is_valid(): try: form.change_username(changed_by=request.user) notify_user(user, _("%(user)s has changed your name to %(newname)s."), reverse('misago:user_warnings', kwargs={ 'user_slug': user.slug, 'user_id': user.pk }), "name_history_%s" % user.pk, formats={ 'user': request.user.username, 'newname': user.username, }, sender=request.user) message = _("%(old_username)s's username has been changed.") message = message % {'old_username': old_username} messages.success(request, message) return redirect(return_path) except IntegrityError: message = _("Error changing username. Please try again.") messages.error(request, message) return render(request, 'misago/modusers/rename.html', {'profile': user, 'form': form, 'return_path': return_path})
def test_regen_blank_avatar(self): """command deletes old notifications""" # create user User = get_user_model() user = User.objects.create_user("Bob", "*****@*****.**", "Pass.123") # notify him notify_user(user, "Hello Bob!", "/") # run command command = deleteoldnotifications.Command() out = StringIO() command.execute(stdout=out) command_output = out.getvalue().splitlines()[0].strip() self.assertEqual(command_output, 'Old notifications have been deleted') self.assertEqual(user.misago_notifications.count(), 1) # outdate notifications cutoff = timedelta(days=settings.MISAGO_NOTIFICATIONS_MAX_AGE * 2) cutoff_date = timezone.now() - cutoff user.misago_notifications.update(date=cutoff_date) # run command again out = StringIO() command.execute(stdout=out) command_output = out.getvalue().splitlines()[0].strip() self.assertEqual(command_output, 'Old notifications have been deleted') self.assertEqual(user.misago_notifications.count(), 0)
def delete_warning(request, user, warning): warnings.delete_warning(request.user, user, warning) message = _("%(user)s's warning has been deleted.") message = message % {'user': user.username} messages.success(request, message) notify_user(user, _("%(user)s has deleted your warning."), reverse('misago:user_warnings', kwargs={ 'user_slug': user.slug, 'user_id': user.pk }), "warnings_%s" % user.pk, formats={'user': request.user.username}, sender=request.user)
def add_participant(request, thread, user): """ Add participant to thread, set "recound private threads" flag on user, notify user about being added to thread and mail him about it """ ThreadParticipant.objects.add_participant(thread, user) notify_user( user, _("%(user)s added you to %(thread)s private thread."), thread.get_new_reply_url(), 'see_thread_%s' % thread.pk, {'user': request.user.username, 'thread': thread.title}, request.user) set_user_unread_private_threads_sync(user)
def warn(request, user, reason=None): return_path = moderation_return_path(request, user) if warnings.is_user_warning_level_max(user): message = _("%(user)s has maximum warning " "level and can't be warned.") message = message % {'user': user.username} messages.info(request, message) return redirect(return_path) form = WarnUserForm(initial={'reason': reason}) if request.method == 'POST': form = WarnUserForm(request.POST) if form.is_valid(): warnings.warn_user(request.user, user, form.cleaned_data['reason']) notify_user(user, _("%(user)s has given you an warning."), reverse('misago:user_warnings', kwargs={ 'user_slug': user.slug, 'user_id': user.pk }), "warnings_%s" % user.pk, formats={'user': request.user.username}, sender=request.user) message = _("%(user)s has been warned.") message = message % {'user': user.username} messages.success(request, message) return redirect(return_path) warning_levels = warnings.get_warning_levels() current_level = warning_levels[user.warning_level] next_level = warning_levels[user.warning_level + 1] return render( request, 'misago/modusers/warn.html', { 'profile': user, 'form': form, 'return_path': return_path, 'current_level': current_level, 'next_level': next_level })
def moderate_signature(request, user): return_path = moderation_return_path(request, user) form = ModerateSignatureForm(instance=user) if request.method == 'POST': form = ModerateSignatureForm(request.POST, instance=user) if form.is_valid(): set_user_signature(request, user, form.cleaned_data['signature']) user.save(update_fields=( 'signature', 'signature_parsed', 'signature_checksum', 'is_signature_locked', 'signature_lock_user_message', 'signature_lock_staff_message' )) message = _("%(user)s's signature has been moderated.") message = message % {'user': user.username} messages.success(request, message) notify_user(user, _("%(user)s has moderated your signature."), reverse('misago:usercp_edit_signature'), "usercp_signature_%s" % user.pk, formats={'user': request.user.username}, sender=request.user) if 'stay' not in request.POST: return redirect(return_path) acl = user.acl editor = Editor(form['signature'], allow_blocks=acl['allow_signature_blocks'], allow_links=acl['allow_signature_links'], allow_images=acl['allow_signature_images']) return render(request, 'misago/modusers/signature.html', { 'profile': user, 'form': form, 'editor': editor, 'return_path': return_path })
def warn(request, user, reason=None): return_path = moderation_return_path(request, user) if warnings.is_user_warning_level_max(user): message = _("%(user)s has maximum warning " "level and can't be warned.") message = message % {'user': user.username} messages.info(request, message) return redirect(return_path) form = WarnUserForm(initial={'reason': reason}) if request.method == 'POST': form = WarnUserForm(request.POST) if form.is_valid(): warnings.warn_user(request.user, user, form.cleaned_data['reason']) notify_user(user, _("%(user)s has given you an warning."), reverse('misago:user_warnings', kwargs={ 'user_slug': user.slug, 'user_id': user.pk }), "warnings_%s" % user.pk, formats={'user': request.user.username}, sender=request.user) message = _("%(user)s has been warned.") message = message % {'user': user.username} messages.success(request, message) return redirect(return_path) warning_levels = warnings.get_warning_levels() current_level = warning_levels[user.warning_level] next_level = warning_levels[user.warning_level + 1] return render(request, 'misago/modusers/warn.html', { 'profile': user, 'form': form, 'return_path': return_path, 'current_level': current_level, 'next_level': next_level })
def follow_user(request, profile): user_locked = request.user.lock() profile.lock() if request.user.is_following(profile): request.user.follows.remove(profile) followed = False else: allow_follow_user(request.user, profile) followed = True request.user.follows.add(profile) if followed: message = _("You are now following %(user)s.") notify_user(profile, _("%(user)s is now following you."), reverse(user_profile.get_default_link(), kwargs={ 'user_slug': user_locked.slug, 'user_id': user_locked.id }), "profile_%s" % user_locked.pk, formats={'user': user_locked.username}, sender=user_locked, update_user=False) else: message = _("You have stopped following %(user)s.") message = message % {'user': profile.username} profile.followers = profile.followed_by.count() if followed: profile.save(update_fields=['followers', 'new_notifications']) else: profile.save(update_fields=['followers']) user_locked.following = user_locked.follows.count() user_locked.save(update_fields=['following']) if request.is_ajax: return {'is_following': followed, 'message': message} else: messages.success(request, message)
def add_participant(request, thread, user): """ Add participant to thread, set "recound private threads" flag on user, notify user about being added to thread and mail him about it """ ThreadParticipant.objects.add_participant(thread, user) notify_user(user, _("%(user)s added you to %(thread)s private thread."), thread.get_new_reply_url(), 'see_thread_%s' % thread.pk, { 'user': request.user.username, 'thread': thread.title }, request.user) set_user_unread_private_threads_sync(user) mail_subject = _("%(thread)s - %(user)s added you to private thread") subject_formats = {'thread': thread.title, 'user': request.user.username} mail_user(request, user, mail_subject % subject_formats, 'misago/emails/privatethread/added', {'thread': thread})
def moderate_avatar(request, user): return_path = moderation_return_path(request, user) avatar_locked = user.is_avatar_locked form = ModerateAvatarForm(instance=user) if request.method == 'POST': form = ModerateAvatarForm(request.POST, instance=user) if form.is_valid(): if not avatar_locked and form.cleaned_data['is_avatar_locked']: set_dynamic_avatar(user) user.save(update_fields=('is_avatar_locked', 'avatar_lock_user_message', 'avatar_lock_staff_message')) if avatar_locked != user.is_avatar_locked: if user.is_avatar_locked: message = _("%(user)s has locked your avatar.") else: message = _("%(user)s has unlocked your avatar.") notify_user(user, message, reverse('misago:usercp_change_avatar'), "usercp_avatar_%s" % user.pk, formats={'user': request.user.username}, sender=request.user) message = _("%(user)s's avatar has been moderated.") message = message % {'user': user.username} messages.success(request, message) if 'stay' not in request.POST: return redirect(return_path) return render(request, 'misago/modusers/avatar.html', { 'profile': user, 'form': form, 'return_path': return_path })
def moderate_avatar(request, user): return_path = moderation_return_path(request, user) avatar_locked = user.is_avatar_locked form = ModerateAvatarForm(instance=user) if request.method == 'POST': form = ModerateAvatarForm(request.POST, instance=user) if form.is_valid(): if not avatar_locked and form.cleaned_data['is_avatar_locked']: set_dynamic_avatar(user) user.save(update_fields=( 'is_avatar_locked', 'avatar_lock_user_message', 'avatar_lock_staff_message' )) if avatar_locked != user.is_avatar_locked: if user.is_avatar_locked: message = _("%(user)s has locked your avatar.") else: message = _("%(user)s has unlocked your avatar.") notify_user(user, message, reverse('misago:usercp_change_avatar'), "usercp_avatar_%s" % user.pk, formats={'user': request.user.username}, sender=request.user) message = _("%(user)s's avatar has been moderated.") message = message % {'user': user.username} messages.success(request, message) if 'stay' not in request.POST: return redirect(return_path) return render(request, 'misago/modusers/avatar.html', {'profile': user, 'form': form, 'return_path': return_path})
def add_participant(request, thread, user): """ Add participant to thread, set "recound private threads" flag on user, notify user about being added to thread and mail him about it """ ThreadParticipant.objects.add_participant(thread, user) notify_user( user, _("%(user)s added you to %(thread)s private thread."), thread.get_new_reply_url(), 'see_thread_%s' % thread.pk, {'user': request.user.username, 'thread': thread.title}, request.user) set_user_unread_private_threads_sync(user) mail_subject = _("%(thread)s - %(user)s added you to private thread") subject_formats = {'thread': thread.title, 'user': request.user.username} mail_user(request, user, mail_subject % subject_formats, 'misago/emails/privatethread/added', {'thread': thread})