Exemple #1
0
def cp_annals(request):
    alert_status = ""
    message = ""
    if request.method == "GET":
        # search annal
        if "q" in request.GET and request.GET['q']:
            annals = Annal.objects.filter(
                annal_name__icontains=request.GET['q'])
            if annals:
                message = _(
                    "<strong>{} annals</strong> found matching your search.".
                    format(len(annals)))
                alert_status = "alert-info"
            else:
                message = _(
                    "<strong>No annals</strong> found matching your search.")
                alert_status = "alert-warning"
            try:
                page_num = int(request.GET['page'])
            except:
                page_num = 1
            [pages_count, page_num,
             annals] = Utils.get_from_page(annals, page_num)
            Utils.process_annals(annals)
            return render(
                request, "what/cp_annals.html", {
                    "not_index_page":
                    True,
                    "message":
                    mark_safe(message),
                    "alert_status":
                    alert_status,
                    "page_num":
                    page_num,
                    "previous_page":
                    max(1, page_num - 1),
                    "next_page":
                    min(pages_count, page_num + 1),
                    "displayed_pages":
                    Utils.get_displayed_pages(pages_count, page_num),
                    "search_term":
                    request.GET['q'],
                    "annals":
                    annals,
                })
    if request.method == "POST":
        # delete annal
        if "changelist-action" in request.POST and request.POST[
                'changelist-action'] == "delete":
            annal_ids = []
            for element in request.POST:
                if element.startswith("sel-annal-") and request.POST[element]:
                    try:
                        annal_ids.append(int(element[10:]))
                    except:
                        continue
            try:
                Annal.objects.filter(id__in=annal_ids).delete()
                message = _("{} annal(s) deleted.".format(len(annal_ids)))
                alert_status = "alert-success"
            except:
                message = _(
                    "Due to an unknown error, the selected annal(s) could not be deleted."
                )
                alert_status = "alert-danger"
        elif "annal-action" in request.POST:
            # add annal
            if request.POST['annal-action'] == "add":
                # TODO: check for teacher access
                try:
                    this_teacher = Teacher.objects.get(
                        user__username=request.session['username'])
                    if "annal_enabled" in request.POST and request.POST[
                            'annal_enabled'] == 'on':
                        enabled = True
                    else:
                        enabled = False
                    # TODO: use html.escape for 'rules' and allow markdown or rst instead of html
                    rules = request.POST['annal_rules']
                    # TODO: convert from different units of times to seconds for 'duration'
                    duration = request.POST['annal_duration']
                    if "annal_reveal_answers" in request.POST and request.POST[
                            'annal_reveal_answers'] == 'on':
                        show_answers = True
                    else:
                        show_answers = False
                    if "annal_starts_on_hidden" in request.POST and request.POST[
                            'annal_starts_on_hidden']:
                        auto_enable = True
                        auto_enable_date = Utils.convert_datetime(
                            request.POST['annal_starts_on_text'])
                    else:
                        auto_enable = False
                        auto_enable_date = None
                    if "annal_ends_on_hidden" in request.POST and request.POST[
                            'annal_ends_on_hidden']:
                        auto_disable = True
                        auto_disable_date = Utils.convert_datetime(
                            request.POST['annal_ends_on_text'])
                    else:
                        auto_disable = False
                        auto_disable_date = None
                    new_annal = Annal(
                        annal_name=(request.POST['annal_name']).strip(),
                        teacher=this_teacher,
                        enabled=enabled,
                        rules=rules,
                        annal_duration=duration,
                        show_correct_answers_at_end=show_answers,
                        auto_enable=auto_enable,
                        auto_enable_date=auto_enable_date,
                        auto_disable=auto_disable,
                        auto_disable_date=auto_disable_date)
                    new_annal.save()
                    message = _(
                        "Annal <strong>{}</strong> is successfully added.".
                        format(request.POST['annal_name']))
                    alert_status = "alert-success"
                except:
                    message = _(
                        "Due to an unknown error, the annal could not be created."
                    )
                    alert_status = "alert-danger"
            # edit annal
            elif request.POST['annal-action'] == "edit":
                try:
                    # TODO: check for teacher access
                    this_teacher = Teacher.objects.get(
                        user__username=request.session['username'])
                    edited_annal = Annal(id=request.POST['annal_id'])
                    edited_annal.annal_name = request.POST['annal_name']
                    if "annal_enabled" in request.POST:
                        edited_annal.enabled = request.POST['annal_enabled']
                    else:
                        edited_annal.enabled = False
                    if "annal_reveal_answers" in request.POST:
                        edited_annal.show_correct_answers_at_end = request.POST[
                            'annal_reveal_answers']
                    else:
                        edited_annal.show_correct_answers_at_end = False
                    edited_annal.annal_duration = int(
                        request.POST['annal_duration'])
                    # TODO: safely escape 'rules' content
                    # TODO: deal with different datetime formats
                    edited_annal.rules = request.POST['annal_rules']
                    if "annal_starts_on_checkbox" in request.POST:
                        if request.POST['annal_starts_on_checkbox'] == "on":
                            edited_annal.auto_enable = True
                            starts_on_date = request.POST[
                                'annal_starts_on_text'].replace(
                                    "midnight", "12:00 am")
                            edited_annal.auto_enable_date = datetime.strptime(
                                starts_on_date, "%m/%d/%Y %I:%M %p")
                        else:
                            edited_annal.auto_enable = False
                    else:
                        edited_annal.auto_enable = False
                    if "annal_ends_on_checkbox" in request.POST:
                        if request.POST['annal_ends_on_checkbox'] == "on":
                            edited_annal.auto_disable = True
                            ends_on_date = request.POST[
                                'annal_ends_on_text'].replace(
                                    "midnight", "12:00 am")
                            edited_annal.auto_disable_date = datetime.strptime(
                                ends_on_date, "%m/%d/%Y %I:%M %p")
                        else:
                            edited_annal.auto_disable = False
                    else:
                        edited_annal.auto_disable = False
                    edited_annal.teacher = this_teacher
                    edited_annal.save()
                    message = _(
                        "Annal <strong>{}</strong> is successfully saved.".
                        format(request.POST['annal_name']))
                    alert_status = "alert-success"
                except:
                    message = _(
                        "Due to an unknown error, the annal could not be saved to database."
                    )
                    alert_status = "alert-danger"
    annals = Annal.objects.all().order_by("annal_name")
    try:
        page_num = int(request.GET['page'])
    except:
        page_num = 1
    [pages_count, page_num, annals] = Utils.get_from_page(annals, page_num)
    Utils.process_annals(annals)
    return render(
        request, "what/cp_annals.html", {
            "not_index_page": True,
            "alert_status": alert_status,
            "message": mark_safe(message),
            "page_num": page_num,
            "previous_page": max(1, page_num - 1),
            "next_page": min(pages_count, page_num + 1),
            "displayed_pages": Utils.get_displayed_pages(
                pages_count, page_num),
            "annals": annals,
        })
