Exemplo n.º 1
0
def event_view(request, event_pk):
    event = get_object_or_404(Event, pk=event_pk)
    profile = UserProfile.objects.get(user=request.user)
    can_edit = (event.owner == profile or request.user.is_superuser)
    now = datetime.datetime.utcnow().replace(tzinfo=utc)
    ongoing = event.start_time <= now and event.end_time >= now
    rsvpd = profile in event.rsvps.all()
    rsvp_form = RsvpForm(
        request.POST if "rsvp" in request.POST else None,
        instance=event,
        profile=profile,
        )
    already_passed = event.end_time <= now
    if rsvp_form.is_valid():
        rsvpd = rsvp_form.save()
        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}),
            )

    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))
Exemplo n.º 2
0
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))
Exemplo n.º 3
0
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))
Exemplo n.º 4
0
def list_all_events_view(request):
    ''' A list view of all events.  Part of archives. '''
    page_name = "Archives - All Events"
    profile = UserProfile.objects.get(user=request.user)

    event_form = EventForm(
        request.POST if "post_event" in request.POST else None,
        profile=profile,
    )

    if event_form.is_valid():
        event_form.save()
        return HttpResponseRedirect(reverse('events:all'))

    # a pseudo-dictionary, actually a list with items of form (event, ongoing,
    # rsvpd, rsvp_form, already_past), where ongoing is a boolean of whether the
    # event is currently ongoing, rsvpd is a boolean of whether the user has
    # rsvp'd to the event
    events_dict = list()
    for event in Event.objects.all():
        rsvp_form = None
        if event.end_time > now():
            rsvp_form = RsvpForm(
                request.POST
                if "rsvp-{0}".format(event.pk) in request.POST else None,
                instance=event,
                profile=profile,
            )
            if rsvp_form.is_valid():
                rsvpd = rsvp_form.save()
                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:all'))

        ongoing = event.start_time <= now() and event.end_time >= now()
        rsvpd = profile in event.rsvps.all()
        events_dict.append((event, ongoing, rsvpd, rsvp_form))

    if request.method == "POST":
        messages.add_message(request, messages.ERROR, MESSAGES["EVENT_ERROR"])

    return render_to_response('list_events.html', {
        'page_name': page_name,
        'events_dict': events_dict,
        'now': now(),
        'event_form': event_form,
    },
                              context_instance=RequestContext(request))
Exemplo n.º 5
0
def list_all_events_view(request):
    ''' A list view of all events.  Part of archives. '''
    page_name = "Archives - All Events"
    profile = UserProfile.objects.get(user=request.user)

    event_form = EventForm(
        request.POST if "post_event" in request.POST else None,
        profile=profile,
        )

    if event_form.is_valid():
        event_form.save()
        return HttpResponseRedirect(reverse('events:all'))

    # a pseudo-dictionary, actually a list with items of form (event, ongoing,
    # rsvpd, rsvp_form, already_past), where ongoing is a boolean of whether the
    # event is currently ongoing, rsvpd is a boolean of whether the user has
    # rsvp'd to the event
    events_dict = list()
    for event in Event.objects.all():
        rsvp_form = None
        if event.end_time > now():
            rsvp_form = RsvpForm(
                request.POST if "rsvp-{0}".format(event.pk) in request.POST else None,
                instance=event,
                profile=profile,
                )
            if rsvp_form.is_valid():
                rsvpd = rsvp_form.save()
                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:all'))

        ongoing = event.start_time <= now() and event.end_time >= now()
        rsvpd = profile in event.rsvps.all()
        events_dict.append(
            (event, ongoing, rsvpd, rsvp_form)
            )

    if request.method == "POST":
        messages.add_message(request, messages.ERROR, MESSAGES["EVENT_ERROR"])

    return render_to_response('list_events.html', {
        'page_name': page_name,
        'events_dict': events_dict,
        'now': now(),
        'event_form': event_form,
        }, context_instance=RequestContext(request))
Exemplo n.º 6
0
class RsvpView(View):
    template_name = 'events/rsvp.html'

    def __init__(self, request, *args, **kwargs):
        super(RsvpView, self).__init__(request, *args, **kwargs)
        self.user = self.request.user
        self.event = kwargs.pop('event')

    def post(self, request, *args, **kwargs):
        self.rsvp_form = RsvpForm(request.POST)
        self.rsvp_form.save()
        return redirect('event-dashboard')

    def get(self, request, *args, **kwargs):
        attendance = Attendance.objects.create(user=self.user,
                                               event=self.event,
                                               attending=True)
        attendance.save()
        return redirect('event-dashboard')
