Exemplo n.º 1
0
def login_view(request):
    """
    Endpoint for logins.
    Logged in users will be redirected to their homepage.
    If login fails, they will be redirected to /login to try again.
    Successful login will authenticate them and send them to their homepage
    """
    if request.user.is_authenticated():
        return redirect_user_to_homepage(request.user.profile.user_type)

    if request.method == "POST":  # User attempting to log in
        form = LoginForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data["email"]
            password = form.cleaned_data["password"]

            user = authenticate(username=email, password=password)
            if user is not None:
                # User was successfully authenticated, redirect them to their home page
                login(request, user)
                return redirect_user_to_homepage(user.profile.user_type)

        # Reject the login and notify that the email / password was wrong
        blank_form = LoginForm()
        return render(request, "login.html", {
            "invalid": True,
            "form": blank_form
        })
    else:  # GET request
        form = LoginForm()
        return render(request, "login.html", {"form": form})
Exemplo n.º 2
0
def register(request):
    """
    Endpoint for new users registering.
    Authenticated users will just be redirected to their homepage.
    If registration fails, the user is redirected to /register and an error appears
    """
    if request.user.is_authenticated():
        return redirect_user_to_homepage(request.user.profile.user_type)

    blank_form = RegisterForm()

    if request.method == "POST":
        form = RegisterForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data["email"]
            password = form.cleaned_data["password"]
            user_type = form.cleaned_data["user_type"]

            currKey = InstructorKey.objects.filter(key = form.cleaned_data["key"]).first()

            if user_type == "I" and currKey is None:
                messages.add_message(request, messages.INFO, 'The Instructor key was incorrect')
                return render(request, "register.html", { "form": form })

            try: # Try and create the new user object
                new_user = User.objects.create_user(email,
                                                    email=email,
                                                    password=password)
                new_user.profile.user_type = user_type
                if user_type == 'I':
                    currKey.delete()

                new_user.save() # save the new user to the database
            except IntegrityError:
                # Duplicate email: notify the user and bail on registering
                return render(request, "register.html", { "duplicate_email": True, "form": blank_form })


            user = authenticate(username=email, password=password)
            login(request, user)

            # Create an introduction notification to display to the new user
            create_introduction_notification(user)

            if user_type == 'S':
                # Allow instructors to join/create a section, students to join a section
                return HttpResponseRedirect("/joinsection/")
            else:
                return redirect_user_to_homepage(user_type)
        else:
            # The form data was bad, display an error
            return render(request, "register.html", { "invalid": True, "form": blank_form })
    else:
        # The user did not try and register, and just needs to see the register form
        return render(request, "register.html", { "form": blank_form })
Exemplo n.º 3
0
def add_tag(request):
    """
    Adds tags to student's profile
    """
    if request.method == 'POST':
        form = ProfileForm(request.POST)
        if form.is_valid():
            tags = form.cleaned_data["tags"]
            tags = [x.strip() for x in tags.split(',')]
            for tag in tags:
                #TODO: tags comma separated
                new_tag = Tag(name=tag)
                try:
                    Tag.objects.get(name=tag)
                    new_tag = Tag.objects.get(name=tag)
                    messages.add_message(request, messages.INFO,
                                         'You cannot add the same tags!')
                    return HttpResponseRedirect("/profile/")
                except ObjectDoesNotExist:
                    new_tag.save()
                new_tag.students.add(request.user)
            return redirect_user_to_homepage(request.user.profile.user_type)
        else:
            # The form data was bad, display an error
            return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
Exemplo n.º 4
0
def reply_to_question(request, question_id):
    question = Question.objects.get(id=int(question_id))
    form = ReplyForm(request.POST)
    if form.is_valid():
        question.reply = form.cleaned_data["text"]
        question.save()
    return redirect_user_to_homepage(request.user.profile.user_type)
Exemplo n.º 5
0
def join_a_section(request, section_id):
    """
    Render the page where instructors can go to make a new section and
    students can go to join a section
    """
    # Join section
    if section_id != "":
        # If User already belongs to a section, remove them from the original
        if request.user.profile.section != None:
            old_section = Section.objects.get(
                id=int(request.user.profile.section.id))
            if request.user.profile.user_type == 'S':
                old_section.students.remove(request.user.id)
            else:
                assert False, "Invalid user type"
        section = Section.objects.get(id=int(section_id))
        if request.user.profile.user_type == 'S':
            section.students.add(request.user)
            request.user.profile.section = section
            request.user.profile.save()
        else:
            assert False, "Invalid user type"
        return redirect_user_to_homepage(request.user.profile.user_type)
    sections = Section.objects.all()

    context = {"sections": sections}
    return render(request, "joinsection.html", context)
