def add_email(request): context = _create_profile_context(request) context["active_tab"] = "email" if request.method == "POST": form = NewEmailForm(request.POST) if form.is_valid(): cleaned = form.cleaned_data email_string = cleaned["new_email"].lower() # Check if the email already exists if Email.objects.filter(email=cleaned["new_email"]).count() > 0: messages.error( request, _("Eposten %s er allerede registrert.") % email_string ) return redirect("profiles") # Check if it's studmail and if someone else already has it in their profile if re.match(r"[^@]+@stud\.ntnu\.no", email_string): ntnu_username = email_string.split("@")[0] user = User.objects.filter(ntnu_username=ntnu_username) if user.count() == 1: if user[0] != request.user: messages.error( request, _( "En bruker med dette NTNU-brukernavnet eksisterer allerede." ), ) return redirect("profiles") # Create the email email = Email(email=email_string, user=request.user) email.save() # Send the verification mail _send_verification_mail(request, email.email) messages.success( request, _("Eposten ble lagret. Du må sjekke din innboks for å verifisere den."), ) return render(request, "profiles/index.html", context)
def _create_profile_context(request): groups = Group.objects.all() Privacy.objects.get_or_create(user=request.user) # This is a hack """ To make sure a privacy exists when visiting /profiles/privacy/. Until now, it has been generated upon loading models.py, which is a bit hacky. The code is refactored to use Django signals, so whenever a user is created, a privacy-property is set up. """ if request.user.is_staff and not request.user.online_mail: create_online_mail_alias(request.user) context = { # edit "position_form": PositionForm(), "user_profile_form": ProfileForm(instance=request.user), # positions "groups": groups, # privacy "privacy_form": PrivacyForm(instance=request.user.privacy), # nibble information "transactions": PaymentTransaction.objects.filter(user=request.user), "orders": Order.objects.filter(order_line__user=request.user).order_by( "-order_line__datetime" ), # SSO / OAuth2 approved apps "connected_apps": AccessToken.objects.filter( user=request.user, expires__gte=timezone.now() ).order_by("expires"), # marks "mark_rule_set": MarkRuleSet.get_current_rule_set(), "mark_rules_accepted": request.user.mark_rules_accepted, "marks": [ # Tuple syntax ('title', list_of_marks, is_collapsed) (_("aktive prikker"), Mark.marks.active(request.user), False), (_("inaktive prikker"), Mark.marks.inactive(request.user), True), ], "suspensions": [ # Tuple syntax ('title', list_of_marks, is_collapsed) ( _("aktive suspensjoner"), Suspension.objects.filter(user=request.user, active=True), False, ), ( _("inaktive suspensjoner"), Suspension.objects.filter(user=request.user, active=False), True, ), ], # password "password_change_form": PasswordChangeForm(request.user), # email "new_email": NewEmailForm(), # approvals "field_of_study_application": FieldOfStudyApplicationForm(), "has_active_approvals": MembershipApproval.objects.filter( applicant=request.user, processed=False ).count() > 0, "approvals": [ # Tuple syntax ('title', list_of_approvals, is_collapsed) ( _("aktive søknader"), MembershipApproval.objects.filter( applicant=request.user, processed=False ), False, ), ( _("avslåtte søknader"), MembershipApproval.objects.filter( applicant=request.user, processed=True, approved=False ), True, ), ( _("godkjente søknader"), MembershipApproval.objects.filter( applicant=request.user, processed=True ), True, ), ], "payments": [ ( _("ubetalt"), PaymentDelay.objects.all().filter(user=request.user, active=True), False, ), ( _("betalt"), PaymentRelation.objects.all().filter(user=request.user), True, ), ], "internal_services_form": InternalServicesForm(), "in_comittee": has_access(request), "enable_dataporten_application": settings.DATAPORTEN.get("STUDY").get("ENABLED") or settings.DATAPORTEN.get("STUDY").get("TESTING"), } return context
def _create_profile_context(request): groups = Group.objects.all() Privacy.objects.get_or_create(user=request.user) # This is a hack """ To make sure a privacy exists when visiting /profiles/privacy/. Until now, it has been generated upon loading models.py, which is a bit hacky. The code is refactored to use Django signals, so whenever a user is created, a privacy-property is set up. """ if request.user.is_staff and not request.user.online_mail: create_online_mail_alias(request.user) context = { # edit 'position_form': PositionForm(), 'user_profile_form': ProfileForm(instance=request.user), # positions 'groups': groups, # privacy 'privacy_form': PrivacyForm(instance=request.user.privacy), # nibble information 'transactions': PaymentTransaction.objects.filter(user=request.user), 'orders': Order.objects.filter( order_line__user=request.user).order_by('-order_line__datetime'), # SSO / OAuth2 approved apps 'connected_apps': AccessToken.objects.filter( user=request.user, expires__gte=timezone.now()).order_by('expires'), # marks 'mark_rules_accepted': request.user.mark_rules, 'marks': [ # Tuple syntax ('title', list_of_marks, is_collapsed) (_('aktive prikker'), Mark.marks.active(request.user), False), (_('inaktive prikker'), Mark.marks.inactive(request.user), True), ], 'suspensions': [ # Tuple syntax ('title', list_of_marks, is_collapsed) (_('aktive suspensjoner'), Suspension.objects.filter(user=request.user, active=True), False), (_('inaktive suspensjoner'), Suspension.objects.filter(user=request.user, active=False), True), ], # password 'password_change_form': PasswordChangeForm(request.user), # email 'new_email': NewEmailForm(), # approvals 'field_of_study_application': FieldOfStudyApplicationForm(), 'has_active_approvals': MembershipApproval.objects.filter(applicant=request.user, processed=False).count() > 0, 'approvals': [ # Tuple syntax ('title', list_of_approvals, is_collapsed) (_("aktive søknader"), MembershipApproval.objects.filter(applicant=request.user, processed=False), False), (_("avslåtte søknader"), MembershipApproval.objects.filter(applicant=request.user, processed=True, approved=False), True), (_("godkjente søknader"), MembershipApproval.objects.filter(applicant=request.user, processed=True), True), ], 'payments': [ (_('ubetalt'), PaymentDelay.objects.all().filter(user=request.user, active=True), False), (_('betalt'), PaymentRelation.objects.all().filter(user=request.user), True), ], 'internal_services_form': InternalServicesForm(), 'in_comittee': has_access(request) } return context