def user_page(request, username): """ The user page has information about an user account. Also where you can change some settings (like the password). """ userModel = get_user_model() try: user = userModel.objects.get(username=username) except userModel.DoesNotExist: raise Http404("User doesn't exist.") polls = user.poll_set.all() context = { 'pageUser': user, 'unreadMessages': user.how_many_unread_messages(), 'poll_count': polls.count(), 'polls': polls[:5] } utilities.get_message(request, context) return render(request, 'accounts/user_page.html', context)
def login(request): """ Login an account. """ context = {} utilities.get_message(request, context) return django_login(request, 'accounts/login.html', extra_context=context)
def message_all(request): messages = request.user.privatemessage_set.all() context = {'messages': messages} utilities.get_message(request, context) return render(request, 'accounts/check_messages.html', context)
def home( request, pageNumber= 0 ): title = 'All polls' selected = 'all_selected' filterPolls = request.GET.get( 'filter', '' ) if request.user.is_authenticated(): if filterPolls == 'already_voted': polls = Poll.objects.filter( vote__voter= request.user ) title = 'Already voted polls' selected = 'voted_selected' elif filterPolls == 'not_voted': polls = Poll.objects.exclude( vote__voter= request.user ) title = 'Not voted polls' selected = 'not_voted_selected' else: polls = Poll.objects.all() else: polls = Poll.objects.all() pollsPerPage = settings.POLLS_PER_PAGE pageNumber = int( pageNumber ) startPoll = pageNumber * pollsPerPage pollsCount = polls.count() totalPages = math.ceil( pollsCount / pollsPerPage ) if startPoll > pollsCount: raise Http404( "Invalid page." ) polls = polls[ startPoll : startPoll + pollsPerPage ] context = { 'title': title, 'polls': polls, 'page': pageNumber, selected: True, 'pages_list': range( 0, totalPages ), 'filter': filterPolls } utilities.get_message( request, context ) return render( request, 'home.html', context )
def all_polls( request, username ): userModel = get_user_model() try: user = userModel.objects.get( username= username ) except userModel.DoesNotExist: raise Http404( "Didn't find the user." ) context = { 'pageUser': user, 'polls': user.poll_set.all() } utilities.get_message( request, context ) return render( request, 'all_polls.html', context )
def results( request, pollId ): try: poll = Poll.objects.get( id= pollId ) except Poll.DoesNotExist: raise Http404( "Invalid poll id." ) total_votes = poll.get_total_votes() has_voted = False if request.user.is_authenticated(): has_voted = poll.has_voted( request.user ) context = { 'poll': poll, 'options': poll.option_set.order_by( '-votes_count' ), 'total_votes': total_votes, 'has_voted': has_voted } utilities.get_message( request, context ) return render( request, 'results.html', context )