Exemplo n.º 6
0
def award_bid(request, bid_id):
    bid = Bid.objects.get(id=int(bid_id))
    new_notification = Notification(recipient=bid.student, subject="Bid on {} Awarded!".format(bid.project.name),
                                    text="Your bid on the project {} was awarded by your instructor(s)!" \
                                         "This will be your project.  Contact your client at {}".format(bid.project.name, bid.project.client.email))
    new_notification.save()
    bid.delete()
    return redirect_user_to_homepage(request.user.profile.user_type)
Exemplo n.º 7
0
def reject_bid(request, bid_id):
    bid = Bid.objects.get(id=int(bid_id))
    new_notification = Notification(recipient=bid.student, subject="Bid on {} Rejected".format(bid.project.name),
                                    text="Your bid on the project {} was rejected by your instructor(s)." \
                                         "Please continue browsing and submitting more bids.")
    new_notification.save()
    bid.delete()
    return redirect_user_to_homepage(request.user.profile.user_type)
Exemplo n.º 8
0
def delete_a_section(request, section_id):
    """
    Deletes a section if it is not the last section
    """
    if section_id != "":
        sections = Section.objects
        if sections.count() != 1:
            if sections.get(id=int(section_id)).students.count() == 0:
                sections.get(id=int(section_id)).delete()

    return redirect_user_to_homepage(request.user.profile.user_type)
Exemplo n.º 9
0
def index(request):
    """
    Authenticated users will be sent to their home pages,
    unauthenticated users will be sent to the Login / Register home screen
    """
    if request.user.is_authenticated():
        if request.user.is_superuser:
            return HttpResponseRedirect("/admin/")
        return redirect_user_to_homepage(request.user.profile.user_type)
    else:
        return render(request, "index.html")
Exemplo n.º 10
0
def reject_project(request, project_id):
    proj = Project.objects.get(id=int(project_id))
    new_notification = Notification(
        recipient=proj.client,
        subject="Project {} Rejected".format(proj.name),
        text=
        "The instructor decided that your project submission was not right for the"
        "scope of the class and has decided not to allow students to bid on it."
    )
    new_notification.save()
    proj.delete()
    return redirect_user_to_homepage(request.user.profile.user_type)
Exemplo n.º 11
0
def send_message(request, message_id):
    """
    Render page where students / instructors can go to send a message to other users
    """
    if (message_id != ''):
        message = Message.objects.get(id=message_id)
        form_init = {
            'recipient': message.sender,
            'subject': 'RE: ' + message.subject,
            'text': message.reply_text
        }
    else:
        form_init = {}

    if request.method == "POST":
        form = MessageForm(request.POST)
        if form.is_valid():
            sender = request.user
            recipient = User.objects.get(email=form.data["recipient"])
            subject = form.data["subject"]
            text = form.data["text"].split('\n     >')[0]

            old_reply_text = message.reply_text if message_id != '' else ''
            reply_text = '\n\n'
            reply_lines = ('From: ' + sender.email + '\n' + text +
                           old_reply_text).split('\n')
            for line in reply_lines:
                reply_text = reply_text + '     > ' + line + '\n'

            new_message = Message(sender=sender,
                                  recipient=recipient,
                                  subject=subject,
                                  text=text,
                                  reply_text=reply_text)

            new_message.save()
            return redirect_user_to_homepage(request.user.profile.user_type)
    form = MessageForm(initial=form_init)
    context = {
        "form": form,
    }
    return render(request, "sendmessage.html", context)
Exemplo n.º 12
0
def edit_a_section(request, section_id):
    """
    Render the page where instructors can go to make a new section and
    students can go to join a section
    """
    if section_id != "":
        # User is choosing to edit the section's name
        form = NewSectionForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data["name"]
            section = Section.objects.get(id=int(section_id))
            setattr(section, "name", name)
            section.save()
            return redirect_user_to_homepage(request.user.profile.user_type)

    form = NewSectionForm()
    sections = Section.objects.all()
    context = {
            "form": form,
            "section_id": section_id
    }
    return render(request, "editsection.html", context)
Exemplo n.º 13
0
def manage_sections(request):
    """
    Render the page where instructors can go to make a new section or
    edit existing ones.
    """

    if request.method == "POST":
        # Class is being submitted
        form = NewSectionForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data["name"]
            new_section = Section(name=name)
            new_section.save()
            return redirect_user_to_homepage(request.user.profile.user_type)

    form = NewSectionForm()
    sections = Section.objects.all()
    context = {
            "form": form,
            "sections": sections,
            "listSize": sections.count()
    }
    return render(request, "managesection.html", context)
Exemplo n.º 14
0
def approve_project(request, project_id):
    proj = Project.objects.get(id=int(project_id))
    proj.is_approved = True
    proj.save()
    return redirect_user_to_homepage(request.user.profile.user_type)