def all_announcements_view(request): ''' The view of manager announcements. ''' page_name = "Archives - All Announcements" userProfile = UserProfile.objects.get(user=request.user) announcement_form = None manager_positions = Manager.objects.filter(incumbent=userProfile) if manager_positions: announcement_form = AnnouncementForm( request.POST if "post_announcement" in request.POST else None, profile=userProfile, ) if announcement_form.is_valid(): announcement_form.save() return HttpResponseRedirect(reverse('managers:all_announcements')) # A pseudo-dictionary, actually a list with items of form (announcement, # announcement_pin_form) announcements_dict = list() for a in Announcement.objects.all(): pin_form = None if a.manager.incumbent == userProfile or request.user.is_superuser: pin_form = PinForm( request.POST if "pin-{0}".format(a.pk) in request.POST else None, instance=a, ) if pin_form.is_valid(): pin_form.save() return HttpResponseRedirect(reverse('managers:all_announcements')) announcements_dict.append((a, pin_form)) return render_to_response('announcements.html', { 'page_name': page_name, 'manager_positions': manager_positions, 'announcements_dict': announcements_dict, 'announcement_form': announcement_form, }, context_instance=RequestContext(request))
def announcements_view(request): ''' The view of manager announcements. ''' page_name = "Manager Announcements" userProfile = UserProfile.objects.get(user=request.user) announcement_form = None manager_positions = Manager.objects.filter(incumbent=userProfile) if manager_positions: announcement_form = AnnouncementForm( request.POST if "post_announcement" in request.POST else None, profile=userProfile, ) if announcement_form and announcement_form.is_valid(): announcement_form.save(request) return HttpResponseRedirect(reverse('managers:announcements')) # A pseudo-dictionary, actually a list with items of form: # (announcement, announcement_pin_form) announcements_dict = list() for a in Announcement.objects.filter(pinned=True): pin_form = None if (a.manager.incumbent == userProfile) or request.user.is_superuser: pin_form = PinForm( request.POST if "pin-{0}".format(a.pk) else None, instance=a, ) if pin_form.is_valid(): pin_form.save() return HttpResponseRedirect(reverse('managers:announcements')) announcements_dict.append((a, pin_form)) # Oldest genesis of an pinned announcement to be displayed. within_life = now() - timedelta(hours=settings.ANNOUNCEMENT_LIFE) for a in Announcement.objects.filter(pinned=False, post_date__gte=within_life): pin_form = None if request.user.is_superuser or (a.manager.incumbent == userProfile): pin_form = PinForm( request.POST if "pin-{0}".format(a.pk) else None, instance=a, ) announcements_dict.append((a, pin_form)) return render_to_response('announcements.html', { 'page_name': page_name, 'manager_positions': manager_positions, 'announcements_dict': announcements_dict, 'announcement_form': announcement_form, }, context_instance=RequestContext(request))
def announcement_view(request, announcement_pk): ''' The view of a single manager announcement. ''' announce = get_object_or_404(Announcement, pk=announcement_pk) page_name = "View Announcement" profile = UserProfile.objects.get(user=request.user) pin_form = PinForm( request.POST if "pin" in request.POST else None, instance=announce, ) can_edit = announce.incumbent == profile or request.user.is_superuser if pin_form.is_valid(): pin_form.save() return HttpResponseRedirect( reverse('managers:view_announcement', kwargs={"announcement_pk": announcement_pk}), ) return render_to_response('view_announcement.html', { 'page_name': page_name, 'pin_form': pin_form, 'can_edit': can_edit, 'announcement': announce, }, context_instance=RequestContext(request))
def homepage_view(request, message=None): ''' The view of the homepage. ''' userProfile = UserProfile.objects.get(user=request.user) request_types = RequestType.objects.filter(enabled=True) # List of request types for which the user is a relevant manager manager_request_types = list() for request_type in request_types: for position in request_type.managers.filter(active=True): if userProfile == position.incumbent: manager_request_types.append(request_type) break # Pseudo-dictionary, list with items of form (request_type, (request, # [list_of_request_responses], response_form)) requests_dict = list() # Generate a dict of open requests for each request_type for which the user # is a relevant manager: if manager_request_types: for request_type in manager_request_types: # Items of form (request, [list_of_request_responses], # response_form, upvote, vote_form) requests_list = list() # Select only open requests of type request_type: requests = Request.objects.filter( request_type=request_type, status=Request.OPEN, ) for req in requests: response_form = ManagerResponseForm( request.POST if "add_response-{0}".format(req.pk) in request.POST else None, initial={'action': Response.NONE}, profile=userProfile, request=req, prefix="{}-response".format(req.pk), ) vote_form = VoteForm( request.POST if "vote-{0}".format(req.pk) in request.POST else None, profile=userProfile, request=req, prefix="vote", ) if response_form.is_valid(): response = response_form.save() if response.request.closed: messages.add_message(request, messages.SUCCESS, MESSAGES['REQ_CLOSED']) if response.request.filled: messages.add_message(request, messages.SUCCESS, MESSAGES['REQ_FILLED']) return HttpResponseRedirect(reverse('homepage')) if vote_form.is_valid(): vote_form.save() return HttpResponseRedirect(reverse('homepage')) response_list = Response.objects.filter(request=req) upvote = userProfile in req.upvotes.all() requests_list.append( (req, response_list, response_form, upvote, vote_form)) requests_dict.append((request_type, requests_list)) ### Announcements # Pseudo-dictionary, list with items of form (announcement, # announcement_unpin_form) announcements_dict = list() # Oldest genesis of an unpinned announcement to be displayed. within_life = now() - timedelta(hours=settings.ANNOUNCEMENT_LIFE) announcements = \ list(Announcement.objects.filter(pinned=True)) + \ list(Announcement.objects.filter(pinned=False, post_date__gte=within_life)) for a in announcements: pin_form = None if request.user.is_superuser or a.manager.incumbent == userProfile: pin_form = PinForm( request.POST if "pin-{0}".format(a.pk) in request.POST else None, instance=a, prefix="pin", ) if pin_form.is_valid(): pin_form.save() return HttpResponseRedirect(reverse('homepage')) announcements_dict.append((a, pin_form)) if Manager.objects.filter(incumbent=userProfile, active=True).count(): announcement_form = AnnouncementForm( request.POST if "post_announcement" in request.POST else None, profile=userProfile, prefix="announce", ) else: announcement_form = None if announcement_form and announcement_form.is_valid(): announcement_form.save(request) return HttpResponseRedirect(reverse('homepage')) ### Events week_from_now = now() + timedelta(days=7) # Get only next 7 days of events: events_list = Event.objects.exclude(start_time__gte=week_from_now).exclude( end_time__lte=now(), ) # Pseudo-dictionary, list with items of form (event, ongoing, rsvpd, rsvp_form) events_dict = list() for event in events_list: ongoing = ((event.start_time <= now()) and (event.end_time >= now())) rsvpd = (userProfile in event.rsvps.all()) rsvp_form = RsvpForm( request.POST if "rsvp-{0}".format(event.pk) in request.POST else None, profile=userProfile, instance=event, prefix="rsvp", ) if rsvp_form.is_valid(): rsvpd = rsvp_form.save() if not rsvpd: message = MESSAGES['RSVP_REMOVE'].format(event=event.title) messages.add_message(request, messages.SUCCESS, message) else: message = MESSAGES['RSVP_ADD'].format(event=event.title) messages.add_message(request, messages.SUCCESS, message) return HttpResponseRedirect(reverse('homepage')) events_dict.append((event, ongoing, rsvpd, rsvp_form)) ### Threads thread_form = ThreadForm( request.POST if "submit_thread_form" in request.POST else None, profile=userProfile, prefix="thread", ) if thread_form.is_valid(): thread_form.save() return HttpResponseRedirect(reverse('homepage')) # List of with items of form (thread, most_recent_message_in_thread) thread_set = [] for thread in Thread.objects.all()[:settings.HOME_MAX_THREADS]: try: latest_message = Message.objects.filter( thread=thread).latest('post_date') except Message.DoesNotExist: latest_message = None thread_set.append((thread, latest_message)) return render_to_response('homepage.html', { 'page_name': "Home", 'requests_dict': requests_dict, 'announcements_dict': announcements_dict, 'announcement_form': announcement_form, 'events_dict': events_dict, 'thread_set': thread_set, 'thread_form': thread_form, }, context_instance=RequestContext(request))