4
from ivle.database import ProjectSet, Subject, Semester, Offering
6
from ivle.webapp.base.rest import (XHTMLRESTView, named_operation,
9
from ivle.webapp.errors import NotFound
11
class OfferingRESTView(XHTMLRESTView):
12
"""REST view for a subject.
14
This view allows for added a ProjectSet to an existing subject."""
16
template = "subject.html"
18
def __init__(self, req, subject, year, semester):
20
self.context = req.store.find(Offering,
21
Offering.subject_id == Subject.id,
22
Subject.short_name == unicode(subject),
23
Offering.semester_id == Semester.id,
24
Semester.year == unicode(year),
25
Semester.semester == unicode(semester)).one()
27
if self.context is None:
30
def new_project_url(self, projectset):
31
return "/api/subjects/%s/%s/%s/+projectsets/%d/+projects/+new" % (
32
self.context.subject.short_name, self.context.semester.year,
33
self.context.semester.semester, projectset.id)
35
@named_operation('edit')
36
def add_projectset(self, req, group_size):
37
"""Add a new ProjectSet"""
38
new_projectset = ProjectSet()
40
new_projectset.max_students_per_group = None
42
new_projectset.max_students_per_group = int(group_size)
43
new_projectset.offering = self.context
45
req.store.add(new_projectset)
48
self.ctx['projectset'] = new_projectset
49
self.ctx['projects'] = []
50
self.ctx['new_project_url'] = self.new_project_url(new_projectset)
52
self.template = 'templates/projectset_fragment.html'
54
return {'success': True, 'projectset_id': new_projectset.id}