def feedback_confirm(request, pk): """ Form to confirm the feedback given by a user. Only for superusers. Sends a simple confirm mail to the user. :param request: :param pk: id of the feedback report :return: """ obj = get_object_or_404(FeedbackReport, pk=pk) if obj.Status != 1: return render(request, "base.html", { "Message": "Report is not in open status.", "return": "index:list_feedback" }) obj.Status = 2 obj.save() send_mail("feedback report", "email/feedback_report_email_statuschange.html", { "report": obj }, obj.Reporter.email) return render(request, "base.html", { "Message": "Report confirmed and reporter notified.", "return": "index:list_feedback" })
def feedback_submit(request): """ The 'thanks' page after feedback is submitted by a user. Also shows form validation errors if present. :param request: :return: """ if request.method != "POST": return render(request, "base.html", { "Message": "Please do not access this page directly." }) form = FeedbackForm(request.POST) if form.is_valid(): feedback = form.save(commit=False) feedback.Reporter = request.user feedback.Status = 1 feedback.save() send_mail("feedback report created", "email/feedback_report_email_created.html", { "report": feedback }, feedback.Reporter.email) send_mail("feedback report created", "email/feedback_report_admin.html", { "report": feedback }, settings.CONTACT_EMAIL) return render(request, "base.html", { "Message": "Feedback saved, thank you for taking the time to improve the system!" }) return render(request, "index/feedback_form.html", { "form": form, "formtitle": "Feedback Form", "buttontext": "Send", "support_name": settings.SUPPORT_NAME, })
def edit_user_groups(request, pk): """ Change the groups of a given user. :param request: :param pk: user id :return: """ usr = get_object_or_404(User, pk=pk) if not usr.groups.exists(): if not usr.is_superuser: raise PermissionDenied( "This user is a student. Students cannot have groups.") if get_grouptype("2u") in usr.groups.all(): raise PermissionDenied( "This user is not yet verified. Please verify first in the user list." ) if request.method == "POST": form = UserGroupsForm(request.POST, instance=usr) if form.is_valid(): if form.has_changed(): # call print list here to force query execute old = print_list(usr.groups.all().values_list('name', flat=True)) form.save() new = print_list(usr.groups.all().values_list('name', flat=True)) send_mail("user groups changed", "email/user_groups_changed.html", { 'oldgroups': old, 'newgroups': new, 'user': usr }, usr.email) return render( request, 'base.html', { 'Message': 'User groups saved!', 'return': 'support:listusers', }) return render(request, 'base.html', { 'Message': 'No changes made.', 'return': 'support:listusers', }) else: form = UserGroupsForm(instance=usr) return render(request, 'support/user_groups_form.html', { 'formtitle': 'Set user groups for {}'.format(usr.username), 'form': form, })
def verify_assistant_fn(user): """ Verify an unverified user and mail a confirmation. :param user: :return: """ account_group = User.groups.through.objects.get(user=user) account_group.group = get_grouptype("2") account_group.save() # inform the user of verification. send_mail("user groups changed", "email/user_groups_changed.html", {'oldgroups': 'type2staff unverified', 'newgroups': 'type2staff', 'message': 'Your account is now verified!', 'user': user}, user.email) return True
def feedback_close(request, pk): """ Close a feedback report. Send a custom message to the user that gave the feedback. Only for superusers :param request: :param pk: id of the feedback report :return: """ obj = get_object_or_404(FeedbackReport, pk=pk) if obj.Status == 3: return render(request, "base.html", { "Message": "Report is already closed.", "return": "index:list_feedback" }) if request.method == "POST": form = CloseFeedbackReportForm(request.POST, initial={ 'email': obj.Reporter.email }) if form.is_valid(): obj.Status = 3 obj.save() send_mail("feedback report", "email/feedback_report_email_statuschange.html", { "report": obj, "message": form.cleaned_data["message"], }, obj.Reporter.email) return render(request, "base.html", { "Message": "Report closed and reporter notified.", "return": "index:list_feedback" }) else: form = CloseFeedbackReportForm(initial={ 'email': obj.Reporter.email }) return render(request, "GenericForm.html", { "formtitle": "Close Feedback Report", "buttontext": "Close", "form": form })
def respond_file(request, pk): """ Form to let a staff member give a response to a students file. :param request: :param pk: :return: """ fileobj = get_object_or_404(StudentFile, pk=pk) if not get_grouptype('3') in request.user.groups.all(): if (request.user not in fileobj.Distribution.Proposal.Assistants.all() and request.user != fileobj.Distribution.Proposal.ResponsibleStaff and request.user != fileobj.Distribution.Proposal.Track.Head) \ or not fileobj.Type.CheckedBySupervisor: raise PermissionDenied("You cannot respond to this file.") if fileobj.Distribution.TimeSlot != get_timeslot(): raise PermissionDenied("Changing history is not allowed.") try: responseobj = fileobj.staffresponse responseobj.Staff = request.user statusorig = responseobj.Status except StaffResponse.DoesNotExist: responseobj = StaffResponse() responseobj.File = fileobj responseobj.Staff = request.user statusorig = None if request.method == 'POST': aspect_forms = [] for i, aspect in enumerate(fileobj.Type.aspects.all()): try: aspect_result = StaffResponseFileAspectResult.objects.get( Aspect=aspect, Response=responseobj) except StaffResponseFileAspectResult.DoesNotExist: aspect_result = StaffResponseFileAspectResult( Aspect=aspect, Response=responseobj) aspect_forms.append({ "form": StaffResponseFileAspectResultForm(request.POST, instance=aspect_result, prefix="aspect" + str(i)), "aspect": aspect }) response_form = StaffResponseForm(request.POST, instance=responseobj, prefix='response') if response_form.is_valid() and all( [form['form'].is_valid() for form in aspect_forms]): if response_form.cleaned_data['Status'] != statusorig: send_mail( 'Professional skill feedback', 'email/prvresponse.html', { 'student': fileobj.Distribution.Student, 'status': response_form.cleaned_data['Status'], 'explanation': response_form.cleaned_data['Explanation'], 'type': fileobj.Type.Name, }, fileobj.Distribution.Student.email) response_form.save() # for first time saving, refetch all aspects as they are now tied to responseobj that is actually saved aspect_forms = [] for i, aspect in enumerate(fileobj.Type.aspects.all()): try: aspect_result = StaffResponseFileAspectResult.objects.get( Aspect=aspect, Response=responseobj) except StaffResponseFileAspectResult.DoesNotExist: aspect_result = StaffResponseFileAspectResult( Aspect=aspect, Response=responseobj) aspect_forms.append({ "form": StaffResponseFileAspectResultForm(request.POST, instance=aspect_result, prefix="aspect" + str(i)), "aspect": aspect }) all([form['form'].save() for form in aspect_forms]) return render( request, 'base.html', { 'Message': 'Response saved!', 'return': 'support:liststudents', 'returnget': fileobj.Distribution.TimeSlot.pk, }) else: aspect_forms = [] for i, aspect in enumerate(fileobj.Type.aspects.all()): try: aspect_result = StaffResponseFileAspectResult.objects.get( Aspect=aspect, Response=responseobj) except StaffResponseFileAspectResult.DoesNotExist: aspect_result = StaffResponseFileAspectResult( Aspect=aspect, Response=responseobj) aspect_forms.append({ "form": StaffResponseFileAspectResultForm(instance=aspect_result, prefix="aspect" + str(i)), "aspect": aspect }) response_form = StaffResponseForm(instance=responseobj, prefix='response') return render( request, 'professionalskills/staffresponseform.html', { 'form': response_form, 'formtitle': 'Respond to {} from {}'.format( fileobj.Type.Name, fileobj.Distribution.Student.usermeta.get_nice_name()), 'aspectforms': aspect_forms, "aspectlabels": StaffResponseFileAspectResult.ResultOptions, })