Exemplo n.º 7
0
def list_events_view(request):
    ''' A list view of upcoming events. '''
    page_name = "Upcoming Events"
    profile = UserProfile.objects.get(user=request.user)
    event_form = EventForm(
        request.POST if 'post_event' in request.POST else None,
        profile=profile,
        )
    now = datetime.datetime.utcnow().replace(tzinfo=utc)
    if event_form.is_valid():
        event_form.save()
        return HttpResponseRedirect(reverse('events:list'))
    # a pseudo-dictionary, actually a list with items of form (event, ongoing,
    # rsvpd, rsvp_form), where ongoing is a boolean of whether the event is
    # currently ongoing, rsvpd is a boolean of whether the user has rsvp'd to
    # the event
    events_dict = list()
    for event in Event.objects.filter(end_time__gte=now):
        rsvp_form = RsvpForm(
            request.POST if "rsvp-{0}".format(event.pk) in request.POST else None,
            instance=event,
            profile=profile,
            )
        if rsvp_form.is_valid():
            rsvpd = rsvp_form.save()
            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:list'))

        ongoing = ((event.start_time <= now) and (event.end_time >= now))
        rsvpd = (profile in event.rsvps.all())
        events_dict.append((event, ongoing, rsvpd, rsvp_form))

    return render_to_response('list_events.html', {
        'page_name': page_name,
        'events_dict': events_dict,
        'now': now,
        'event_form': event_form,
        }, context_instance=RequestContext(request))
Exemplo n.º 8
0
    def post(self, request, *args, **kwargs):
        self.message_form = EventMessageForm()
        self.rsvp_form = RsvpForm()
        if self.message_form.is_valid():
            data = self.message_form.cleaned_data
            message = Message.create_message(
                user=data['user'],
                event=data['event'],
                text=data['text'],
            )
            message.save()
            return redirect('event-detail', slug=event.slug)

        if self.rsvp_form.is_valid():
            rdata = self.rsvp_form.cleaned_data
            attendee = Attendance.objects.create(user=rdata['user'],
                                                 event=rdata['event'],
                                                 attending=rd['attending'])
            attendee.save()

        return self.get(request, *args, **kwargs)
Exemplo n.º 9
0
    def test_rsvp_already_past(self):
        self.ev.end_time = now() - timedelta(days=1)
        self.ev.save()

        form = RsvpForm(
            QueryDict(""),
            profile=self.profile,
            instance=self.ev,
        )
        self.assertRaises(
            forms.ValidationError,
            form.clean,
        )
Exemplo n.º 10
0
class DetailEventView(TemplateView):

    template_name = 'events/detail.html'
    model = Event

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        self.user = self.request.user
        self.event = get_object_or_404(Event, slug=self.kwargs['slug'])
        return super(DetailEventView, self).dispatch(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        self.message_form = EventMessageForm()
        self.rsvp_form = RsvpForm()
        if self.message_form.is_valid():
            data = self.message_form.cleaned_data
            message = Message.create_message(
                user=data['user'],
                event=data['event'],
                text=data['text'],
            )
            message.save()
            return redirect('event-detail', slug=event.slug)

        if self.rsvp_form.is_valid():
            rdata = self.rsvp_form.cleaned_data
            attendee = Attendance.objects.create(user=rdata['user'],
                                                 event=rdata['event'],
                                                 attending=rd['attending'])
            attendee.save()

        return self.get(request, *args, **kwargs)

    def get_context_data(self, *args, **kwargs):
        context = super(DetailEventView,
                        self).get_context_data(*args, **kwargs)
        context['message_form'] = EventMessageForm(event=self.event,
                                                   user=self.user)
        context['event'] = self.event
        try:
            guests = Attendance.objects.filter(event=self.event)
            context['guests'] = guests
        except:
            self.guests = None
        messages = Message.objects.filter(event=self.event)
        context['messages'] = messages
        context['user'] = self.user

        return context
Exemplo n.º 11
0
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,
                    )
                vote_form = VoteForm(
                    request.POST if "vote-{0}".format(req.pk) in request.POST else None,
                    profile=userProfile,
                    request=req,
                    )

                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.
    now = datetime.utcnow().replace(tzinfo=utc)
    within_life = now - timedelta(days=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,
                )
            if pin_form.is_valid():
                pin_form.save()
                return HttpResponseRedirect(reverse('homepage'))
        announcements_dict.append((a, pin_form))

    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('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, 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,
            )

        if rsvp_form.is_valid():
            rsvpd = rsvp_form.save()
            if 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,
        )
    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))
Exemplo n.º 12
0
 def post(self, request, *args, **kwargs):
     self.rsvp_form = RsvpForm(request.POST)
     self.rsvp_form.save()
     return redirect('event-dashboard')
Exemplo n.º 13
0
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))