Exemple #1
0
def create(request, department, number, page_type):
	if not request.user.is_authenticated():
		return register(request)

	course = get_object_or_404(Course, department=department, number=int(number))

	if page_type not in page_types:
		raise Http404

	page_type_obj = page_types[page_type]
	data = {
		'course': course,
		'page_type': page_type_obj,
		'terms': terms,
		'field_templates': page_type_obj.get_uneditable_fields() + page_type_obj.get_editable_fields(),
		'years': years,
		'current_term': current_term,
		'current_year': current_year,
		'exam_types': exam_types,
		'current_exam_type': exam_types[0], # default
		'edit_mode': False,
	}
	if request.method == 'POST':
		errors = page_type_obj.find_errors(request.POST)
		kwargs = page_type_obj.get_kwargs(request.POST)
		course_sem, created = CourseSemester.objects.get_or_create(term=request.POST['term'], year=request.POST['year'], course=course)
		is_unique = Page.objects.filter(course_sem=course_sem, slug=kwargs['slug']).count() == 0
		if errors or not is_unique: # it returns None only if nothing is wrong
			data['errors'] = errors
			if not is_unique:
				data['errors'].append('Subject or whatever not unique') # Fix this later
			# Keep the posted data
			data['current_term'] = request.POST['term']
			try:
				data['current_year'] = int(request.POST['year'])
			except ValueError:
				pass # defaults to the current year
			data['current_exam_type'] = request.POST['exam_type'] if 'exam_type' in request.POST else ''
			data['subject'] =  request.POST['subject'] if 'subject' in request.POST else ''

			data['content'] = request.POST['content']
			data['message'] = request.POST['message']

			return render(request, 'pages/create.html', data)
		else:
			commit_message = request.POST['message']
			# The title and subject are generated by the PageType object, in kwargs
			new_page = Page(course_sem=course_sem, page_type=page_type, **kwargs)
			new_page.save()
			username = request.user.username
			email = request.user.email
			new_page.save_content(request.POST['content'], commit_message, username)

			# Add the history item - should be done automatically one day
			course.add_event(page=new_page, user=request.user, action='created', message=commit_message)
			data['page'] = new_page
			return show(request, department, number, page_type, course_sem.term, course_sem.year, new_page.slug)

	return render(request, 'pages/create.html', data)
Exemple #2
0
def create(request, department, number, page_type):
	course = get_object_or_404(Course, department=department, number=int(number))

	if page_type not in types:
		raise Http404
	else:
		obj = types[page_type]
		if request.method == 'POST':
			# Create the page
			num_sections = request.POST['num_sections']
			term = request.POST['term']
			year = request.POST['year']
			try:
				course_sem = CourseSemester.objects.get(term=term, year=year, course=course)
			except CourseSemester.DoesNotExist:
				course_sem = CourseSemester(term=term, year=year, course=course)
				course_sem.save()

			# The title and subject are generated by the PageType object, in kwargs
			kwargs = obj.get_kwargs(request.POST)
			new_page = Page(course_sem=course_sem, num_sections=num_sections, page_type=page_type, **kwargs)
			new_page.save()
			username = request.user.username if request.user.is_authenticated() else 'Anonymous'
			email = request.user.email if request.user.is_authenticated() else '*****@*****.**'
			new_page.save_sections(request.POST, username, email)
			data = {
				'course': course,
			}
			# Get the keyword arguments from the page type method
			if new_page:
				data['page'] = new_page
				return render(request, "pages/success.html", data)
			else:
				return render(request, "pages/error.html", data)
		else:
			data = {
				'course': course,
				'page_type': obj,
				'form_template': obj.get_form_template(),
				'help_template': obj.get_help_template(),
				'field_templates': obj.get_field_templates(),
				'terms': ['winter', 'summer', 'fall'], # fix this later
				'years': range(2011, 1999, -1),
				'num_sections': range(1, 11), # for people without javascript DON'T DELETE THIS UNLESS YOU HAVE ANOTHER SOLUTION FOR A FALLBACK
			}
			return render(request, "pages/create.html", data)
Exemple #3
0
def create(request, department, number, page_type, semester=None):
	if not request.user.is_authenticated():
		return register(request)

	course = get_object_or_404(Course, department=department.upper(), number=int(number))

	if page_type not in page_types:
		raise Http404

	page_type_obj = page_types[page_type]
	data = {
		'professors': Professor.objects.all(),
		'title': 'Create a page (%s)' % course,
		'course': course,
		'page_type': page_type_obj,
		'terms': terms,
		'field_templates': page_type_obj.get_uneditable_fields() + page_type_obj.get_editable_fields(),
		'years': years,
		'page_type_url': page_type_obj.get_url(course), # can't call it on page_type_obj directly
		'current_term': current_term,
		'current_year': current_year,
		'exam_types': exam_types,
		'current_exam_type': exam_types[0], # default
		'edit_mode': False,
	}

	if semester is not None:
		data['current_term'] = semester[0]
		data['current_year'] = int(semester[1])
		data['does_not_exist'] = True

	if request.method == 'POST':
		errors = page_type_obj.find_errors(request.POST)
		kwargs = page_type_obj.get_kwargs(request.POST)
		course_sem, created = CourseSemester.objects.get_or_create(term=request.POST['term'], year=request.POST['year'], course=course)

		is_unique = Page.objects.filter(course_sem=course_sem, slug=kwargs['slug']).count() == 0
		if errors or not is_unique: # it returns None only if nothing is wrong
			data['errors'] = errors
			if not is_unique:
				data['errors'].append('Subject or whatever not unique') # Fix this later
			# Keep the posted data
			data['current_term'] = request.POST['term']
			try:
				data['current_year'] = int(request.POST['year'])
			except ValueError:
				pass # defaults to the current year
			data['current_exam_type'] = request.POST['exam_type'] if 'exam_type' in request.POST else ''
			data['subject'] = request.POST['subject'] if 'subject' in request.POST else ''

			data['content'] = request.POST['content']
			data['message'] = request.POST['message']
			try:
				data['current_professor'] = int(request.POST['professor_id'])
			except (ValueError, KeyError):
				pass

			return render(request, 'pages/create.html', data)
		else:
			commit_message = request.POST['message'] if request.POST['message'] else 'Minor edit'
			# The title and subject are generated by the PageType object, in kwargs
			new_page = Page(course_sem=course_sem, page_type=page_type, **kwargs)
			new_page.save()
			username = request.user.username
			email = request.user.email
			new_page.save_content(request.POST['content'], commit_message, username)

			# Add the history item - should be done automatically one day
			course.add_event(page=new_page, user=request.user, action='created', message=commit_message)
			data['page'] = new_page

			# If the user isn't watching the course already, start watching
			user = request.user.get_profile()
			if not user.is_watching(course):
				user.start_watching(course)

			return redirect(new_page.get_absolute_url())

	return render(request, 'pages/create.html', data)