Change Assignments app according to requirements ofFinal Project

parent 4aef3fdf
from django.forms import ModelForm
from .models import Assignment, Course
class AssignmentForm(ModelForm):
class Meta:
model = Assignment
fields = ["name", "description", "max_points", "course"]
......@@ -7,6 +7,9 @@ class Course(models.Model):
course_title = models.CharField(max_length = 50)
section = models.CharField(max_length = 3)
def __str__(self):
return'{}/{}/{}'.format(self.course_code, self.course_title, self.section)
class Assignment(models.Model):
course = models.ForeignKey(Course, on_delete = models.CASCADE, default = 1)
name = models.CharField(max_length = 10)
......@@ -15,5 +18,9 @@ class Assignment(models.Model):
passing_score = models.IntegerField(default = 0)
def save(self, *args, **kwargs):
super(Assignment, self).save(*args, **kwargs)
self.passing_score = F('max_points')*0.6
super(Assignment, self).save(*args, **kwargs)
def __str__(self):
return'{}'.format(self.name)
......@@ -11,11 +11,12 @@ h3 {
p {
color: red;
font-weight: lighter;
font-weight: bold;
font-family: "Gill Sans", sans-serif;
}
body {
padding: 20px;
color: black;
background-color: orange;
font-family: "Gill Sans", sans-serif;
......
{% extends "assignments/base.html" %}
{% block content %}
<form method="POST" action="{% url 'assignments:add' %}">
{% csrf_token %}
{{assignment_form.as_p}}
<button class="button" type="submit">Save Assignment</button>
<button class="button"><a href="/assignments/"">Back</a></button>
</form>
{% endblock %}
......@@ -11,5 +11,7 @@
{% endif %}
{% endfor %}
{% endfor %}
<center>
<button class="button"><a href="{% url 'assignments:add' %}">New Assignment</a></button>
</center>
{% endblock %}
......@@ -2,8 +2,9 @@ from django.urls import path
from . import views
urlpatterns = [
path('', views.assignments, name='assignments'),
path("<int:assignment_id>/details/", views.details, name='details')
path('', views.displayAssignments.as_view(), name='assignments'),
path("<int:assignment_id>/details/", views.details, name='details'),
path("add/", views.add, name='add')
]
app_name = 'assignments'
from django.shortcuts import render
from django.shortcuts import render, redirect
from . import models
from django.http import HttpResponse
from .forms import AssignmentForm
from django.http import HttpResponse, Http404
from django.template import Template, Context
from django.template.loader import get_template
from django.views import View
# Create your views here.
def index(request):
return HttpResponse("This is the Assignments page!")
def assignments(request):
all_assignments = models.Assignment.objects.order_by("name")
all_courses = models.Course.objects.order_by("course_code")
context = {
"all_assignments": all_assignments,
"all_courses": all_courses
}
class displayAssignments(View):
template = "assignments/assignments.html"
def get(self, request):
all_assignments = models.Assignment.objects.order_by("name")
all_courses = models.Course.objects.order_by("course_code")
context = {
"all_assignments": all_assignments,
"all_courses": all_courses
}
return render(request, "assignments/assignments.html", context)
return render(request, self.template, context)
def details(request, assignment_id):
assignment = models.Assignment.objects.get(pk=assignment_id)
......@@ -25,3 +29,13 @@ def details(request, assignment_id):
"img": "images/" + str(assignment_id) + ".jpg"
}
return render(request, "assignments/details.html", context)
def add(request):
if request.method == "POST":
assignment_form = AssignmentForm(request.POST)
if assignment_form.is_valid():
new_assignment = assignment_form.save()
return redirect("assignments:assignments")
else:
assignment_form = AssignmentForm()
return render(request, "assignments/addassignments.html", {"assignment_form": assignment_form})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment