Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Remove dep on easy thumbnails #420

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"``long_description`` (%s)\n" % readme_file)
sys.exit(1)

install_requires = ['django-guardian', 'html2text']
try:
from collections import OrderedDict
except ImportError:
install_requires.append('ordereddict')

setup(name='django-userena',
version=userena.get_version(),
description='Complete user management application for Django',
Expand All @@ -22,14 +28,7 @@
download_url='https://github.com/bread-and-pepper/django-userena/downloads',
packages = find_packages(exclude=['demo', 'demo.*']),
include_package_data=True,
install_requires = [
'easy_thumbnails',
'django-guardian>=1.1.0.beta',
'html2text',
### Required to build documentation
# 'sphinx',
# 'south',
],
install_requires = install_requires,
test_suite='tests.main',
classifiers = ['Development Status :: 4 - Beta',
'Environment :: Web Environment',
Expand All @@ -39,4 +38,4 @@
'Operating System :: OS Independent',
'Programming Language :: Python',
'Topic :: Utilities'],
)
)
60 changes: 60 additions & 0 deletions userena/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os

from django.db.models.fields.files import ImageField
from django.conf import settings

from userena import settings as userena_settings

"""
This file is taken from:

https://raw.githubusercontent.com/barszczmm/django-easy-userena/master/userena/fields.py

and modified to eliminate easy_thumbnails completely (and remove references)
to USERENA_MUGSHOT_CROP_APP.
"""

class PILThumbnailerImageField(ImageField):
"""
Django field that behaves as ImageField but auto resizes images on upload.
"""
def __init__(self, *args, **kwargs):
"""
Added fields:
- resize_source: a dict containing width and height to resize image:
{'size': (width, height)}
"""
self.resize_source = kwargs.get('resize_source', {})
del kwargs['resize_source']
super(PILThumbnailerImageField, self).__init__(*args, **kwargs)

def south_field_triple(self):
"""
Return a suitable description of this field for South.
"""
from south.modelsinspector import introspector
field_class = 'django.db.models.fields.files.FileField'
args, kwargs = introspector(self)
return (field_class, args, kwargs)

def pre_save(self, model_instance, add):
file = super(PILThumbnailerImageField, self).pre_save(model_instance, add)
if file and self.resize_source and (file.width != self.resize_source['size'][0] or file.height != self.resize_source['size'][1]):
file_fullpath = os.path.join(settings.MEDIA_ROOT, file.name)
self._resize_image(file_fullpath, self.resize_source)
return file

def _resize_image(self, filename, resize_source):
"""
Resizes the image to specified width and height.
- filename: full path of image to resize
- resize_source: a dict containing width and height to resize image:
{'size': (width, height)}
"""
from PIL import Image, ImageOps
img = Image.open(filename)
img = ImageOps.fit(img, resize_source['size'], Image.ANTIALIAS)
try:
img.save(filename, optimize=1)
except IOError:
img.save(filename)
13 changes: 12 additions & 1 deletion userena/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
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
# See: https://github.com/barszczmm/django-easy-userena/blob/master/userena/models.py
from userena.fields import PILThumbnailerImageField as ThumbnailerImageField
from guardian.shortcuts import get_perms
from userena import settings as userena_settings
from userena.managers import UserenaManager, UserenaBaseProfileManager
Expand All @@ -14,6 +15,11 @@
import datetime
from .mail import send_mail

from django.utils.timezone import is_aware, is_naive

# See: https://stackoverflow.com/questions/7065164/how-to-make-an-unaware-datetime-timezone-aware-in-python
import pytz # need to make aware datetime objects that are unaware (actually to match)


PROFILE_PERMISSIONS = (
('view_profile', 'Can view profile'),
Expand Down Expand Up @@ -188,6 +194,11 @@ def activation_key_expired(self):
"""
expiration_days = datetime.timedelta(days=userena_settings.USERENA_ACTIVATION_DAYS)
expiration_date = self.user.date_joined + expiration_days
the_datetime_now = get_datetime_now()
get_datetime_now_is_aware = is_aware(the_datetime_now)
expiration_date_is_aware = is_aware(expiration_date)
if is_aware(the_datetime_now) and is_naive(expiration_date):
expiration_date = pytz.utc.localize(expiration_date)
if self.activation_key == userena_settings.USERENA_ACTIVATED:
return True
if get_datetime_now() >= expiration_date:
Expand Down
2 changes: 2 additions & 0 deletions userena/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ def get_datetime_now():
when ``USE_TZ`` in project's settings is True or False respectively.
In older versions of Django it uses datetime.datetime.now().

For Django 1.6, need to use .utcnow()
See: https://docs.djangoproject.com/en/1.6/topics/i18n/timezones/#naive-and-aware-datetime-objects
"""
try:
from django.utils import timezone
Expand Down