Exemple #2
0
def cp_annals(request):
	alert_status = ""
	message = ""
	if request.method == "GET":
		# search annal
		if "q" in request.GET and request.GET['q']:
			annals = Annal.objects.filter(annal_name__icontains=request.GET['q'])
			if annals:
				message = _("<strong>{} annals</strong> found matching your search.".format(len(annals)))
				alert_status = "alert-info"
			else:
				message = _("<strong>No annals</strong> found matching your search.")
				alert_status = "alert-warning"
			try:
				page_num = int(request.GET['page'])
			except:
				page_num = 1
			[pages_count, page_num, annals] = Utils.get_from_page(annals, page_num)
			Utils.process_annals(annals)
			return render(request, "what/cp_annals.html", {
				"not_index_page": True,
				"message": mark_safe(message),
				"alert_status": alert_status,
				"page_num": page_num,
				"previous_page": max(1, page_num - 1),
				"next_page": min(pages_count, page_num + 1),
				"displayed_pages": Utils.get_displayed_pages(pages_count, page_num),
				"search_term": request.GET['q'],
				"annals": annals,
				})
	if request.method == "POST":
		# delete annal
		if "changelist-action" in request.POST and request.POST['changelist-action'] == "delete":
			annal_ids = []
			for element in request.POST:
				if element.startswith("sel-annal-") and request.POST[element]:
					try:
						annal_ids.append(int(element[10:]))
					except:
						continue
			try:
				Annal.objects.filter(id__in=annal_ids).delete()
				message = _("{} annal(s) deleted.".format(len(annal_ids)))
				alert_status = "alert-success"
			except:
				message = _("Due to an unknown error, the selected annal(s) could not be deleted.")
				alert_status = "alert-danger"
		elif "annal-action" in request.POST:
			# add annal
			if request.POST['annal-action'] == "add":
				# TODO: check for teacher access
				try:
					this_teacher = Teacher.objects.get(user__username=request.session['username'])
					if "annal_enabled" in request.POST and request.POST['annal_enabled'] == 'on':
						enabled = True
					else:
						enabled = False
					# TODO: use html.escape for 'rules' and allow markdown or rst instead of html
					rules = request.POST['annal_rules']
					# TODO: convert from different units of times to seconds for 'duration'
					duration = request.POST['annal_duration']
					if "annal_reveal_answers" in request.POST and request.POST['annal_reveal_answers'] == 'on':
						show_answers = True
					else:
						show_answers = False
					if "annal_starts_on_hidden" in request.POST and request.POST['annal_starts_on_hidden']:
						auto_enable = True
						auto_enable_date = Utils.convert_datetime(request.POST['annal_starts_on_text'])
					else:
						auto_enable = False
						auto_enable_date = None
					if "annal_ends_on_hidden" in request.POST and request.POST['annal_ends_on_hidden']:
						auto_disable = True
						auto_disable_date = Utils.convert_datetime(request.POST['annal_ends_on_text'])
					else:
						auto_disable = False
						auto_disable_date = None
					new_annal = Annal(annal_name=(request.POST['annal_name']).strip(), teacher=this_teacher,
						enabled=enabled, rules=rules, annal_duration=duration, show_correct_answers_at_end=show_answers,
						auto_enable=auto_enable, auto_enable_date=auto_enable_date,
						auto_disable=auto_disable, auto_disable_date=auto_disable_date)
					new_annal.save()
					message = _("Annal <strong>{}</strong> is successfully added.".format(request.POST['annal_name']))
					alert_status = "alert-success"
				except:
					message = _("Due to an unknown error, the annal could not be created.")
					alert_status = "alert-danger"
			# edit annal
			elif request.POST['annal-action'] == "edit":
				try:
					# TODO: check for teacher access
					this_teacher = Teacher.objects.get(user__username=request.session['username'])
					edited_annal = Annal(id=request.POST['annal_id'])
					edited_annal.annal_name = request.POST['annal_name']
					if "annal_enabled" in request.POST:
						edited_annal.enabled = request.POST['annal_enabled']
					else:
						edited_annal.enabled = False
					if "annal_reveal_answers" in request.POST:
						edited_annal.show_correct_answers_at_end = request.POST['annal_reveal_answers']
					else:
						edited_annal.show_correct_answers_at_end = False
					edited_annal.annal_duration = int(request.POST['annal_duration'])
					# TODO: safely escape 'rules' content
					# TODO: deal with different datetime formats
					edited_annal.rules = request.POST['annal_rules']
					if "annal_starts_on_checkbox" in request.POST:
						if request.POST['annal_starts_on_checkbox'] == "on":
							edited_annal.auto_enable = True
							starts_on_date = request.POST['annal_starts_on_text'].replace("midnight", "12:00 am")
							edited_annal.auto_enable_date = datetime.strptime(starts_on_date, "%m/%d/%Y %I:%M %p")
						else:
							edited_annal.auto_enable = False
					else:
						edited_annal.auto_enable = False
					if "annal_ends_on_checkbox" in request.POST:
						if request.POST['annal_ends_on_checkbox'] == "on":
							edited_annal.auto_disable = True
							ends_on_date = request.POST['annal_ends_on_text'].replace("midnight", "12:00 am")
							edited_annal.auto_disable_date = datetime.strptime(ends_on_date, "%m/%d/%Y %I:%M %p")
						else:
							edited_annal.auto_disable = False
					else:
						edited_annal.auto_disable = False
					edited_annal.teacher = this_teacher
					edited_annal.save()
					message = _("Annal <strong>{}</strong> is successfully saved.".format(request.POST['annal_name']))
					alert_status = "alert-success"
				except:
					message = _("Due to an unknown error, the annal could not be saved to database.")
					alert_status = "alert-danger"
	annals = Annal.objects.all().order_by("annal_name")
	try:
		page_num = int(request.GET['page'])
	except:
		page_num = 1
	[pages_count, page_num, annals] = Utils.get_from_page(annals, page_num)
	Utils.process_annals(annals)
	return render(request, "what/cp_annals.html", {
		"not_index_page": True,
		"alert_status": alert_status,
		"message": mark_safe(message),
		"page_num": page_num,
		"previous_page": max(1, page_num - 1),
		"next_page": min(pages_count, page_num + 1),
		"displayed_pages": Utils.get_displayed_pages(pages_count, page_num),
		"annals": annals,
		})
