Exemple #1
0
 def test_basic(self):
     """
     Test user profile helpers normally.
     """
     if hasattr(settings, 'AUTH_PROFILE_MODULE'):
         self.assertTrue(get_user_profile_model() is not None, "Received "
             "user profile model is %s" % get_user_profile_model(settings))
         if settings.AUTH_PROFILE_MODULE != 'richtemplates.UserProfile':
             self.assertTrue(UserProfile._meta.abstract is True)
         else:
             self.assertTrue(UserProfile._meta.abstract is False)
def get_skin_from_request(request):
    """
    Returns ``RichSkin`` object for the given request.
    If request's user is AnonymousUser skin would be taken from session.
    If there is no skin information in the session (key name is defined
    by ``RICHTEMPLATES_SESSION_SKIN_NAME`` setting) default skin would be
    returned. Same if authenticated user's profile has no information on
    skin (or profile is not available at all).
    If possible, skin would be taken form user's profile.
    """
    user = getattr(request, 'user', None)
    profile_model = get_user_profile_model()
    # Check if profile model is specified and user authed
    if profile_model and user.is_authenticated():
        try:
            profile = user.get_profile()
            alias = getattr(profile, richtemplates.settings.PROFILE_SKIN_FIELD,
                None)
            if alias:
                skin = get_skin_by_alias(alias)
                return skin
        except profile_model.DoesNotExist:
            logging.debug("Profile specified but not available for user %s. "
                "Will fallback to ``get_skin_from_session`` method." % user)
    skin = get_skin_from_session(request.session)
    return skin
def get_skin_from_request(request):
    """
    Returns ``RichSkin`` object for the given request.
    If request's user is AnonymousUser skin would be taken from session.
    If there is no skin information in the session (key name is defined
    by ``RICHTEMPLATES_SESSION_SKIN_NAME`` setting) default skin would be
    returned. Same if authenticated user's profile has no information on
    skin (or profile is not available at all).
    If possible, skin would be taken form user's profile.
    """
    user = getattr(request, 'user', None)
    profile_model = get_user_profile_model()
    # Check if profile model is specified and user authed
    if profile_model and user.is_authenticated():
        try:
            profile = user.get_profile()
            alias = getattr(profile, richtemplates.settings.PROFILE_SKIN_FIELD,
                None)
            if alias:
                skin = get_skin_by_alias(alias)
                return skin
        except profile_model.DoesNotExist:
            logging.debug("Profile specified but not available for user %s. "
                "Will fallback to ``get_skin_from_session`` method." % user)
    skin = get_skin_from_session(request.session)
    return skin
Exemple #4
0
 def test_empty_setting(self):
     """
     Tests user profile helpers when ``AUTH_PROFILE_MODULE`` is not defined.
     """
     if hasattr(settings, 'AUTH_PROFILE_MODULE'):
         delattr(settings, 'AUTH_PROFILE_MODULE')
     self.assertTrue(get_user_profile_model() is None)
     if self.AUTH_PROFILE_MODULE_COPY:
         settings.AUTH_PROFILE_MODULE = self.AUTH_PROFILE_MODULE_COPY
     self.assertTrue(UserProfile._meta.abstract is True)
def request_new_profile(sender, instance, **kwargs):
    """
    Creation of profile for new users
    """
    _UserProfile = get_user_profile_model()
    if _UserProfile is None:
        return
    profile, created = _UserProfile.objects.get_or_create(user=instance, )
    if created is True:
        logging.debug("Creating profile for %s ..." % instance)
        profile.activation_token = default_token_generator.make_token(instance)
        profile.save()
        logging.debug("Created profile's id: %s" % profile.id)
Exemple #6
0
def request_new_profile(sender, instance, **kwargs):
    """
    Creation of profile for new users
    """
    _UserProfile = get_user_profile_model()
    if _UserProfile is None:
        return
    profile, created = _UserProfile.objects.get_or_create(
        user=instance,
    )
    if created is True:
        logging.debug("Creating profile for %s ..." % instance)
        profile.activation_token = default_token_generator.make_token(instance)
        profile.save()
        logging.debug("Created profile's id: %s" % profile.id)
def new_richtemplates_profile(instance, **kwargs):
    if kwargs['created'] is True:
        UserProfile = get_user_profile_model()
        if UserProfile is not None:
            try:
                # We run get_or_create instead of create as there may be other
                # handlers which would automaticaly create profiles
                UserProfile.objects.get_or_create(
                    user = instance,
                )
                logging.debug("New profile created for user %s" % instance)
            except (DatabaseError, IntegrityError), err:
                logging.warning("Richtemplates tried to create profile for new "
                                "user %s but it seems there is already one or "
                                "profile table does not exist. "
                                "Original error: %s" % (instance, err))
                logging.warning("Consider running syncdb again after issue is "
                                "resolved")
