def password_changed(request): """ Inform that the password has been changed, and redirect to home. """ utilities.set_message(request, 'Password changed!') return HttpResponseRedirect(reverse('home'))
def vote_poll( request, pollId ): try: poll = Poll.objects.get( id= pollId ) except Poll.DoesNotExist: raise Http404( "Invalid poll id." ) if not poll.is_opened: utilities.set_message( request, 'Poll is closed!' ) return HttpResponseRedirect( poll.get_result_url() ) errors = [] try: Vote.objects.get( poll= poll, voter= request.user ) except Vote.DoesNotExist: if request.method == 'POST': try: optionsId = request.POST.getlist( 'options' ) except KeyError: errors.append( "Need to send the options." ) else: vote = Vote( poll= poll, voter= request.user ) vote.save() for option in optionsId: try: selectedOption = poll.option_set.get( id= option ) except Option.DoesNotExist: pass # just ignore else: selectedOption.votes_count += 1 selectedOption.save() return HttpResponseRedirect( poll.get_result_url() ) # already has voted, redirect to the results page else: return HttpResponseRedirect( poll.get_result_url() ) context = { 'poll': poll, 'errors': errors } return render( request, 'vote_poll.html', context )
def message_remove(request, messageId): """ Remove a private message. """ try: message = request.user.privatemessage_set.get(id=messageId) except PrivateMessage.DoesNotExist: raise Http404("Message doesn't exist.") message.delete() utilities.set_message(request, 'Message removed!') return HttpResponseRedirect(reverse('accounts:message_all'))
def remove_poll( request, pollId ): try: poll = request.user.poll_set.get( id= pollId ) except Poll.DoesNotExist: raise Http404( "Didn't find the poll." ) utilities.set_message( request, "'{}' poll removed!".format( poll.title ) ) poll.delete() nextUrl = request.GET.get( 'next', '/' ) return HttpResponseRedirect( nextUrl )
def remove_user(request, username): """ Remove an user account (also removes everything associated with it). """ userModel = get_user_model() try: user = userModel.objects.get(username=username) except userModel.DoesNotExist: raise Http404("User doesn't exist.") else: utilities.set_message(request, "'{}' user removed!".format(user.username)) user.delete() return HttpResponseRedirect(reverse('home'))
def open_close_poll( request, pollId ): try: poll = request.user.poll_set.get( id= pollId ) except Poll.DoesNotExist: raise Http404( "Didn't find the poll." ) poll.is_opened = not poll.is_opened poll.save( update_fields= [ 'is_opened' ] ) if poll.is_opened: message = "'{}' is now opened.".format( poll.title ) else: message = "'{}' is now closed.".format( poll.title ) utilities.set_message( request, message ) nextUrl = request.GET.get( 'next', '/' ) return HttpResponseRedirect( nextUrl )
def disable_user(request, username): """ Enable/disable an user account. If the account is disabled, the user won't be able to login. """ userModel = get_user_model() try: user = userModel.objects.get(username=username) except userModel.DoesNotExist: raise Http404("User doesn't exist.") else: value = not user.is_active # only other staff users can enable/disable staff users if user.is_staff: if request.user.is_staff: user.is_active = value user.save() else: return HttpResponseForbidden("Can't disable a staff member.") else: user.is_active = value user.save() if value: message = "'{}' account is now active.".format(user.username) else: message = "'{}' account is now disabled.".format(user.username) utilities.set_message(request, message) return HttpResponseRedirect(user.get_url())
def new_account(request): """ Create a new user account. """ if request.method == 'POST': form = MyUserCreationForm(request.POST) if form.is_valid(): form.save() utilities.set_message( request, "User '{}' created!".format(form.cleaned_data['username'])) return HttpResponseRedirect(reverse('accounts:login')) else: form = MyUserCreationForm() context = {'form': form} return render(request, 'accounts/new_account.html', context)
def message_send(request, username): """ Send a private message to another user. """ userModel = get_user_model() try: user = userModel.objects.get(username=username) except userModel.DoesNotExist: raise Http404('Invalid username.') if request.method == 'POST': form = PrivateMessageForm(request.POST) if form.is_valid(): title = form.cleaned_data['title'] content = form.cleaned_data['content'] message = PrivateMessage(receiver=user, sender=request.user, title=title, content=content) message.save() utilities.set_message(request, 'Message sent to {}!'.format(user.username)) return HttpResponseRedirect(user.get_url()) else: form = PrivateMessageForm() context = {'form': form, 'receiver': user} return render(request, 'accounts/send_message.html', context)
def set_moderator(request, username): """ Give/remove moderator rights from an account. """ userModel = get_user_model() try: user = userModel.objects.get(username=username) except userModel.DoesNotExist: raise Http404("User doesn't exist.") user.is_moderator = not user.is_moderator user.save() if user.is_moderator: message = "'{}' is now a moderator.".format(user.username) else: message = "'{}' is not a moderator anymore.".format(user.username) utilities.set_message(request, message) return HttpResponseRedirect(user.get_url())