def account_data(self, test_value): """ Returns a dict with test data for all the user/profile fields. """ # User fields data = {"email": test_value + "@example.com"} for field in ("first_name", "last_name", "username", "password1", "password2"): if field.startswith("password"): value = "x" * settings.ACCOUNTS_MIN_PASSWORD_LENGTH else: value = test_value data[field] = value # Profile fields Profile = get_profile_model() if Profile is not None: user_fieldname = get_profile_user_fieldname() for field in Profile._meta.fields: if field.name not in (user_fieldname, "id"): if field.choices: value = field.choices[0][0] else: value = test_value data[field.name] = value return data
def account_data(self, test_value): """ Returns a dict with test data for all the user/profile fields. """ # User fields data = {"email": test_value + "@example.com"} for field in ("first_name", "last_name", "username", "password1", "password2"): if field.startswith("password"): value = "x" * settings.ACCOUNTS_MIN_PASSWORD_LENGTH else: value = test_value data[field] = value # Profile fields Profile = get_profile_model() if Profile is not None: from mezzanine.accounts.forms import ProfileFieldsForm user_fieldname = get_profile_user_fieldname() for name, field in ProfileFieldsForm().fields.items(): if name not in (user_fieldname, "id"): if hasattr(field, "choices"): value = list(field.choices)[0][0] elif isinstance(field, (DateField, DateTimeField)): value = "9001-04-20" else: value = test_value data[name] = value return data
class ProfileInline(admin.StackedInline): model = get_profile_model() can_delete = False template = "admin/profile_inline.html" extra = 0 def get_min_num(self, request, obj=None, **kwargs): """This causes profile forms to be shown when editing but hidden when creating. If min_num is fixed at 1, Django's initial user creation form fails if the profile model has a required field.""" return 0 if obj is None else 1
def karma(sender, **kwargs): """ Each time a rating is saved, check its value and modify the profile karma for the related object's user accordingly. Since ratings are either +1/-1, if a rating is being edited, we can assume that the existing rating is in the other direction, so we multiply the karma modifier by 2. """ rating = kwargs["instance"] value = int(rating.value) if not kwargs["created"]: value *= 2 content_object = rating.content_object if rating.user != content_object.user: queryset = get_profile_model().objects.filter(user=content_object.user) queryset.update(karma=models.F("karma") + value)
def profile(request, username, template="accounts/account_profile.html"): """ Display a profile. """ profile_user = get_object_or_404(User, username=username, is_active=True) profile_fields = SortedDict() Profile = get_profile_model() if Profile is not None: profile = profile_user.get_profile() user_fieldname = get_profile_user_fieldname() exclude = tuple(settings.ACCOUNTS_PROFILE_FORM_EXCLUDE_FIELDS) for field in Profile._meta.fields: if field.name not in ("id", user_fieldname) + exclude: value = getattr(profile, field.name) profile_fields[field.verbose_name.title()] = value context = {"profile_user": profile_user, "profile_fields": profile_fields.items()} return render(request, template, context)
def profile_fields(user): """ Returns profile fields as a dict for the given user. Used in the profile view template when the ``ACCOUNTS_PROFILE_VIEWS_ENABLED`` setting is set to ``True``, and also in the account approval emails sent to administrators when the ``ACCOUNTS_APPROVAL_REQUIRED`` setting is set to ``True``. """ fields = SortedDict() Profile = get_profile_model() if Profile is not None: profile = user.get_profile() user_fieldname = get_profile_user_fieldname() exclude = tuple(settings.ACCOUNTS_PROFILE_FORM_EXCLUDE_FIELDS) for field in Profile._meta.fields: if field.name not in ("id", user_fieldname) + exclude: value = getattr(profile, field.name) fields[field.verbose_name.title()] = value return list(fields.items())
def karma(sender, **kwargs): """ Each time a rating is saved, check its value and modify the profile karma for the related object's user accordingly. Since ratings are either +1/-1, if a rating is being edited, we can assume that the existing rating is in the other direction, so we multiply the karma modifier by 2. We also run this when a rating is deleted (undone), in which case we just negate the rating value from the karma. """ rating = kwargs["instance"] value = int(rating.value) if "created" not in kwargs: value *= -1 # Rating deleted elif not kwargs["created"]: value *= 2 # Rating changed content_object = rating.content_object if rating.user != content_object.user: queryset = get_profile_model().objects.filter(user=content_object.user) queryset.update(karma=models.F("karma") + value)
def profile(request, username, template="accounts/account_profile.html"): """ Display a profile. """ profile_user = get_object_or_404(User, username=username, is_active=True) profile_fields = SortedDict() Profile = get_profile_model() if Profile is not None: profile = profile_user.get_profile() user_fieldname = get_profile_user_fieldname() exclude = tuple(settings.ACCOUNTS_PROFILE_FORM_EXCLUDE_FIELDS) for field in Profile._meta.fields: if field.name not in ("id", user_fieldname) + exclude: value = getattr(profile, field.name) profile_fields[field.verbose_name.title()] = value context = { "profile_user": profile_user, "profile_fields": profile_fields.items(), } return render(request, template, context)
_("Invalid username/email and password")) elif not self._user.is_active: raise forms.ValidationError(_("Your account is inactive")) return self.cleaned_data def save(self): """ Just return the authenticated user - used for logging in. """ return getattr(self, "_user", None) # If a profile model has been configured with the ``AUTH_PROFILE_MODULE`` # setting, create a model form for it that will have its fields added to # ``ProfileForm``. Profile = get_profile_model() _exclude_fields = tuple(settings.ACCOUNTS_PROFILE_FORM_EXCLUDE_FIELDS) if Profile is not None: class ProfileFieldsForm(forms.ModelForm): class Meta: model = Profile exclude = (get_profile_user_fieldname(),) + _exclude_fields class ProfileForm(Html5Mixin, forms.ModelForm): """ ModelForm for auth.User - used for signup and profile update. If a Profile model is defined via ``AUTH_PROFILE_MODULE``, its fields are injected into the form. """
class Meta: model = get_profile_model() exclude = (get_profile_user_fieldname(),) + _exclude_fields
class ProfileInline(admin.StackedInline): model = get_profile_model() can_delete = False template = "admin/profile_inline.html" extra = 0
from django.shortcuts import get_object_or_404, redirect from django.utils.timezone import now from django.views.generic import ListView, CreateView, DetailView, TemplateView from mezzanine.accounts import get_profile_model from mezzanine.conf import settings from mezzanine.generic.models import ThreadedComment, Keyword from mezzanine.utils.views import paginate from drum.links.forms import LinkForm from drum.links.models import Link from drum.links.utils import order_by_score from django.http import HttpResponse, HttpResponseNotAllowed, HttpResponseBadRequest # Returns the name to be used for reverse profile lookups from the user # object. That's "profile" for the ``drum.links.Profile``, but otherwise # depends on the model specified in ``AUTH_PROFILE_MODULE``. USER_PROFILE_RELATED_NAME = get_profile_model().user.field.related_query_name() class UserFilterView(ListView): """ List view that puts a ``profile_user`` variable into the context, which is optionally retrieved by a ``username`` urlpattern var. If a user is loaded, ``object_list`` is filtered by the loaded user. Used for showing lists of links and comments. """ def get_context_data(self, **kwargs): context = super(UserFilterView, self).get_context_data(**kwargs) try: username = self.kwargs["username"] except KeyError: profile_user = None
from mezzanine.pages.models import RichTextPage from links.forms import LinkForm from links.models import Link from links.utils import order_by_score from theme.models import Portfolio, PortfolioItem, Certificate, ArtigoFinal fs = FileSystemStorage(location=settings.FORMS_UPLOAD_ROOT) # Returns the name to be used for reverse profile lookups from the user # object. That's "profile" for the ``links.Profile``, but otherwise # depends on the model specified in ``AUTH_PROFILE_MODULE``. USER_PROFILE_RELATED_NAME = get_profile_model().user.field.related_query_name() class UserFilterView(ListView): """ List view that puts a ``profile_user`` variable into the context, which is optionally retrieved by a ``username`` urlpattern var. If a user is loaded, ``object_list`` is filtered by the loaded user. Used for showing lists of links and comments. """ def get_context_data(self, **kwargs): context = super(UserFilterView, self).get_context_data(**kwargs) try: username = self.kwargs["username"] except KeyError:
LOGOUT_URL = settings.LOGOUT_URL PROFILE_URL = getattr(settings, "PROFILE_URL", "/users/") PROFILE_UPDATE_URL = getattr(settings, "PROFILE_UPDATE_URL", "/account/update/") PASSWORD_RESET_URL = getattr(settings, "PASSWORD_RESET_URL", "/account/password/reset/") PASSWORD_RESET_VERIFY_URL = getattr(settings, "PASSWORD_RESET_VERIFY_URL", "/account/password/verify/") verify_pattern = "/(?P<uidb36>[-\w]+)/(?P<token>[-\w]+)/$" urlpatterns = patterns("mezzanine.accounts.views", url("^%s/$" % LOGIN_URL.strip("/"), "login", name="login"), url("^%s/$" % LOGOUT_URL.strip("/"), "logout", name="logout"), url("^%s/$" % SIGNUP_URL.strip("/"), "signup", name="signup"), url("^%s%s" % (SIGNUP_VERIFY_URL.strip("/"), verify_pattern), "signup_verify", name="signup_verify"), url("^%s/$" % PROFILE_UPDATE_URL.strip("/"), "profile_update", name="profile_update"), url("^%s/$" % PASSWORD_RESET_URL.strip("/"), "password_reset", name="mezzanine_password_reset"), url("^%s%s" % (PASSWORD_RESET_VERIFY_URL.strip("/"), verify_pattern), "password_reset_verify", name="password_reset_verify"), ) if get_profile_model(): urlpatterns += patterns("mezzanine.accounts.views", url("^%s/(?P<username>.*)/$" % PROFILE_URL.strip("/"), "profile", name="profile"), )