def event_view(request, event_pk): if not request.user.is_authenticated(): return HttpResponse(json.dumps(dict()), content_type="application/json") event = get_object_or_404(Event, pk=event_pk) profile = UserProfile.objects.get(user=request.user) rsvp_form = RsvpForm( request.POST if "rsvp" in request.POST else None, instance=event, profile=profile, ) if rsvp_form.is_valid(): rsvpd = rsvp_form.save() if request.is_ajax(): link_string = 'rsvp_link_{pk}'.format(pk=event.pk) list_string = 'rsvp_list_{pk}'.format(pk=event.pk) response = dict() response[link_string], response[list_string] = build_ajax_rsvps( event, profile ) return HttpResponse(json.dumps(response), content_type="application/json") if rsvpd: message = MESSAGES['RSVP_ADD'].format(event=event.title) else: message = MESSAGES['RSVP_REMOVE'].format(event=event.title) messages.add_message(request, messages.SUCCESS, message) return HttpResponseRedirect( reverse('events:view', kwargs={"event_pk": event_pk}), ) elif request.is_ajax(): raise Http404 if request.method == "POST": messages.add_message(request, messages.ERROR, MESSAGES["EVENT_ERROR"]) already_passed = event.end_time <= now() can_edit = event.owner == profile or request.user.is_superuser ongoing = event.start_time <= now() and event.end_time >= now() rsvpd = profile in event.rsvps.all() return render_to_response('view_event.html', { 'page_name': event.title, 'can_edit': can_edit, 'event': event, 'ongoing': ongoing, 'rsvp_form': rsvp_form, 'rsvpd': rsvpd, 'already_passed': already_passed, }, context_instance=RequestContext(request))
def event_view(request, event_pk): if not request.user.is_authenticated(): return HttpResponse(json.dumps(dict()), content_type="application/json") event = get_object_or_404(Event, pk=event_pk) profile = UserProfile.objects.get(user=request.user) rsvp_form = RsvpForm( request.POST if "rsvp" in request.POST else None, instance=event, profile=profile, ) if rsvp_form.is_valid(): rsvpd = rsvp_form.save() if request.is_ajax(): link_string = 'rsvp_link_{pk}'.format(pk=event.pk) list_string = 'rsvp_list_{pk}'.format(pk=event.pk) response = dict() response[link_string], response[list_string] = build_ajax_rsvps( event, profile) return HttpResponse(json.dumps(response), content_type="application/json") if rsvpd: message = MESSAGES['RSVP_ADD'].format(event=event.title) else: message = MESSAGES['RSVP_REMOVE'].format(event=event.title) messages.add_message(request, messages.SUCCESS, message) return HttpResponseRedirect( reverse('events:view', kwargs={"event_pk": event_pk}), ) elif request.is_ajax(): raise Http404 if request.method == "POST": messages.add_message(request, messages.ERROR, MESSAGES["EVENT_ERROR"]) already_passed = event.end_time <= now() can_edit = event.owner == profile or request.user.is_superuser ongoing = event.start_time <= now() and event.end_time >= now() rsvpd = profile in event.rsvps.all() return render_to_response('view_event.html', { 'page_name': event.title, 'can_edit': can_edit, 'event': event, 'ongoing': ongoing, 'rsvp_form': rsvp_form, 'rsvpd': rsvpd, 'already_passed': already_passed, }, context_instance=RequestContext(request))
def get_updates_view(request): """Return a user's updates. AJAX.""" if not request.is_ajax(): raise Http404 if not request.user.is_authenticated(): return HttpResponse(json.dumps(dict()), content_type="application/json") try: user_profile = UserProfile.objects.get(user=request.user) except UserProfile.DoesNotExist: return HttpResponse(json.dumps(dict()), content_type="application/json") response = dict() if request.user.is_superuser: num_of_profile_requests = ProfileRequest.objects.all().count() if num_of_profile_requests == 0: response[ "profile_requests_link" ] = """ <span class="glyphicon glyphicon-inbox"></span> Profile Requests """ else: response[ "profile_requests_link" ] = """ <span title="{req_num} open profile request{mult}" class="badge pull-right">{req_num}</span> <span class="glyphicon glyphicon-inbox"></span> Profile Requests """.format( req_num=num_of_profile_requests, mult="s" if num_of_profile_requests > 1 else "" ) notification_count = request.user.notifications.unread().count() if notification_count == 0: response[ "notifications_link" ] = """ <span class="glyphicon glyphicon-bell"></span> Notifications """ response[ "profile_dropdown_link" ] = """ <span><span class="pull-right"><b class="caret"></b></span> <span class="glyphicon glyphicon-user"></span> {first_name} </span> """.format( first_name=request.user.first_name ) else: response[ "notifications_link" ] = """ <span title="{n_num} unread notification{mult}" class="badge pull-right">{n_num}</span> <span class="glyphicon glyphicon-bell"></span> Notifications """.format( n_num=notification_count, mult="s" if notification_count > 1 else "" ) response[ "profile_dropdown_link" ] = """ <span><span class="pull-right"><b class="caret"></b></span><span title="You have {n_num} unread notification{mult}." class="badge pull-right">{n_num}</span> <span class="glyphicon glyphicon-user"></span> {first_name} </span> """.format( n_num=notification_count, mult="s" if notification_count > 1 else "", first_name=request.user.first_name ) req_dict = dict() for req_type in RequestType.objects.filter(enabled=True): open_reqs = Request.objects.filter(request_type=req_type, status=Request.OPEN) if not req_type.managers.filter(incumbent__user=request.user): open_reqs = open_reqs.exclude(~Q(owner__user=request.user), private=True) num_open = open_reqs.count() if num_open == 0: req_dict[ "{rtype}_requests_link".format(rtype=req_type.url_name) ] = """ <span class="glyphicon glyphicon-{icon}"></span> {name} """.format( icon=req_type.glyphicon if req_type.glyphicon else "inbox", name=req_type.name ) else: req_dict[ "{rtype}_requests_link".format(rtype=req_type.url_name) ] = """ <span title="{num} open request{mult}" class="badge pull-right">{num}</span> <span class="glyphicon glyphicon-{icon}"></span> {name} """.format( num=num_open, mult="s" if num_open > 1 else "", icon=req_type.glyphicon if req_type.glyphicon else "inbox", name=req_type.name, ) if req_dict.keys(): response["requests_dict"] = req_dict request_pk_list = request.GET.get("request_pk_list", False) if request_pk_list: request_pk_list = request_pk_list.split(",") for request_pk in request_pk_list: try: req = Request.objects.get(pk=request_pk) except Request.DoesNotExist: continue response["vote_count_{pk}".format(pk=req.pk)] = req.upvotes.all().count() list_string = "vote_list_{pk}".format(pk=request_pk) vote_string = "in_votes_{pk}".format(pk=request_pk) count_string = "vote_count_{pk}".format(pk=request_pk) response[list_string], response[vote_string], response[count_string] = build_ajax_votes(req, user_profile) event_pk_list = request.GET.get("event_pk_list", False) if event_pk_list: event_pk_list = event_pk_list.split(",") for event_pk in event_pk_list: try: event = Event.objects.get(pk=event_pk) except Event.DoesNotExist: continue link_string = "rsvp_link_{pk}".format(pk=event.pk) list_string = "rsvp_list_{pk}".format(pk=event.pk) response[link_string], response[list_string] = build_ajax_rsvps(event, user_profile) thread_pk = request.GET.get("thread_pk", False) if thread_pk: try: thread = Thread.objects.get(pk=thread_pk) except Thread.DoesNotExist: pass else: response["following"] = user_profile in thread.followers.all() response["num_of_followers"] = thread.followers.all().count() return HttpResponse(json.dumps(response), content_type="application/json")
def get_updates_view(request): """Return a user's updates. AJAX.""" if not request.is_ajax(): raise Http404 if not request.user.is_authenticated(): return HttpResponse(json.dumps(dict()), content_type="application/json") try: user_profile = UserProfile.objects.get(user=request.user) except UserProfile.DoesNotExist: return HttpResponse(json.dumps(dict()), content_type="application/json") response = dict() if request.user.is_superuser: num_of_profile_requests = ProfileRequest.objects.all().count() if num_of_profile_requests == 0: response['profile_requests_link'] = ''' <span class="glyphicon glyphicon-inbox"></span> Profile Requests ''' else: response['profile_requests_link'] = """ <span title="{req_num} open profile request{mult}" class="badge pull-right">{req_num}</span> <span class="glyphicon glyphicon-inbox"></span> Profile Requests """.format( req_num=num_of_profile_requests, mult='s' if num_of_profile_requests > 1 else '', ) notification_count = request.user.notifications.unread().count() if notification_count == 0: response['notifications_link'] = ''' <span class="glyphicon glyphicon-bell"></span> Notifications ''' response['profile_dropdown_link'] = ''' <span><span class="pull-right"><b class="caret"></b></span> <span class="glyphicon glyphicon-user"></span> {first_name} </span> '''.format(first_name=request.user.first_name) else: response['notifications_link'] = """ <span title="{n_num} unread notification{mult}" class="badge pull-right">{n_num}</span> <span class="glyphicon glyphicon-bell"></span> Notifications """.format( n_num=notification_count, mult='s' if notification_count > 1 else '', ) response['profile_dropdown_link'] = ''' <span><span class="pull-right"><b class="caret"></b></span><span title="You have {n_num} unread notification{mult}." class="badge pull-right">{n_num}</span> <span class="glyphicon glyphicon-user"></span> {first_name} </span> '''.format( n_num=notification_count, mult='s' if notification_count > 1 else '', first_name=request.user.first_name, ) req_dict = dict() for req_type in RequestType.objects.filter(enabled=True): open_reqs = Request.objects.filter(request_type=req_type, status=Request.OPEN) if not req_type.managers.filter(incumbent__user=request.user): open_reqs = open_reqs.exclude( ~Q(owner__user=request.user), private=True, ) num_open = open_reqs.count() if num_open == 0: req_dict['{rtype}_requests_link'.format(rtype=req_type.url_name)] \ = """ <span class="glyphicon glyphicon-{icon}"></span> {name} """.format( icon=req_type.glyphicon if req_type.glyphicon else 'inbox', name=req_type.name ) else: req_dict['{rtype}_requests_link'.format(rtype=req_type.url_name)] \ = """ <span title="{num} open request{mult}" class="badge pull-right">{num}</span> <span class="glyphicon glyphicon-{icon}"></span> {name} """.format( num=num_open, mult='s' if num_open > 1 else '', icon=req_type.glyphicon if req_type.glyphicon else 'inbox', name=req_type.name, ) if req_dict.keys(): response['requests_dict'] = req_dict request_pk_list = request.GET.get('request_pk_list', False) if request_pk_list: request_pk_list = request_pk_list.split(',') for request_pk in request_pk_list: try: req = Request.objects.get(pk=request_pk) except Request.DoesNotExist: continue response['vote_count_{pk}'.format( pk=req.pk)] = req.upvotes.all().count() list_string = 'vote_list_{pk}'.format(pk=request_pk) vote_string = 'in_votes_{pk}'.format(pk=request_pk) count_string = 'vote_count_{pk}'.format(pk=request_pk) response[list_string], response[vote_string], \ response[count_string] = build_ajax_votes( req, user_profile ) event_pk_list = request.GET.get('event_pk_list', False) if event_pk_list: event_pk_list = event_pk_list.split(',') for event_pk in event_pk_list: try: event = Event.objects.get(pk=event_pk) except Event.DoesNotExist: continue link_string = 'rsvp_link_{pk}'.format(pk=event.pk) list_string = 'rsvp_list_{pk}'.format(pk=event.pk) response[link_string], response[list_string] = build_ajax_rsvps( event, user_profile) thread_pk = request.GET.get('thread_pk', False) if thread_pk: try: thread = Thread.objects.get(pk=thread_pk) except Thread.DoesNotExist: pass else: response['following'] = user_profile in thread.followers.all() response['num_of_followers'] = thread.followers.all().count() return HttpResponse(json.dumps(response), content_type="application/json")