Exemple #3
0
def cp_students(request):
    alert_status = ""
    message = ""
    if request.method == "GET":
        # search student
        if "q" in request.GET and request.GET['q']:
            students = Student.objects.filter(
                student_name__icontains=request.GET['q'])
            if students:
                message = _(
                    "<strong>{} students</strong> found matching your search.".
                    format(len(students)))
                alert_status = "alert-info"
            else:
                message = _(
                    "<strong>No students</strong> found matching your search.")
                alert_status = "alert-warning"
            try:
                page_num = int(request.GET['page'])
            except:
                page_num = 1
            [pages_count, page_num,
             students] = Utils.get_from_page(students, page_num)
            return render(
                request, "what/cp_students.html", {
                    "not_index_page":
                    True,
                    "message":
                    mark_safe(message),
                    "alert_status":
                    alert_status,
                    "page_num":
                    page_num,
                    "previous_page":
                    max(1, page_num - 1),
                    "next_page":
                    min(pages_count, page_num + 1),
                    "displayed_pages":
                    Utils.get_displayed_pages(pages_count, page_num),
                    "search_term":
                    request.GET['q'],
                    "students":
                    students,
                })
    elif request.method == "POST":
        # delete student
        if "changelist-action" in request.POST and request.POST[
                'changelist-action'] == "delete":
            student_ids = []
            for element in request.POST:
                if element.startswith(
                        "sel-student-") and request.POST[element]:
                    try:
                        student_ids.append(int(element[12:]))
                    except:
                        continue
            try:
                Student.objects.filter(id__in=student_ids).delete()
                message = _("{} student(s) deleted.".format(len(student_ids)))
                alert_status = "alert-success"
            except:
                message = _(
                    "Due to an unknown error, the selected student(s) could not be deleted."
                )
                alert_status = "alert-danger"
        elif "student-action" in request.POST:
            # add student
            if request.POST['student-action'] == "add":
                try:
                    new_student = Student(
                        student_name=(request.POST['student_name']).strip())
                    new_student.save()
                    message = _(
                        "Student <strong>{}</strong> is successfully added.".
                        format(request.POST['student_name']))
                    alert_status = "alert-success"
                except IntegrityError:
                    message = _("Student already exists in database.")
                    alert_status = "alert-danger"
                except:
                    message = _(
                        "Due to an unknown error, the student could not be created."
                    )
                    alert_status = "alert-danger"
            # edit student
            elif request.POST['student-action'] == "edit":
                try:
                    # no need to check for teacher access on changing students
                    edited_student = Student(id=request.POST['student_id'])
                    edited_student.student_name = request.POST['student_name']
                    edited_student.save()
                    message = _(
                        "Student <strong>{}</strong> is successfully saved.".
                        format(request.POST['annal_name']))
                    alert_status = "alert-success"
                except IntegrityError:
                    message = _(
                        "Student with same name already exists in database.")
                    alert_status = "alert-danger"
                except:
                    message = _(
                        "Due to an unknown error, the student could not be saved to database."
                    )
                    alert_status = "alert-danger"
    students = Student.objects.all().order_by("student_name")
    try:
        page_num = int(request.GET['page'])
    except:
        page_num = 1
    [pages_count, page_num, students] = Utils.get_from_page(students, page_num)
    return render(
        request, "what/cp_students.html", {
            "not_index_page": True,
            "alert_status": alert_status,
            "message": mark_safe(message),
            "page_num": page_num,
            "previous_page": max(1, page_num - 1),
            "next_page": min(pages_count, page_num + 1),
            "displayed_pages": Utils.get_displayed_pages(
                pages_count, page_num),
            "students": students,
        })
