Beispiel #1
0
def list_user_threads_view(request, targetUsername):
    ''' View of threads a user has created. '''
    targetUser = get_object_or_404(User, username=targetUsername)
    targetProfile = get_object_or_404(UserProfile, user=targetUser)
    threads = Thread.objects.filter(owner=targetProfile)
    page_name = "{0}'s Threads".format(targetUser.get_full_name())
    create_form = ThreadForm(
        request.POST if "submit_thread_form" in request.POST else None,
        profile=UserProfile.objects.get(user=request.user),
        prefix="create",
    )

    if create_form.is_valid():
        thread = create_form.save()
        return HttpResponseRedirect(
            reverse("threads:view_thread", kwargs={"pk": thread.pk}))
    elif request.method == "POST":
        messages.add_message(request, messages.ERROR, MESSAGES['THREAD_ERROR'])

    return render_to_response('list_threads.html', {
        'page_name': page_name,
        'threads': threads,
        "create_form": create_form,
        'targetUsername': targetUsername,
    },
                              context_instance=RequestContext(request))
Beispiel #2
0
def list_all_threads_view(request):
    ''' View of all threads. '''
    threads = Thread.objects.all()

    create_form = ThreadForm(
        request.POST if "submit_thread_form" in request.POST else None,
        profile=UserProfile.objects.get(user=request.user),
        )

    if create_form.is_valid():
        thread = create_form.save()
        return HttpResponseRedirect(reverse("threads:view_thread", kwargs={"thread_pk": thread.pk}))
    elif request.method == "POST":
        messages.add_message(request, messages.ERROR, MESSAGES['THREAD_ERROR'])

    return render_to_response('list_threads.html', {
        'page_name': "All Threads",
        'threads': threads,
        }, context_instance=RequestContext(request))
Beispiel #3
0
def list_all_threads_view(request):
    ''' View of all threads. '''
    threads = Thread.objects.all()

    create_form = ThreadForm(
        request.POST if "submit_thread_form" in request.POST else None,
        profile=UserProfile.objects.get(user=request.user),
    )

    if create_form.is_valid():
        thread = create_form.save()
        return HttpResponseRedirect(
            reverse("threads:view_thread", kwargs={"pk": thread.pk}))
    elif request.method == "POST":
        messages.add_message(request, messages.ERROR, MESSAGES['THREAD_ERROR'])

    return render_to_response('list_threads.html', {
        'page_name': "All Threads",
        "create_form": create_form,
        'threads': threads,
    },
                              context_instance=RequestContext(request))
Beispiel #4
0
def list_user_threads_view(request, targetUsername):
    ''' View of threads a user has created. '''
    targetUser = get_object_or_404(User, username=targetUsername)
    targetProfile = get_object_or_404(UserProfile, user=targetUser)
    threads = Thread.objects.filter(owner=targetProfile)
    page_name = "{0}'s Threads".format(targetUser.get_full_name())
    create_form = ThreadForm(
        request.POST if "submit_thread_form" in request.POST else None,
        profile=UserProfile.objects.get(user=request.user),
        prefix="create",
        )

    if create_form.is_valid():
        thread = create_form.save()
        return HttpResponseRedirect(reverse("threads:view_thread", kwargs={"thread_pk": thread.pk}))
    elif request.method == "POST":
        messages.add_message(request, messages.ERROR, MESSAGES['THREAD_ERROR'])

    return render_to_response('list_threads.html', {
        'page_name': page_name,
        'threads': threads,
        'targetUsername': targetUsername,
        }, context_instance=RequestContext(request))
def new_thread(request, subject_id):
    subject = get_object_or_404(Subject, pk=subject_id)
    poll_subject_formset = formset_factory(PollSubjectForm, extra=3)
    if request.method == "POST":
        thread_form = ThreadForm(request.POST)
        post_form = PostForm(request.POST)
        poll_form = PollForm(request.POST)
        poll_subject_formset = poll_subject_formset(request.POST)
        if thread_form.is_valid() and post_form.is_valid() and poll_form.is_valid() and poll_subject_formset.is_valid():
            thread = thread_form.save(False)
            thread.subject = subject
            thread.user = request.user
            thread.save()

            post = post_form.save(False)
            post.user = request.user
            post.thread = thread
            post.save()

            poll = poll_form.save(False)
            poll.thread = thread
            poll.save()

            for subject_form in poll_subject_formset:
                subject = subject_form.save(False)
                subject.poll = poll
                subject.save()

        messages.success(request, "You have created a new thread!")

        return redirect(reverse('thread', args={thread.pk}))

    else:
        thread_form = ThreadForm()
        post_form = PostForm(request.POST)
        poll_form = PollForm()
        poll_subject_formset = poll_subject_formset()

    args = {
        'thread_form': thread_form,
        'post_form': post_form,
        'subject': subject,
        'poll_form': poll_form,
        'poll_subject_formset': poll_subject_formset,
    }

    args.update(csrf(request))

    return render(request, 'thread_form.html', args)
Beispiel #6
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))
Beispiel #7
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))