Skip to content

Commit

Permalink
Merge pull request bread-and-pepper#252 from zalew/master
Browse files Browse the repository at this point in the history
Support for optional arguments in USERENA_MUGSHOT_PATH setting.
  • Loading branch information
wunki committed Nov 27, 2012
2 parents 64e08a6 + 0076a33 commit af409f4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
14 changes: 14 additions & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ Default: ``mugshots/`` (string)
The default path that the mugshots will be saved to. Is appended to the
``MEDIA_PATH`` in your Django settings.

You can use the following options as arguments (f.ex. ``mugshots/%(username)s/``):

``id``
User.id

``username``
User.username

``date``
User.date_joined

``date_now``
Current date

USERENA_USE_HTTPS
~~~~~~~~~~~~~~~~~
Default: ``False`` (boolean)
Expand Down
58 changes: 32 additions & 26 deletions userena/models.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.template.loader import render_to_string
#!/usr/bin/python
# -*- coding: utf-8 -*-
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.core.mail import send_mail
from django.core.exceptions import ImproperlyConfigured

from userena.utils import get_gravatar, generate_sha1, get_protocol, get_datetime_now
from userena.managers import UserenaManager, UserenaBaseProfileManager
from userena import settings as userena_settings

from guardian.shortcuts import get_perms
from guardian.shortcuts import assign

from django.db import models
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _
from easy_thumbnails.fields import ThumbnailerImageField
from guardian.shortcuts import get_perms
from userena import settings as userena_settings
from userena.managers import UserenaManager, UserenaBaseProfileManager
from userena.utils import get_gravatar, generate_sha1, get_protocol, \
get_datetime_now
import datetime

import datetime, random

PROFILE_PERMISSIONS = (
('view_profile', 'Can view profile'),
)


def upload_to_mugshot(instance, filename):
"""
Uploads a mugshot for a user to the ``USERENA_MUGSHOT_PATH`` and saving it
Expand All @@ -32,10 +30,17 @@ def upload_to_mugshot(instance, filename):
"""
extension = filename.split('.')[-1].lower()
salt, hash = generate_sha1(instance.id)
return '%(path)s%(hash)s.%(extension)s' % {'path': userena_settings.USERENA_MUGSHOT_PATH,
path = userena_settings.USERENA_MUGSHOT_PATH % {
'username': instance.user.username,
'id': instance.user.id,
'date': instance.user.date_joined,
'date_now': get_datetime_now().date(),
}
return '%(path)s%(hash)s.%(extension)s' % {'path': path,
'hash': hash[:10],
'extension': extension}


class UserenaSignup(models.Model):
"""
Userena model which stores all the necessary information to have a full
Expand Down Expand Up @@ -71,7 +76,6 @@ class UserenaSignup(models.Model):
blank=True,
null=True)


objects = UserenaManager()

class Meta:
Expand Down Expand Up @@ -117,14 +121,13 @@ def send_confirmation_email(self):
a request is made to change this email address.
"""
context= {'user': self.user,
context = {'user': self.user,
'without_usernames': userena_settings.USERENA_WITHOUT_USERNAMES,
'new_email': self.email_unconfirmed,
'protocol': get_protocol(),
'confirmation_key': self.email_confirmation_key,
'site': Site.objects.get_current()}


# Email to the old address, if present
subject_old = render_to_string('userena/emails/confirmation_email_subject_old.txt',
context)
Expand All @@ -149,7 +152,7 @@ def send_confirmation_email(self):
send_mail(subject_new,
message_new,
settings.DEFAULT_FROM_EMAIL,
[self.email_unconfirmed,])
[self.email_unconfirmed, ])

def activation_key_expired(self):
"""
Expand Down Expand Up @@ -179,7 +182,7 @@ def send_activation_email(self):
user.
"""
context= {'user': self.user,
context = {'user': self.user,
'without_usernames': userena_settings.USERENA_WITHOUT_USERNAMES,
'protocol': get_protocol(),
'activation_days': userena_settings.USERENA_ACTIVATION_DAYS,
Expand All @@ -195,7 +198,8 @@ def send_activation_email(self):
send_mail(subject,
message,
settings.DEFAULT_FROM_EMAIL,
[self.user.email,])
[self.user.email, ])


class UserenaBaseProfile(models.Model):
""" Base model needed for extra profile functionality """
Expand All @@ -219,11 +223,10 @@ class UserenaBaseProfile(models.Model):
max_length=15,
choices=PRIVACY_CHOICES,
default=userena_settings.USERENA_DEFAULT_PRIVACY,
help_text = _('Designates who can view your profile.'))
help_text=_('Designates who can view your profile.'))

objects = UserenaBaseProfileManager()


class Meta:
"""
Meta options making the model abstract and defining permissions.
Expand Down Expand Up @@ -277,7 +280,8 @@ def get_mugshot_url(self):
'monsterid',
'wavatar']:
return userena_settings.USERENA_MUGSHOT_DEFAULT
else: return None
else:
return None

def get_full_name_or_username(self):
"""
Expand Down Expand Up @@ -340,7 +344,8 @@ def can_view_profile(self, user):
"""
# Simple cases first, we don't want to waste CPU and DB hits.
# Everyone.
if self.privacy == 'open': return True
if self.privacy == 'open':
return True
# Registered users.
elif self.privacy == 'registered' and isinstance(user, User):
return True
Expand All @@ -352,6 +357,7 @@ def can_view_profile(self, user):
# Fallback to closed profile.
return False


class UserenaLanguageBaseProfile(UserenaBaseProfile):
"""
Extends the :class:`UserenaBaseProfile` with a language choice.
Expand Down

0 comments on commit af409f4

Please sign in to comment.