Exemple #4
0
def cp_students(request):
	alert_status = ""
	message = ""
	if request.method == "GET":
		# search student
		if "q" in request.GET and request.GET['q']:
			students = Student.objects.filter(student_name__icontains=request.GET['q'])
			if students:
				message = _("<strong>{} students</strong> found matching your search.".format(len(students)))
				alert_status = "alert-info"
			else:
				message = _("<strong>No students</strong> found matching your search.")
				alert_status = "alert-warning"
			try:
				page_num = int(request.GET['page'])
			except:
				page_num = 1
			[pages_count, page_num, students] = Utils.get_from_page(students, page_num)
			return render(request, "what/cp_students.html", {
				"not_index_page": True,
				"message": mark_safe(message),
				"alert_status": alert_status,
				"page_num": page_num,
				"previous_page": max(1, page_num - 1),
				"next_page": min(pages_count, page_num + 1),
				"displayed_pages": Utils.get_displayed_pages(pages_count, page_num),
				"search_term": request.GET['q'],
				"students": students,
				})
	elif request.method == "POST":
		# delete student
		if "changelist-action" in request.POST and request.POST['changelist-action'] == "delete":
			student_ids = []
			for element in request.POST:
				if element.startswith("sel-student-") and request.POST[element]:
					try:
						student_ids.append(int(element[12:]))
					except:
						continue
			try:
				Student.objects.filter(id__in=student_ids).delete()
				message = _("{} student(s) deleted.".format(len(student_ids)))
				alert_status = "alert-success"
			except:
				message = _("Due to an unknown error, the selected student(s) could not be deleted.")
				alert_status = "alert-danger"
		elif "student-action" in request.POST:
			# add student
			if request.POST['student-action'] == "add":
				try:
					new_student = Student(student_name=(request.POST['student_name']).strip())
					new_student.save()
					message = _("Student <strong>{}</strong> is successfully added.".format(request.POST['student_name']))
					alert_status = "alert-success"
				except IntegrityError:
					message = _("Student already exists in database.")
					alert_status = "alert-danger"
				except:
					message = _("Due to an unknown error, the student could not be created.")
					alert_status = "alert-danger"
			# edit student
			elif request.POST['student-action'] == "edit":
				try:
					# no need to check for teacher access on changing students
					edited_student = Student(id=request.POST['student_id'])
					edited_student.student_name = request.POST['student_name']
					edited_student.save()
					message = _("Student <strong>{}</strong> is successfully saved.".format(request.POST['annal_name']))
					alert_status = "alert-success"
				except IntegrityError:
					message = _("Student with same name already exists in database.")
					alert_status = "alert-danger"
				except:
					message = _("Due to an unknown error, the student could not be saved to database.")
					alert_status = "alert-danger"
	students = Student.objects.all().order_by("student_name")
	try:
		page_num = int(request.GET['page'])
	except:
		page_num = 1
	[pages_count, page_num, students] = Utils.get_from_page(students, page_num)
	return render(request, "what/cp_students.html", {
		"not_index_page": True,
		"alert_status": alert_status,
		"message": mark_safe(message),
		"page_num": page_num,
		"previous_page": max(1, page_num - 1),
		"next_page": min(pages_count, page_num + 1),
		"displayed_pages": Utils.get_displayed_pages(pages_count, page_num),
		"students": students,
		})