def my_profile_view(request): """ The view of the profile page. """ page_name = "Profile Page" if request.user.username == ANONYMOUS_USERNAME: return red_home(request, MESSAGES["SPINELESS"]) userProfile = UserProfile.objects.get(user=request.user) change_password_form = PasswordChangeForm( request.user, request.POST if "submit_password_form" in request.POST else None ) update_email_form = UpdateEmailForm( request.POST if "submit_profile_form" in request.POST else None, instance=request.user, prefix="user" ) update_profile_form = UpdateProfileForm( request.POST if "submit_profile_form" in request.POST else None, instance=userProfile, prefix="profile" ) if change_password_form.is_valid(): change_password_form.save() messages.add_message(request, messages.SUCCESS, "Your password was successfully changed.") return HttpResponseRedirect(reverse("my_profile")) if update_email_form.is_valid() and update_profile_form.is_valid(): update_email_form.save() update_profile_form.save() messages.add_message(request, messages.SUCCESS, "Your profile has been successfully updated.") return HttpResponseRedirect(reverse("my_profile")) return render_to_response( "my_profile.html", { "page_name": page_name, "update_email_form": update_email_form, "update_profile_form": update_profile_form, "change_password_form": change_password_form, }, context_instance=RequestContext(request), )
def my_profile_view(request): ''' The view of the profile page. ''' page_name = "Profile Page" if request.user.username == ANONYMOUS_USERNAME: return red_home(request, MESSAGES['SPINELESS']) user = request.user userProfile = UserProfile.objects.get(user=request.user) change_password_form = PasswordChangeForm( request.user, request.POST if "submit_password_form" in request.POST else None, ) update_profile_form = UpdateProfileForm( request.POST if "submit_profile_form" in request.POST else None, user=request.user, initial={ 'former_houses': userProfile.former_houses, 'email': user.email, 'email_visible_to_others': userProfile.email_visible, 'phone_number': userProfile.phone_number, 'phone_visible_to_others': userProfile.phone_visible, }, ) if change_password_form.is_valid(): change_password_form.save() messages.add_message(request, messages.SUCCESS, "Your password was successfully changed.") return HttpResponseRedirect(reverse('my_profile')) if update_profile_form.is_valid(): update_profile_form.save() messages.add_message(request, messages.SUCCESS, "Your profile has been successfully updated.") return HttpResponseRedirect(reverse('my_profile')) return render_to_response('my_profile.html', { 'page_name': page_name, 'update_profile_form': update_profile_form, 'change_password_form': change_password_form, }, context_instance=RequestContext(request))
def wrap(request, *args, **kwargs): if not request.user.is_authenticated(): redirect_to = reverse(redirect_no_user) if redirect_no_user == "login": redirect_to += "?next=" + request.path return HttpResponseRedirect(redirect_to) if not can_manage(request.user, semester=kwargs.get("semester", None)): messages = MESSAGES['ADMINS_ONLY'] if Semester.objects.filter(current=True).count() == 0: messages = "Workshift semester has not been created yet. " + messages return red_home(request, messages) return redirect_profile(request, messages) return view_func(request, *args, **kwargs)
def my_profile_view(request): ''' The view of the profile page. ''' page_name = "Profile Page" if request.user.username == ANONYMOUS_USERNAME: return red_home(request, MESSAGES['SPINELESS']) userProfile = UserProfile.objects.get(user=request.user) change_password_form = PasswordChangeForm( request.user, request.POST if "submit_password_form" in request.POST else None, ) update_email_form = UpdateEmailForm( request.POST if "submit_profile_form" in request.POST else None, instance=request.user, prefix="user", ) update_profile_form = UpdateProfileForm( request.POST if "submit_profile_form" in request.POST else None, instance=userProfile, prefix="profile", ) if change_password_form.is_valid(): change_password_form.save() messages.add_message(request, messages.SUCCESS, "Your password was successfully changed.") return HttpResponseRedirect(reverse('my_profile')) if update_email_form.is_valid() and update_profile_form.is_valid(): update_email_form.save() update_profile_form.save() messages.add_message(request, messages.SUCCESS, "Your profile has been successfully updated.") return HttpResponseRedirect(reverse('my_profile')) return render_to_response('my_profile.html', { 'page_name': page_name, "update_email_form": update_email_form, 'update_profile_form': update_profile_form, 'change_password_form': change_password_form, }, context_instance=RequestContext(request))
def requests_view(request, requestType): ''' Generic request view. Parameters: request is the HTTP request requestType is URL name of a RequestType. e.g. "food", "maintenance", "network", "site" ''' userProfile = UserProfile.objects.get(user=request.user) request_type = get_object_or_404(RequestType, url_name=requestType) page_name = "{0} Requests".format(request_type.name.title()) if not request_type.enabled: message = "{0} requests have been disabled.".format(request_type.name.title()) return red_home(request, message) relevant_managers = request_type.managers.filter(active=True) manager = any(i.incumbent == userProfile for i in relevant_managers) request_form = RequestForm( request.POST if "submit_request" in request.POST else None, profile=userProfile, request_type=request_type, ) if request_form.is_valid(): request_form.save() return HttpResponseRedirect(reverse('managers:requests', kwargs={'requestType': requestType})) # number of requests loaded x = 0 # A pseudo-dictionary, actually a list with items of form (request, # [request_responses_list], response_form, upvote, vote_form) requests_dict = list() requests = Request.objects.filter(request_type=request_type) if not request_type.managers.filter(incumbent__user=request.user): requests = requests.exclude( ~Q(owner__user=request.user), private=True, ) for req in requests: request_responses = Response.objects.filter(request=req) if manager: response_form = ManagerResponseForm( request.POST if "add_response-{0}".format(req.pk) in request.POST else None, initial={'action': Response.NONE}, prefix="{0}".format(req.pk), profile=userProfile, request=req, ) else: response_form = ResponseForm( request.POST if "add_response-{0}".format(req.pk) in request.POST else None, prefix="{0}".format(req.pk), profile=userProfile, request=req, ) upvote = userProfile in req.upvotes.all() vote_form = VoteForm( request.POST if "vote-{0}".format(req.pk) in request.POST else None, profile=userProfile, request=req, ) if response_form.is_valid(): response_form.save() return HttpResponseRedirect(reverse('managers:requests', kwargs={'requestType': requestType})) if vote_form.is_valid(): vote_form.save() return HttpResponseRedirect(reverse('managers:requests', kwargs={'requestType': requestType})) requests_dict.append((req, request_responses, response_form, upvote, vote_form)) x += 1 if x >= settings.MAX_REQUESTS: break return render_to_response('requests.html', { 'manager': manager, 'request_type': request_type, 'page_name': page_name, 'request_form': request_form, 'requests_dict': requests_dict, 'relevant_managers': relevant_managers, }, context_instance=RequestContext(request))
def requests_view(request, requestType): ''' Generic request view. Parameters: request is the HTTP request requestType is URL name of a RequestType. e.g. "food", "maintenance", "network", "site" ''' userProfile = UserProfile.objects.get(user=request.user) request_type = get_object_or_404(RequestType, url_name=requestType) page_name = "%s Requests" % request_type.name.title() if not request_type.enabled: message = "%s requests have been disabled." % request_type.name.title() return red_home(request, message) relevant_managers = request_type.managers.filter(active=True) manager = any(i.incumbent == userProfile for i in relevant_managers) request_form = RequestForm( request.POST if 'submit_request' in request.POST else None, profile=userProfile, request_type=request_type, ) if manager: form_class = ManagerResponseForm else: form_class = ResponseForm response_form = form_class( request.POST if 'add_response' in request.POST else None, profile=userProfile, ) vote_form = VoteForm( request.POST if 'upvote' in request.POST else None, profile=userProfile, ) if request_form.is_valid(): request_form.save() return HttpResponseRedirect(reverse('requests', kwargs={'requestType': requestType})) if response_form.is_valid(): response_form.save() return HttpResponseRedirect(reverse('requests', kwargs={'requestType': requestType})) if vote_form.is_valid(): vote_form.save() return HttpResponseRedirect(reverse('requests', kwargs={'requestType': requestType})) x = 0 # number of requests loaded requests_dict = list() # A pseudo-dictionary, actually a list with items of form (request, [request_responses_list], response_form, upvote, vote_form) for req in Request.objects.filter(request_type=request_type): request_responses = Response.objects.filter(request=req) if manager: resp_form = ManagerResponseForm( initial={ 'request_pk': req.pk, 'mark_filled': req.filled, 'mark_closed': req.closed, }, profile=userProfile, ) else: resp_form = ResponseForm( initial={'request_pk': req.pk}, profile=userProfile, ) upvote = userProfile in req.upvotes.all() vote_form = VoteForm( initial={'request_pk': req.pk}, profile=userProfile, ) requests_dict.append((req, request_responses, resp_form, upvote, vote_form)) x += 1 if x >= settings.MAX_REQUESTS: break return render_to_response('requests.html', { 'manager': manager, 'request_type': request_type.name.title(), 'page_name': page_name, 'request_form': request_form, 'requests_dict': requests_dict, 'relevant_managers': relevant_managers, }, context_instance=RequestContext(request))