def activate(request, activation_key, template_name='userena/activate_fail.html', extra_context=None): """ Activate a user with an activation key. """ user = UserenaSignup.objects.activate_user(activation_key) if user: # Sign the user in. auth_user = authenticate(identification=user.username, check_password=False) UserDetail.set_activation_datetime(auth_user) login(request, auth_user) if userena_settings.USERENA_USE_MESSAGES: messages.success(request, _('Congratulations -- your Art of Gratitude account is confirmed!'), fail_silently=True) if request.method == 'POST': form = ProfileForm(request.POST, user=user) if form.is_valid(): form.save() redirect_to = settings.LOGIN_REDIRECT_URL % {'username': user.username } return redirect(redirect_to) else: if not extra_context: extra_context = {} return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request)
def user_tribes_list(request): """ 列出当前登陆用户所有拥有进入权限的tribe """ can_entered_tribes = get_objects_for_user(request.user,'studygroup.enter_studytribe') extra_context = {'can_entered_tribes':can_entered_tribes} return ExtraContextTemplateView.as_view(template_name="studytribe/tribemember/studytribe_list.html", extra_context=extra_context)(request)
def profile_detail_extended(request, username, edit_profile_form=EditProfileForm, template_name='userena/profile_detail_extended.html', success_url=None,extra_context=None, **kwargs): user = get_object_or_404(get_user_model(), username__iexact=username) profile = get_user_profile(user=user) if not extra_context: extra_context = dict() extra_context['profile'] = profile extra_context['profExpData'] = Profile_Experience.objects.filter(profile = profile) extra_context['projExpData'] = Project_Experience.objects.filter(profile = profile) extra_context['awardsData'] = Awards.objects.filter(profile = profile) return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request)
def profile_detail( request, username, template_name=userena_settings.USERENA_PROFILE_DETAIL_TEMPLATE, extra_context=None, **kwargs): """ Detailed view of an user. :param username: String of the username of which the profile should be viewed. :param template_name: String representing the template name that should be used to display the profile. :param extra_context: Dictionary of variables which should be supplied to the template. The ``profile`` key is always the current profile. **Context** ``profile`` Instance of the currently viewed ``Profile``. """ user = get_object_or_404(get_user_model(), username__iexact=username) profile_model = get_profile_model() try: profile = user.get_profile() except profile_model.DoesNotExist: profile = profile_model.objects.create(user=user) if not profile.can_view_profile(request.user): return HttpResponseForbidden( _("You don't have permission to view this profile.")) if not extra_context: extra_context = dict() extra_context['profile'] = user.get_profile() extra_context['hide_email'] = userena_settings.USERENA_HIDE_EMAIL extra_context['location'] = request.user_location["user_location_lat_lon"] extra_context['is_admin'] = user.is_superuser extra_context['per_page'] = int(request.GET.get('per_page', 6)) tabs_page = "profile-detail" active_tab = request.session.get(tabs_page, "account-events") extra_context['active_tab'] = active_tab return ExtraContextTemplateView.as_view( template_name=template_name, extra_context=extra_context)(request)
def signup(request, signup_form=SignupFormBeta, template_name='userena/signup_form.html', success_url=None, extra_context=None): form = SignupFormBeta() if request.method == 'POST': # If the form has been submitted... form = SignupFormBeta(request.POST, request.FILES) if request.POST.get('beta_access') == 'noisetracker': return userena_views.signup(request, signup_form) if not extra_context: extra_context = dict() extra_context['form'] = form return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request)
def profile_detail(request, username, template_name=userena_settings.USERENA_PROFILE_DETAIL_TEMPLATE, extra_context=None, **kwargs): """ Detailed view of an user. :param username: String of the username of which the profile should be viewed. :param template_name: String representing the template name that should be used to display the profile. :param extra_context: Dictionary of variables which should be supplied to the template. The ``profile`` key is always the current profile. **Context** ``profile`` Instance of the currently viewed ``Profile``. """ user = get_object_or_404(get_user_model(), username__iexact=username) profile_model = get_profile_model() try: profile = user.get_profile() except profile_model.DoesNotExist: profile = profile_model.objects.create(user=user) if not profile.can_view_profile(request.user): return HttpResponseForbidden(_("You don't have permission to view this profile.")) if not extra_context: extra_context = dict() extra_context['profile'] = user.get_profile() extra_context['hide_email'] = userena_settings.USERENA_HIDE_EMAIL extra_context['location'] = request.user_location["user_location_lat_lon"] extra_context['is_admin'] = user.is_superuser extra_context['per_page'] = int(request.GET.get('per_page', 6)) tabs_page = "profile-detail" active_tab = request.session.get(tabs_page, "account-events") extra_context['active_tab'] = active_tab return ExtraContextTemplateView.as_view( template_name=template_name, extra_context=extra_context )(request)
def personal_email_confirm(request, confirmation_key, template_name='userena/email_confirm_fail.html', success_url=None, extra_context=None): """ Confirms an email address with a confirmation key. Confirms a new email address by running :func:`User.objects.confirm_email` method. If the method returns an :class:`User` the user will have his new e-mail address set and redirected to ``success_url``. If no ``User`` is returned the user will be represented with a fail message from ``template_name``. :param confirmation_key: String with a SHA1 representing the confirmation key used to verify a new email address. :param template_name: String containing the template name which should be rendered when confirmation fails. When confirmation is successful, no template is needed because the user will be redirected to ``success_url``. :param success_url: String containing the URL which is redirected to after a successful confirmation. Supplied argument must be able to be rendered by ``reverse`` function. :param extra_context: Dictionary of variables that are passed on to the template supplied by ``template_name``. """ user = Profile.objects.confirm_personal_email(confirmation_key) if user: if userena_settings.USERENA_USE_MESSAGES: messages.success(request, 'Your email address has been changed.', fail_silently=True) if success_url: redirect_to = success_url else: redirect_to = reverse('userena_email_confirm_complete', kwargs={'username': user.username}) return redirect(redirect_to) else: if not extra_context: extra_context = dict() return ExtraContextTemplateView.as_view( template_name=template_name, extra_context=extra_context)(request)
def _friends(request,username, template_name='friends.html', success_url=None, extra_context=None): user = get_object_or_404(get_user_model(), username__iexact=username) profile = user.get_profile() user_initial = {'first_name': user.first_name, 'last_name': user.last_name} data = locals() if not extra_context: extra_context = dict() #extra_context['ftype'] = ftype extra_context['profile'] = profile #extra_context['user'] = user #more_data = {'username':username,'ftype':ftype} #return direct_to_template(template = 'friends.html', kwargs=kwargs) return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request)
def signup(request, signup_form=SignupForm, template_name='userena/signup_form.html', success_url=None, extra_context=None): # If signup is disabled, return 403 if userena_settings.USERENA_DISABLE_SIGNUP: raise PermissionDenied # If no usernames are wanted and the default form is used, fallback to the # default form that doesn't display to enter the username. if userena_settings.USERENA_WITHOUT_USERNAMES and (signup_form == SignupForm): signup_form = SignupFormOnlyEmail form = signup_form() if request.method == 'POST': form = signup_form(request.POST, request.FILES) if form.is_valid(): user = form.save() new_profile = MyProfile(user=user) # Send the signup complete signal userena_signals.signup_complete.send(sender=None, user=user) if success_url: redirect_to = success_url else: redirect_to = reverse('userena_signup_complete', kwargs={'username': user.username}) # A new signed user should logout the old one. if request.user.is_authenticated(): logout(request) if (userena_settings.USERENA_SIGNIN_AFTER_SIGNUP and not userena_settings.USERENA_ACTIVATION_REQUIRED): user = authenticate(identification=user.email, check_password=False) login(request, user) return redirect(redirect_to) if not extra_context: extra_context = dict() extra_context['form'] = form return ExtraContextTemplateView.as_view( template_name=template_name, extra_context=extra_context)(request)
def profile_listview( request, username, template_name=userena_settings.USERENA_PROFILE_DETAIL_TEMPLATE, extra_context=None, **kwargs): """ note: 'extra_context' is a dictionary of variables which should be supplied to the template. """ user = get_object_or_404(get_user_model(), username__iexact=username) fullsalelist = Entry.objects.filter( author__username__iexact=username).order_by('-pub_date') userreviews = UserReview.objects.filter( name__username__iexact=username).order_by('-pub_date') if request.is_ajax(): object_name = request.POST.get('entryname') targetobject = Entry.objects.get(headline=object_name) if request.user.username == targetobject.author.username: targetobject.delete() return HttpResponseRedirect('/storefront/') profile_model = get_profile_model() try: profile = user.my_profile except profile_model.DoesNotExist: profile = profile_model.objects.create(user=user) if not profile.can_view_profile(request.user): raise PermissionDenied if not extra_context: extra_context = dict() if username == request.user.username: pageowner = "True" extra_context['pageowner'] = pageowner extra_context['profile'] = user.my_profile extra_context['fullsalelist'] = fullsalelist extra_context['userreviews'] = userreviews extra_context['hide_email'] = userena_settings.USERENA_HIDE_EMAIL return ExtraContextTemplateView.as_view( template_name='profile_listview.html', extra_context=extra_context)(request)
def profile_detail( request, username, template_name=userena_settings.USERENA_PROFILE_DETAIL_TEMPLATE, extra_context=None, **kwargs): """ Detailed view of an user. :param username: String of the username of which the profile should be viewed. :param template_name: String representing the template name that should be used to display the profile. :param extra_context: Dictionary of variables which should be supplied to the template. The ``profile`` key is always the current profile. **Context** ``profile`` Instance of the currently viewed ``Profile``. """ user = get_object_or_404(get_user_model(), username__iexact=username) current_tasks = Task.objects.filter(edition=Edition.get_current) profile_model = get_profile_model() try: profile = user.get_profile() except profile_model.DoesNotExist: profile = profile_model.objects.create(user=user) if not profile.can_view_profile(request.user): raise PermissionDenied if not extra_context: extra_context = dict() extra_context['profile'] = user.get_profile() extra_context['tasks'] = current_tasks.filter(volunteers__user=user) extra_context['hide_email'] = userena_settings.USERENA_HIDE_EMAIL check_profile_completeness(request, user.get_profile()) return ExtraContextTemplateView.as_view( template_name=template_name, extra_context=extra_context)(request)
def signin(request, auth_form=AuthenticationForm, template_name='studytribe/tribemember/signin.html', redirect_field_name=REDIRECT_FIELD_NAME, redirect_signin_function=signin_redirect, formname=SIGNIN_FORM_NAME, extra_context=None): form = auth_form if request.method == 'POST': form = auth_form(request.POST, request.FILES) if form.is_valid(): identification, password, remember_me = (form.cleaned_data['identification'], form.cleaned_data['password'], form.cleaned_data['remember_me']) user = authenticate(identification=identification, password=password) if user.is_active: login(request, user) if remember_me: request.session.set_expiry(userena_settings.USERENA_REMEMBER_ME_DAYS[1] * 86400) else: request.session.set_expiry(0) if userena_settings.USERENA_USE_MESSAGES: messages.success(request, _('You have been signed in.'), fail_silently=True) # Whereto now? redirect_to = redirect_signin_function( request.REQUEST.get(redirect_field_name), user) return redirect(redirect_to) else: return redirect(reverse('userena_disabled', kwargs={'username': user.username})) if not extra_context: extra_context = dict() extra_context.update({ formname: form, 'next': request.REQUEST.get(redirect_field_name), }) return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request)
def signup(request, signup_form=SignupForm, template_name='userena/signup_form.html', success_url=None, extra_context=None): # If signup is disabled, return 403 if userena_settings.USERENA_DISABLE_SIGNUP: raise PermissionDenied # If no usernames are wanted and the default form is used, fallback to the # default form that doesn't display to enter the username. if userena_settings.USERENA_WITHOUT_USERNAMES and (signup_form == SignupForm): signup_form = SignupFormOnlyEmail form = signup_form() if request.method == 'POST': form = signup_form(request.POST, request.FILES) if form.is_valid(): user = form.save() new_profile = MyProfile(user=user) # Send the signup complete signal userena_signals.signup_complete.send(sender=None, user=user) if success_url: redirect_to = success_url else: redirect_to = reverse('userena_signup_complete', kwargs={'username': user.username}) # A new signed user should logout the old one. if request.user.is_authenticated(): logout(request) if (userena_settings.USERENA_SIGNIN_AFTER_SIGNUP and not userena_settings.USERENA_ACTIVATION_REQUIRED): user = authenticate(identification=user.email, check_password=False) login(request, user) return redirect(redirect_to) if not extra_context: extra_context = dict() extra_context['form'] = form return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request)
def signup(request, signup_form=SignupForm, template_name='studytribe/tribemember/signup.html', success_url=None, formname=SIGNUP_FORM_NAME, extra_context=None): """ 几乎原样照抄userena的signup和login,view的代码, 增加了activate_required用来动态指定是否需要激活 加上可以定制form在context中名字的代码。 因为在首页当中需要在一个页面中打印两个form表单, 分别是loginform和signupform; """ if userena_settings.USERENA_WITHOUT_USERNAMES and (signup_form == SignupForm): signup_form = SignupFormOnlyEmail form = signup_form() if request.method == 'POST': form = signup_form(request.POST, request.FILES) if form.is_valid(): user = form.save() # Send the signup complete signal userena_signals.signup_complete.send(sender=None, user=user) if success_url: redirect_to = success_url else: redirect_to = reverse('userena_signup_complete', kwargs={'username': user.username}) # A new signed user should logout the old one. if request.user.is_authenticated(): logout(request) return redirect(redirect_to) if not extra_context: extra_context = dict() extra_context[formname] = form return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request)
def profile_listview(request, username, template_name=userena_settings.USERENA_PROFILE_DETAIL_TEMPLATE, extra_context=None, **kwargs): """ note: 'extra_context' is a dictionary of variables which should be supplied to the template. """ user = get_object_or_404(get_user_model(), username__iexact=username) fullsalelist = Entry.objects.filter(author__username__iexact=username).order_by('-pub_date') userreviews = UserReview.objects.filter(name__username__iexact=username).order_by('-pub_date') if request.is_ajax(): object_name = request.POST.get('entryname') targetobject = Entry.objects.get(headline=object_name) if request.user.username == targetobject.author.username: targetobject.delete() return HttpResponseRedirect('/storefront/') profile_model = get_profile_model() try: profile = user.get_profile() except profile_model.DoesNotExist: profile = profile_model.objects.create(user=user) if not profile.can_view_profile(request.user): raise PermissionDenied if not extra_context: extra_context = dict() if username == request.user.username: pageowner="True" extra_context['pageowner'] = pageowner extra_context['profile'] = user.get_profile() extra_context['fullsalelist'] = fullsalelist extra_context['userreviews'] = userreviews extra_context['hide_email'] = userena_settings.USERENA_HIDE_EMAIL return ExtraContextTemplateView.as_view(template_name='profile_listview.html', extra_context=extra_context)(request)
def signin(request, auth_form=RememberMeAuthenticationForm, template_name='gratitude/signin.html', redirect_field_name=REDIRECT_FIELD_NAME, redirect_signin_function=signin_redirect, extra_context=None): form = auth_form(error_class=AlertErrorList) if request.method == 'POST': form = auth_form(request.POST, request.FILES, error_class=AlertErrorList) if form.is_valid(): identification, password, remember_me = (form.cleaned_data['identification'], form.cleaned_data['password'], form.cleaned_data['remember_me']) user = authenticate(identification=identification, password=password) if user.is_active: login(request, user) if remember_me: request.session.set_expiry(userena_settings.USERENA_REMEMBER_ME_DAYS[1] * 86400) else: request.session.set_expiry(0) if userena_settings.USERENA_USE_MESSAGES: messages.success(request, _('You have been signed in.'), fail_silently=True) save_stashed_gratitudes(request, user) return redirect(reverse('gratitude_profile_simple')) else: return redirect(reverse('userena_disabled', kwargs={'username': user.username})) if not extra_context: extra_context = dict() extra_context.update({ 'form': form, 'next': request.REQUEST.get(redirect_field_name), }) return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request)
def signin(request, auth_form=AuthenticationForm): form = auth_form if request.method == 'POST': form = auth_form(request.POST, request.FILES) if form.is_valid(): identification, password, remember_me = \ (form.cleaned_data['identification'], form.cleaned_data['password'], form.cleaned_data['remember_me']) user = authenticate(identification=identification, password=password) if user.is_active: login(request, user) if remember_me: request.session.set_expiry(7 * 86400) else: request.session.set_expiry(0) return redirect(reverse('user-home')) else: return redirect(reverse('userena_disabled', kwargs={'username': user.username})) else: print 'Invalid form!' return ExtraContextTemplateView.as_view(template_name='login-failed.html')(request) return None
def profile_edit(request, username, edit_profile_form=EditProfileForm, template_name='userena/profile_form.html', success_url=None, extra_context=None, **kwargs): """ Edit profile. Edits a profile selected by the supplied username. First checks permissions if the user is allowed to edit this profile, if denied will show a 404. When the profile is successfully edited will redirect to ``success_url``. :param username: Username of the user which profile should be edited. :param edit_profile_form: Form that is used to edit the profile. The :func:`EditProfileForm.save` method of this form will be called when the form :func:`EditProfileForm.is_valid`. Defaults to :class:`EditProfileForm` from userena. :param template_name: String of the template that is used to render this view. Defaults to ``userena/edit_profile_form.html``. :param success_url: Named URL which will be passed on to a django ``reverse`` function after the form is successfully saved. Defaults to the ``userena_detail`` url. :param extra_context: Dictionary containing variables that are passed on to the ``template_name`` template. ``form`` key will always be the form used to edit the profile, and the ``profile`` key is always the edited profile. **Context** ``form`` Form that is used to alter the profile. ``profile`` Instance of the ``Profile`` that is edited. """ user = get_object_or_404(get_user_model(), username__iexact=username) profile = user.get_profile() user_initial = {'first_name': user.first_name, 'last_name': user.last_name} form = edit_profile_form(instance=profile, initial=user_initial) if request.method == 'POST': form = edit_profile_form(request.POST, request.FILES, instance=profile, initial=user_initial) if form.is_valid(): profile = form.save(commit=False) profile.save() if userena_settings.USERENA_USE_MESSAGES: messages.success(request, _('Your profile has been updated.'), fail_silently=True) if success_url: # Send a signal that the profile has changed userena_signals.profile_change.send(sender=None, user=user) redirect_to = success_url else: redirect_to = reverse('userena_profile_detail', kwargs={'username': username}) return redirect(redirect_to) if not extra_context: extra_context = dict() extra_context['form'] = form extra_context['profile'] = profile return ExtraContextTemplateView.as_view( template_name=template_name, extra_context=extra_context)(request)
def signup(request, signup_form=SignupForm, template_name='userena/signup_form.html', success_url=None, extra_context=None): """ Signup of an account. Signup requiring a username, email and password. After signup a user gets an email with an activation link used to activate their account. After successful signup redirects to ``success_url``. :param signup_form: Form that will be used to sign a user. Defaults to userena's :class:`SignupForm`. :param template_name: String containing the template name that will be used to display the signup form. Defaults to ``userena/signup_form.html``. :param success_url: String containing the URI which should be redirected to after a successful signup. If not supplied will redirect to ``userena_signup_complete`` view. :param extra_context: Dictionary containing variables which are added to the template context. Defaults to a dictionary with a ``form`` key containing the ``signup_form``. **Context** ``form`` Form supplied by ``signup_form``. """ # If signup is disabled, return 403 if userena_settings.USERENA_DISABLE_SIGNUP: raise PermissionDenied # If no usernames are wanted and the default form is used, fallback to the # default form that doesn't display to enter the username. if (userena_settings.USERENA_WITHOUT_USERNAMES and (signup_form == SignupForm)): signup_form = SignupFormOnlyEmail form = signup_form() if request.method == 'POST': form = signup_form(request.POST, request.FILES) if form.is_valid(): user = form.save() # Send the signup complete signal userena_signals.signup_complete.send(sender=None, user=user) if success_url: redirect_to = success_url else: redirect_to = reverse('userena_signup_complete', kwargs={'username': user.username}) # A new signed user should logout the old one. if request.user.is_authenticated(): logout(request) if (userena_settings.USERENA_SIGNIN_AFTER_SIGNUP and not userena_settings.USERENA_ACTIVATION_REQUIRED): user = authenticate(identification=user.email, check_password=False) login(request, user) return redirect(redirect_to) if not extra_context: extra_context = dict() extra_context['form'] = form return ExtraContextTemplateView.as_view( template_name=template_name, extra_context=extra_context)(request)
def profile_edit(request, username, edit_profile_form=AccountForm, template_name='userena/profile_form.html', success_url=None, why_message=None, extra_context=None, **kwargs): """ Edit profile. Edits a profile selected by the supplied username. First checks permissions if the user is allowed to edit this profile, if denied will show a 404. When the profile is successfully edited will redirect to ``success_url``. :param username: Username of the user which profile should be edited. :param edit_profile_form: Form that is used to edit the profile. The :func:`EditProfileForm.save` method of this form will be called when the form :func:`EditProfileForm.is_valid`. Defaults to :class:`EditProfileForm` from userena. :param template_name: String of the template that is used to render this view. Defaults to ``userena/edit_profile_form.html``. :param success_url: Named URL which will be passed on to a django ``reverse`` function after the form is successfully saved. Defaults to the ``userena_detail`` url. :param extra_context: Dictionary containing variables that are passed on to the ``template_name`` template. ``form`` key will always be the form used to edit the profile, and the ``profile`` key is always the edited profile. **Context** ``form`` Form that is used to alter the profile. ``profile`` Instance of the ``Profile`` that is edited. """ user = get_object_or_404(get_user_model(), username__iexact=username) profile = user.get_profile() user_initial = { 'first_name': user.first_name, 'last_name': user.last_name, 'not_from_canada': profile.not_from_canada or not request.user_location['is_canada'], 'native_region': profile.native_region or request.user_location['user_location_region'] } form = edit_profile_form(instance=profile, initial=user_initial) if request.method == 'POST': form = edit_profile_form(request.POST, request.FILES, instance=profile, initial=user_initial) if form.is_valid(): profile = form.save() if profile.not_from_canada or profile.native_region: profile.tax_origin_confirmed = True else: profile.tax_origin_confirmed = False profile.save() if success_url: redirect_to = success_url # Fix strange bug on production redirect_to = re.sub(r'http:\/([^\/])', r'http://\1', redirect_to) else: redirect_to = reverse('userena_profile_detail', kwargs={'username': username}) return redirect(redirect_to) if not extra_context: extra_context = dict() extra_context['form'] = form extra_context['profile'] = profile extra_context['why_message'] = why_message return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request)
def profile_edit(request, username, edit_profile_form=EditProfileForm, template_name='userena/profile_form.html', success_url=None, extra_context=None, **kwargs): """ Edit profile. Edits a profile selected by the supplied username. First checks permissions if the user is allowed to edit this profile, if denied will show a 404. When the profile is successfully edited will redirect to ``success_url``. :param username: Username of the user which profile should be edited. :param edit_profile_form: Form that is used to edit the profile. The :func:`EditProfileForm.save` method of this form will be called when the form :func:`EditProfileForm.is_valid`. Defaults to :class:`EditProfileForm` from userena. :param template_name: String of the template that is used to render this view. Defaults to ``userena/edit_profile_form.html``. :param success_url: Named URL which will be passed on to a django ``reverse`` function after the form is successfully saved. Defaults to the ``userena_detail`` url. :param extra_context: Dictionary containing variables that are passed on to the ``template_name`` template. ``form`` key will always be the form used to edit the profile, and the ``profile`` key is always the edited profile. **Context** ``form`` Form that is used to alter the profile. ``profile`` Instance of the ``Profile`` that is edited. """ user = get_object_or_404(get_user_model(), username__iexact=username) profile = user.get_profile() user_initial = {'first_name': user.first_name, 'last_name': user.last_name} form = edit_profile_form(instance=profile, initial=user_initial) if request.method == 'POST': form = edit_profile_form(request.POST, request.FILES, instance=profile, initial=user_initial) if form.is_valid(): profile = form.save(commit=False) profile.save() # go trough all the task categories for this volunteer for category in TaskCategory.objects.all(): exists = VolunteerCategory.objects.filter(volunteer=profile, category=category) selected = form.cleaned_data.get('categories').filter(name=category.name) # when the category does not exist and was selected, add it if not exists and selected: profilecategory = VolunteerCategory(volunteer=profile, category=category) profilecategory.save() # when the category exists and was deselected, delete it elif exists and not selected: profilecategory = VolunteerCategory.objects.filter(volunteer=profile, category=category) profilecategory.delete() if userena_settings.USERENA_USE_MESSAGES: messages.success(request, _('Your profile has been updated.'), fail_silently=True) if success_url: # Send a signal that the profile has changed userena_signals.profile_change.send(sender=None, user=user) redirect_to = success_url else: redirect_to = reverse('userena_profile_detail', kwargs={'username': username}) return redirect(redirect_to) if not extra_context: extra_context = dict() extra_context['form'] = form extra_context['profile'] = profile return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request)
def message_compose(request, recipients=None, compose_form=ComposeForm, success_url=None, template_name="umessages/message_form.html", recipient_filter=None, extra_context=None): """ Compose a new message :recipients: String containing the usernames to whom the message is send to. Can be multiple username by seperating them with a ``+`` sign. :param compose_form: The form that is used for getting neccesary information. Defaults to :class:`ComposeForm`. :param success_url: String containing the named url which to redirect to after successfull sending a message. Defaults to ``userena_umessages_list`` if there are multiple recipients. If there is only one recipient, will redirect to ``userena_umessages_detail`` page, showing the conversation. :param template_name: String containing the name of the template that is used. :param recipient_filter: A list of :class:`User` that don"t want to receive any messages. :param extra_context: Dictionary with extra variables supplied to the template. **Context** ``form`` The form that is used. """ initial_data = dict() if recipients: username_list = [r.strip() for r in recipients.split("+")] recipients = [u for u in User.objects.filter(username__in=username_list)] initial_data["to"] = recipients form = compose_form(initial=initial_data) if request.method == "POST": form = compose_form(request.POST) if form.is_valid(): requested_redirect = request.REQUEST.get("next", False) message = form.save(request.user) recipients = form.cleaned_data['to'] if userena_settings.USERENA_USE_MESSAGES: messages.success(request, _('Message is sent.'), fail_silently=True) requested_redirect = request.REQUEST.get(REDIRECT_FIELD_NAME, False) # Redirect mechanism redirect_to = reverse('userena_umessages_list') if requested_redirect: redirect_to = requested_redirect elif success_url: redirect_to = success_url elif len(recipients) == 1: redirect_to = reverse('userena_umessages_detail', kwargs={'username': recipients[0].username}) return redirect(redirect_to) if not extra_context: extra_context = dict() extra_context["form"] = form extra_context["recipients"] = recipients return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request)
def profile_edit_extended(request, username, edit_profile_form=EditProfileForm, template_name='userena/profile_form_extended.html', success_url=None,extra_context=None, **kwargs): """ Edit profile. Edits a profile selected by the supplied username. First checks permissions if the user is allowed to edit this profile, if denied will show a 404. When the profile is successfully edited will redirect to ``success_url``. :param username: Username of the user which profile should be edited. :param edit_profile_form: Form that is used to edit the profile. The :func:`EditProfileForm.save` method of this form will be called when the form :func:`EditProfileForm.is_valid`. Defaults to :class:`EditProfileForm` from userena. :param template_name: String of the template that is used to render this view. Defaults to ``userena/edit_profile_form.html``. :param success_url: Named URL which will be passed on to a django ``reverse`` function after the form is successfully saved. Defaults to the ``userena_detail`` url. :param extra_context: Dictionary containing variables that are passed on to the ``template_name`` template. ``form`` key will always be the form used to edit the profile, and the ``profile`` key is always the edited profile. **Context** ``form`` Form that is used to alter the profile. ``profile`` Instance of the ``Profile`` that is edited. """ user = get_object_or_404(get_user_model(), username__iexact=username) profile = get_user_profile(user=user) user_initial = {'first_name': user.first_name, 'last_name': user.last_name} form = edit_profile_form(instance=profile, initial=user_initial) #FormSetProfExp = formset_factory(ProfExpForm) #FormSetProjExp = formset_factory(ProjExpForm) #FormSetAwards = formset_factory(AwardsForm, can_delete = True) FormSetProfExp = modelformset_factory(Profile_Experience, exclude=('profile',), can_delete = True, widgets = {'jobstart':SelectDateWidget(),'jobend':SelectDateWidget() }) FormSetProjExp = modelformset_factory(Project_Experience, exclude=('profile',), can_delete = True,widgets = {'projectstart':SelectDateWidget(),'projectend':SelectDateWidget() }) FormSetAwards = modelformset_factory(Awards, exclude=('profile',), can_delete = True) if request.method == 'POST': formSetProfExp = FormSetProfExp(request.POST) formSetProjExp = FormSetProjExp(request.POST) formSetAwards = FormSetAwards(request.POST) ''' for form in formSetProfExp: if form.is_valid(): profExp = form.save(commit = False) profExp.profile = profile try: profExp.save() except IntegrityError: pass for form in formSetProjExp: if form.is_valid(): projExp = form.save(commit = False) projExp.profile = profile try: projExp.save() except IntegrityError: pass ''' if formSetProfExp.is_valid(): formSetProfExp.save(commit = False) for anobject in formSetProfExp.new_objects: anobject.profile = profile formSetProfExp.save(commit = True) if formSetProjExp.is_valid(): formSetProjExp.save(commit = False) for anobject in formSetProjExp.new_objects: anobject.profile = profile formSetProjExp.save(commit = True) if formSetAwards.is_valid(): formSetAwards.save(commit = False) for anobject in formSetAwards.new_objects: anobject.profile = profile formSetAwards.save(commit = True) form = edit_profile_form(request.POST, request.FILES, instance=profile, initial=user_initial) if form.is_valid(): profile = form.save() if userena_settings.USERENA_USE_MESSAGES: messages.success(request, _('Your profile has been updated.'),fail_silently=True) if success_url: # Send a signal that the profile has changed userena_signals.profile_change.send(sender=None, user=user) redirect_to = success_url else: redirect_to = reverse('userena_profile_detail', kwargs={'username': username}) return redirect(redirect_to) if not extra_context: extra_context = dict() extra_context['form'] = form extra_context['profile'] = profile # here additional arguments, such as "extra", may be passed formSetProfExp = FormSetProfExp(queryset = Profile_Experience.objects.filter(profile = profile)) formSetProjExp = FormSetProjExp(queryset = Project_Experience.objects.filter(profile = profile)) formSetAwards = FormSetAwards(queryset = Awards.objects.filter(profile = profile)) extra_context['formSetProfExp'] = formSetProfExp extra_context['formSetProjExp'] = formSetProjExp extra_context['formSetAwards'] = formSetAwards return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request)
def profile_edit(request, username, edit_profile_form=AccountForm, template_name='userena/profile_form.html', success_url=None, why_message=None, extra_context=None, **kwargs): """ Edit profile. Edits a profile selected by the supplied username. First checks permissions if the user is allowed to edit this profile, if denied will show a 404. When the profile is successfully edited will redirect to ``success_url``. :param username: Username of the user which profile should be edited. :param edit_profile_form: Form that is used to edit the profile. The :func:`EditProfileForm.save` method of this form will be called when the form :func:`EditProfileForm.is_valid`. Defaults to :class:`EditProfileForm` from userena. :param template_name: String of the template that is used to render this view. Defaults to ``userena/edit_profile_form.html``. :param success_url: Named URL which will be passed on to a django ``reverse`` function after the form is successfully saved. Defaults to the ``userena_detail`` url. :param extra_context: Dictionary containing variables that are passed on to the ``template_name`` template. ``form`` key will always be the form used to edit the profile, and the ``profile`` key is always the edited profile. **Context** ``form`` Form that is used to alter the profile. ``profile`` Instance of the ``Profile`` that is edited. """ user = get_object_or_404(get_user_model(), username__iexact=username) profile = user.get_profile() user_initial = { 'first_name': user.first_name, 'last_name': user.last_name, 'not_from_canada': profile.not_from_canada or not request.user_location['is_canada'], 'native_region': profile.native_region or request.user_location['user_location_region'] } form = edit_profile_form(instance=profile, initial=user_initial) if request.method == 'POST': form = edit_profile_form(request.POST, request.FILES, instance=profile, initial=user_initial) if form.is_valid(): profile = form.save() if profile.not_from_canada or profile.native_region: profile.tax_origin_confirmed = True else: profile.tax_origin_confirmed = False profile.save() if success_url: redirect_to = success_url # Fix strange bug on production redirect_to = re.sub(r'http:\/([^\/])', r'http://\1', redirect_to) else: redirect_to = reverse('userena_profile_detail', kwargs={'username': username}) return redirect(redirect_to) if not extra_context: extra_context = dict() extra_context['form'] = form extra_context['profile'] = profile extra_context['why_message'] = why_message return ExtraContextTemplateView.as_view( template_name=template_name, extra_context=extra_context)(request)
def personal_email_change(request, username, email_form=ChangePersonalEmailForm, template_name='accounts/personal_email_form.html', success_url=None, extra_context=None): """ Change email address :param username: String of the username which specifies the current account. :param email_form: Form that will be used to change the email address. Defaults to :class:`ChangeEmailForm` supplied by userena. :param template_name: String containing the template to be used to display the email form. Defaults to ``userena/email_form.html``. :param success_url: Named URL where the user will get redirected to when successfully changing their email address. When not supplied will redirect to ``userena_email_complete`` URL. :param extra_context: Dictionary containing extra variables that can be used to render the template. The ``form`` key is always the form supplied by the keyword argument ``form`` and the ``user`` key by the user whose email address is being changed. **Context** ``form`` Form that is used to change the email address supplied by ``form``. ``account`` Instance of the ``Account`` whose email address is about to be changed. **Todo** Need to have per-object permissions, which enables users with the correct permissions to alter the email address of others. """ user = get_object_or_404(User, username__iexact=username) prev_email = user.profile.alternative_email form = email_form(user) if request.method == 'POST': form = email_form(user, request.POST, request.FILES) if form.is_valid(): form.save() if success_url: # Send a signal that the email has changed userena_signals.email_change.send( sender=None, user=user, prev_email=prev_email, new_email=user.profile.alternative_email) redirect_to = success_url else: redirect_to = reverse('userena_email_change_complete', kwargs={'username': user.username}) return redirect(redirect_to) if not extra_context: extra_context = dict() extra_context['form'] = form extra_context['profile'] = get_user_profile(user=user) return ExtraContextTemplateView.as_view( template_name=template_name, extra_context=extra_context)(request)
def signup(request, signup_form=SignupForm, template_name='userena/signup_form.html', success_url=None, extra_context=None): """ Signup of an account. Signup requiring a username, email and password. After signup a user gets an email with an activation link used to activate their account. After successful signup redirects to ``success_url``. :param signup_form: Form that will be used to sign a user. Defaults to userena's :class:`SignupForm`. :param template_name: String containing the template name that will be used to display the signup form. Defaults to ``userena/signup_form.html``. :param success_url: String containing the URI which should be redirected to after a successful signup. If not supplied will redirect to ``userena_signup_complete`` view. :param extra_context: Dictionary containing variables which are added to the template context. Defaults to a dictionary with a ``form`` key containing the ``signup_form``. **Context** ``form`` Form supplied by ``signup_form``. """ # If signup is disabled, return 403 if userena_settings.USERENA_DISABLE_SIGNUP: raise PermissionDenied # If no usernames are wanted and the default form is used, fallback to the # default form that doesn't display to enter the username. if userena_settings.USERENA_WITHOUT_USERNAMES and (signup_form == SignupForm): signup_form = SignupFormOnlyEmail form = signup_form() if request.method == 'POST': form = signup_form(request.POST, request.FILES) if form.is_valid(): user = form.save() # Send the signup complete signal userena_signals.signup_complete.send(sender=None, user=user) if success_url: redirect_to = success_url else: redirect_to = reverse('userena_signup_complete', kwargs={'username': user.username}) # A new signed user should logout the old one. if request.user.is_authenticated(): logout(request) if (userena_settings.USERENA_SIGNIN_AFTER_SIGNUP and not userena_settings.USERENA_ACTIVATION_REQUIRED): user = authenticate(identification=user.email, check_password=False) login(request, user) return redirect(redirect_to) if not extra_context: extra_context = dict() extra_context['form'] = form return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request)
def home(request): template_name = 'icantieri/home.html' extra_context = dict() #extra_context['merchandises'] = get_public_merchandises return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request)