def addclass(request): if request.user.is_staff: if request.method == "POST": form = AddClassForm(request.POST) if form.is_valid(): cd = form.cleaned_data if ClassRoom.objects.filter(code="%s" % cd['code']): messages.add_message(request, messages.ERROR, 'There is already a class with that code.') return HttpResponseRedirect(reverse(profile,)) teacher = Teacher.objects.get(name=request.user.first_name) new_class = ClassRoom.objects.create(code=cd['code'], teacher=teacher, description=cd['description'], day=cd['day'], level=cd['level']) messages.add_message(request, messages.SUCCESS, 'Class has been successfully added.') return HttpResponseRedirect(reverse(profile,)) else: form = AddClassForm() return render_to_response('addclass.html', {'form': form}, context_instance=RequestContext(request)) else: return HttpResponseForbidden
def editclass(request, classcode): if request.user.is_staff: classroom = ClassRoom.objects.get(code=classcode) if request.method == "POST": form = AddClassForm(request.POST) if form.is_valid(): cd = form.cleaned_data if classroom.code != cd['code'] and ClassRoom.objects.filter(code="%s" % cd['code']): messages.add_message(request, messages.ERROR, 'There is another class with that code.') return HttpResponseRedirect(reverse(profile,)) classroom.code = cd['code'] classroom.description = cd['description'] classroom.level = cd['level'] classroom.day = cd['day'] classroom.save() messages.add_message(request, messages.SUCCESS, 'Class information has been successfully edited.') return HttpResponseRedirect(reverse(classprofile, args=(classcode,))) else: form = AddClassForm(initial={'code': classroom.code, 'description': classroom.description, 'day': classroom.day, 'level': classroom.level,}) return render_to_response('editclass.html', {'form': form, 'classroom': classroom,}, context_instance=RequestContext(request)) else: return HttpResponseForbidden