Skip to content
Snippets Groups Projects
Commit 7f4c3994 authored by Odin Johan Vatne's avatar Odin Johan Vatne
Browse files

Added project create/edit pages

parent f052eb62
No related branches found
No related tags found
No related merge requests found
from django import forms from django import forms
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import Group, User from django.contrib.auth.models import Group, User
...@@ -23,3 +23,8 @@ class NewUserForm(UserCreationForm): ...@@ -23,3 +23,8 @@ class NewUserForm(UserCreationForm):
user.groups.add(Group.objects.get(name=user_group)) user.groups.add(Group.objects.get(name=user_group))
return user return user
class NewProjectForm(forms.Form):
title = forms.CharField(label="Title", max_length=200)
description = forms.CharField(label="Description", max_length=2048)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Create Project | PAS</title>
</head>
<body>
<form action="/project/create/" method="post">
{% csrf_token %} {{ form }}
<input type="submit" value="Create Project" />
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Edit Project | PAS</title>
</head>
<body>
<form action="/project/{{project.id}}/edit/" method="post">
{% csrf_token %} {{ form }}
<input type="submit" value="Update Project" />
</form>
</body>
</html>
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
<body> <body>
<h1>{{project.title}}</h1> <h1>{{project.title}}</h1>
<h3>{{project.professor}}</h3> <h3>{{project.professor}}</h3>
{% if project.professor.id == user.id %}
<a href="/project/{{project.id}}/edit">Edit this project</a>
{% endif %}
<p>{{project.description}}</p> <p>{{project.description}}</p>
<em>Status: </em>{{project.status}} <em>Status: </em>{{project.status}}
<br /> <br />
......
...@@ -12,4 +12,7 @@ urlpatterns = [ ...@@ -12,4 +12,7 @@ urlpatterns = [
path('apply/<int:project_id>/', views.apply, name='apply'), path('apply/<int:project_id>/', views.apply, name='apply'),
path('project/<int:project_id>/applications', path('project/<int:project_id>/applications',
views.project_applications, name='project_applications'), views.project_applications, name='project_applications'),
path('project/create/', views.create_project, name='create_project'),
path('project/<int:project_id>/edit/',
views.edit_project, name='edit_project'),
] ]
def is_professor(user):
return user.groups.filter(name='professor').exists()
def is_student(user):
return user.groups.filter(name='student').exists()
from django.contrib.auth import authenticate, login, logout from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib.auth.forms import AuthenticationForm from django.contrib.auth.forms import AuthenticationForm
from django.core.checks import messages from django.core.checks import messages
from django.http.response import Http404, HttpResponseForbidden, HttpResponseRedirect from django.http.response import Http404, HttpResponseForbidden, HttpResponseRedirect
from django.shortcuts import get_object_or_404, redirect, render from django.shortcuts import get_object_or_404, redirect, render
from django.http import HttpResponse from django.http import HttpResponse
from pasapp.forms import NewUserForm from pasapp.forms import NewProjectForm, NewUserForm
from pasapp.models import Application, Project from pasapp.models import Application, Project
from pasapp.utils import is_professor
# Create your views here. # Create your views here.
...@@ -20,7 +21,8 @@ def index(request): ...@@ -20,7 +21,8 @@ def index(request):
def project_details(request, project_id): def project_details(request, project_id):
project = get_object_or_404(Project, pk=project_id) project = get_object_or_404(Project, pk=project_id)
numApplicants = Application.objects.filter(project=project_id).count() numApplicants = Application.objects.filter(project=project_id).count()
context = {'project': project, 'numApplicants': numApplicants} context = {'user': request.user, 'project': project,
'numApplicants': numApplicants}
return render(request, 'pasapp/project_details.html', context) return render(request, 'pasapp/project_details.html', context)
...@@ -48,6 +50,50 @@ def project_applications(request, project_id): ...@@ -48,6 +50,50 @@ def project_applications(request, project_id):
return render(request, 'pasapp/project_applications.html', context) return render(request, 'pasapp/project_applications.html', context)
@login_required
@user_passes_test(is_professor)
def create_project(request):
if request.method == 'POST':
form = NewProjectForm(request.POST)
if form.is_valid():
title = form.cleaned_data.get('title')
description = form.cleaned_data.get('description')
status = 'Hidden'
professor = request.user
project = Project(title=title, description=description,
status=status, professor=professor)
project.save()
return redirect("index")
else:
form = NewProjectForm()
context = {'form': form}
return render(request, 'pasapp/create_project.html', context)
@login_required
@user_passes_test(is_professor)
def edit_project(request, project_id):
project = get_object_or_404(Project, pk=project_id)
if not request.user.id == project.professor.id:
return HttpResponseForbidden()
if request.method == 'POST':
form = NewProjectForm(request.POST)
if form.is_valid():
title = form.cleaned_data.get('title')
description = form.cleaned_data.get('description')
project.title = title
project.description = description
project.save()
redirect("index")
else:
form = NewProjectForm(
initial={'title': project.title, 'description': project.description})
context = {'form': form, 'project': project}
return render(request, 'pasapp/edit_project.html', context)
def register(request): def register(request):
if request.method == 'POST': if request.method == 'POST':
form = NewUserForm(request.POST) form = NewUserForm(request.POST)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment