Beispiel #1
0
def sendMessage(request, user_id):
    """Called to create a new message
    """
    if not (request.user.is_authenticated()):
        return HttpResponseRedirect('/login/')
    else:
        currentUser = UserProfile.objects.get(user_id=request.user.id)
    if request.method == 'POST':  # If the form has been submitted...
        form = SendMessageForm(request.POST,
                               user_id)  # A form bound to the POST data
        if form.is_valid():  # All validation rules pass
            content = form.cleaned_data['content']

            #creates new tool
            try:
                AlertMessage.create(currentUser,
                                    UserProfile.objects.get(user_id=user_id),
                                    "Message", content, False, 0).save()
            except:
                return HttpResponseRedirect('ERROR')
            messages.add_message(request,
                                 messages.INFO,
                                 'Message Sent.',
                                 extra_tags='alert-success')
            return HttpResponseRedirect(
                '/tooldirectory/')  # Redirect after POST
    else:
        form = SendMessageForm()  # An unbound form

    return render(request, 'messageCenter/sendMessage.html', {
        'form': form,
        'user_id': user_id,
        'receiver': User.objects.get(id=user_id)
    })
Beispiel #2
0
def messageView(request, message_id):
    """View for reading a message
    """

    if len(AlertMessage.objects.filter(
            id=message_id)) == 0:  # Check if requested message exists

        return HttpResponseRedirect('/messagecenter/')

    msg = AlertMessage.objects.get(id=message_id)  # gets the message to view
    doesConflict = False  # sets initial value of reservation conflict to false

    if not (request.user.is_authenticated()
            ):  # Check if user is logged in properly
        return HttpResponseRedirect(
            '/login/')  # If not, send them to the logib page

    if not msg.read:  # If message is unread
        msg.read = True
        msg.save()

    existingReservations = Reservation.objects.filter(tool_id=msg.toolId)

    if request.method == 'POST':  # If reply form was submitted

        currentUser = UserProfile.objects.get(user_id=request.user.id)
        content = None
        if msg.subject == "Request":  # Checks if message being viewed is a request

            for r in existingReservations:  # Check if request conflicts with existing reservation

                if not (
                    (msg.startDate < r.startDate and msg.endDate < r.startDate)
                        or
                    (msg.startDate >= r.endDate and msg.endDate > r.endDate)):
                    doesConflict = True

            form = SendMessageForm(request.POST)
            sub = "Request Declined"

            if form.is_valid():  # All validation rules pass
                content = form.cleaned_data['content']
                content = User.objects.get(id=currentUser.user_id).username + \
                    " Has denied your request because: " + content

        else:  # Message is not a request
            form = SendMessageForm(request.POST)
            sub = "Reply"

            if form.is_valid():  # All validation rules pass
                content = form.cleaned_data['content']

        if form.is_valid():

            # creates new message
            messages.add_message(request,
                                 messages.INFO,
                                 'Reply Sent.',
                                 extra_tags='alert-success')
            AlertMessage.create(currentUser,
                                UserProfile.objects.get(id=msg.sender.id), sub,
                                content, False, 0).save()

            if AlertMessage.objects.get(id=message_id).subject != "Request":
                return HttpResponseRedirect('/messagecenter/message/' +
                                            message_id)

            return HttpResponseRedirect('/messagecenter/delete/' +
                                        str(msg.id))  # Redirect after POST

        return render(
            request, 'messageCenter/viewMessage.html', {
                'msg': msg,
                'form': form,
                'formError': True,
                'doesConflict': doesConflict
            })

    else:  # Message is being read, not replied to

        if msg.subject == "Request":  # Checks if message is a request
            form = SendMessageForm()

            for r in existingReservations:

                # Checks for reservation conflicts
                if not (
                    (msg.startDate < r.startDate and msg.endDate < r.startDate)
                        or
                    (msg.startDate >= r.endDate and msg.endDate > r.endDate)):
                    doesConflict = True

        else:
            form = SendMessageForm()  # An unbound form

    return render(request, 'messageCenter/viewMessage.html', {
        'msg': msg,
        'form': form,
        'formError': False,
        'doesConflict': doesConflict
    })