def put_missing_userprofiles(sender, **kwargs):
    """
    If user profile model is defined at ``settings.AUTH_PROFILE_MODULE``
    richtemplates would try to find out if there are missing profiles
    and would ask to create them if necessary.
    """
    UserProfile = get_user_profile_model()
    if UserProfile:
        try:
            global PROFILE_APP
            PROFILE_APP = get_app(UserProfile._meta.app_label)
        except ImproperlyConfigured:
            pass
        users_count = User.objects.count()
        profiles_count = UserProfile.objects.count()
        if users_count > profiles_count:
            if kwargs['interactive'] is True:
                msg = "There are %d User objects without profiles"\
                    % (users_count - profiles_count)
                logging.info(msg)
                answer = ''
                while answer.lower() not in ('yes', 'no'):
                    prompt = "Create missing profiles? [yes/no]: "
                    try:
                        answer = raw_input(prompt).lower()
                    except KeyboardInterrupt:
                        sys.stderr.write("\nInterrupted by user - taken "
                            "as 'no'\n")
                        answer = 'no'
                if answer == 'yes':
                    rel_profile_name = UserProfile._meta.object_name.lower()
                    users_without_profile = User.objects\
                        .annotate(profile_count=Count(rel_profile_name))\
                        .filter(profile_count=0)
                    for user in users_without_profile:
                        UserProfile.objects.create(user=user)
                        if kwargs['verbosity'] >= 1:
                            print "[INFO] Created profile for user %s" % user
Exemple #9
0
def put_missing_userprofiles(sender, **kwargs):
    """
    If user profile model is defined at ``settings.AUTH_PROFILE_MODULE``
    richtemplates would try to find out if there are missing profiles
    and would ask to create them if necessary.
    """
    UserProfile = get_user_profile_model()
    if UserProfile:
        try:
            global PROFILE_APP
            PROFILE_APP = get_app(UserProfile._meta.app_label)
        except ImproperlyConfigured:
            pass
        users_count = User.objects.count()
        profiles_count = UserProfile.objects.count()
        if users_count > profiles_count:
            if kwargs['interactive'] is True:
                msg = "There are %d User objects without profiles"\
                    % (users_count - profiles_count)
                logging.info(msg)
                answer = ''
                while answer.lower() not in ('yes', 'no'):
                    prompt = "Create missing profiles? [yes/no]: "
                    try:
                        answer = raw_input(prompt).lower()
                    except KeyboardInterrupt:
                        sys.stderr.write("\nInterrupted by user - taken "
                                         "as 'no'\n")
                        answer = 'no'
                if answer == 'yes':
                    rel_profile_name = UserProfile._meta.object_name.lower()
                    users_without_profile = User.objects\
                        .annotate(profile_count=Count(rel_profile_name))\
                        .filter(profile_count=0)
                    for user in users_without_profile:
                        UserProfile.objects.create(user=user)
                        if kwargs['verbosity'] >= 1:
                            print "[INFO] Created profile for user %s" % user
Exemple #10
0
def set_skin_at_request(request, skin_alias):
    """
    Sets skin on the given ``request`` instance.
    If user from request is authenticated and has profile with skin field
    (defined at ``RICHTEMPLATES_PROFILE_SKIN_FIELD``) then skin is set
    there. Otherwise, skin is set on the session.
    """
    if skin_alias not in richtemplates.settings.SKINS.keys():
        raise SkinDoesNotExist("Skin '%s' is not available")
    user = getattr(request, 'user', None)
    profile_model = get_user_profile_model()
    if profile_model and user.is_authenticated():
        try:
            profile = user.get_profile()
            profile.skin = skin_alias
            profile.save()
            logging.debug("Set skin %s for user %s in his/her profile"
                % (skin_alias, user))
            # Do not store skin on session if profile is available
            # as at the time user logs out his/her session is destroyed
            return
        except profile_model.DoesNotExist:
            pass
    set_skin_at_session(request.session, skin_alias)
Exemple #11
0
def set_skin_at_request(request, skin_alias):
    """
    Sets skin on the given ``request`` instance.
    If user from request is authenticated and has profile with skin field
    (defined at ``RICHTEMPLATES_PROFILE_SKIN_FIELD``) then skin is set
    there. Otherwise, skin is set on the session.
    """
    if skin_alias not in richtemplates.settings.SKINS.keys():
        raise SkinDoesNotExist("Skin '%s' is not available")
    user = getattr(request, 'user', None)
    profile_model = get_user_profile_model()
    if profile_model and user.is_authenticated():
        try:
            profile = user.get_profile()
            profile.skin = skin_alias
            profile.save()
            logging.debug("Set skin %s for user %s in his/her profile"
                % (skin_alias, user))
            # Do not store skin on session if profile is available
            # as at the time user logs out his/her session is destroyed
            return
        except profile_model.DoesNotExist:
            pass
    set_skin_at_session(request.session, skin_alias)
Exemple #12
0
 class Meta:
     exclude = ('user',)
     model = get_user_profile_model()