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

Commit

Permalink
fix #147, fix #146: Allow slug change until first award, use md5 hash…
Browse files Browse the repository at this point in the history
… in image filenames
  • Loading branch information
lmorchard committed Feb 22, 2013
1 parent 128c4bc commit 81bf49d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
15 changes: 11 additions & 4 deletions badger/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@

TIME_ZONE_OFFSET = getattr(settings, "TIME_ZONE_OFFSET", timedelta(0))

MK_UPLOAD_TMPL = '%(base)s/%(field_fn)s_%(slug)s_%(now)s_%(rand)04d.%(ext)s'
MK_UPLOAD_TMPL = '%(base)s/%(h1)s/%(h2)s/%(hash)s_%(field_fn)s_%(now)s_%(rand)04d.%(ext)s'

DEFAULT_HTTP_PROTOCOL = getattr(settings, "DEFAULT_HTTP_PROTOCOL", "http")

Expand Down Expand Up @@ -166,8 +166,8 @@ def slugify(txt):
txt = txt.strip()
# remove spaces before and after dashes
txt = re.sub('\s*-\s*','-', txt, re.UNICODE)
# replace remaining spaces with underscores
txt = re.sub('[\s/]', '_', txt, re.UNICODE)
# replace remaining spaces with dashes
txt = re.sub('[\s/]', '-', txt, re.UNICODE)
# replace colons between numbers with dashes
txt = re.sub('(\d):(\d)', r'\1-\2', txt, re.UNICODE)
# replace double quotes with single quotes
Expand All @@ -193,8 +193,12 @@ def mk_upload_to(field_fn, ext, tmpl=MK_UPLOAD_TMPL):
"""upload_to builder for file upload fields"""
def upload_to(instance, filename):
base, slug = instance.get_upload_meta()
slug_hash = (hashlib.md5(instance.slug.encode('utf-8', 'ignore'))
.hexdigest())
return tmpl % dict(now=int(time()), rand=random.randint(0, 1000),
slug=slug[:50], base=base, field_fn=field_fn,
pk=instance.pk,
hash=slug_hash, h1=slug_hash[0], h2=slug_hash[1],
ext=ext)
return upload_to

Expand Down Expand Up @@ -430,9 +434,12 @@ def clean(self):

def save(self, **kwargs):
"""Save the submission, updating slug and screenshot thumbnails"""
if not self.slug:
awards_count = self.award_set.count()
if awards_count == 0:
self.slug = slugify(self.title)

super(Badge, self).save(**kwargs)

if notification:
if self.creator:
notification.send([self.creator], 'badge_edited',
Expand Down
3 changes: 3 additions & 0 deletions badger/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def test_unicode_slug(self):
badge = self._get_badge()
badge.title = u'弁護士バッジ(レプリカ)'
badge.slug = ''
img_data = open(BADGE_IMG_FN, 'r').read()
badge.image.save('', ContentFile(img_data), True)
badge.save()

ok_(badge.slug != '')
Expand Down Expand Up @@ -145,6 +147,7 @@ def test_baked_award_image(self):
eq_(expected_url, hosted_assertion_url)
award_1.delete()


class BadgerProgressTest(BadgerTestCase):

def test_progress_badge_already_awarded(self):
Expand Down

0 comments on commit 81bf49d

Please sign in to comment.