<commit_before>import datetime
import os.path
from django.conf import settings

from django.db import models
from django.contrib.auth.models import User
from django.core.files.base import ContentFile
from django.utils.translation import ugettext as _
from apps.managers.cache_mgr import cache_mgr

try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

try:
    from PIL import Image
except ImportError:
    import Image

from apps.lib.avatar import AVATAR_STORAGE_DIR, AVATAR_RESIZE_METHOD

def avatar_file_path(instance=None, filename=None, user=None):
    user = user or instance.user
    return os.path.join(settings.MAKAHIKI_MEDIA_PREFIX,
                        AVATAR_STORAGE_DIR,
                        user.username,
                        filename)


class Avatar(models.Model):
    user = models.ForeignKey(User)
    primary = models.BooleanField(default=False)
    avatar = models.ImageField(max_length=1024,
                               upload_to=avatar_file_path,
                               blank=True)
    date_uploaded = models.DateTimeField(default=datetime.datetime.now)

    def __unicode__(self):
        return _(u'Avatar for %s') % self.user

    def save(self, *args, **kwargs):
        if self.primary:
            avatars = Avatar.objects.filter(user=self.user, primary=True)\
            .exclude(id=self.id)
            avatars.update(primary=False)

        super(Avatar, self).save(*args, **kwargs)

    def thumbnail_exists(self, size):
        return self.avatar.storage.exists(self.avatar_name(size))

    def create_thumbnail(self, size):
        try:
            orig = self.avatar.storage.open(self.avatar.name, 'rb').read()<commit_msg>invalidate the RIB template cache after avatar change<commit_after>import datetime
import os.path
from django.conf import settings

from django.db import models
from django.contrib.auth.models import User
from django.core.files.base import ContentFile
from django.utils.translation import ugettext as _
from apps.managers.cache_mgr import cache_mgr

try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

try:
    from PIL import Image
except ImportError:
    import Image

from apps.lib.avatar import AVATAR_STORAGE_DIR, AVATAR_RESIZE_METHOD

def avatar_file_path(instance=None, filename=None, user=None):
    user = user or instance.user
    return os.path.join(settings.MAKAHIKI_MEDIA_PREFIX,
                        AVATAR_STORAGE_DIR,
                        user.username,
                        filename)


class Avatar(models.Model):
    user = models.ForeignKey(User)
    primary = models.BooleanField(default=False)
    avatar = models.ImageField(max_length=1024,
                               upload_to=avatar_file_path,
                               blank=True)
    date_uploaded = models.DateTimeField(default=datetime.datetime.now)

    def __unicode__(self):
        return _(u'Avatar for %s') % self.user

    def save(self, *args, **kwargs):
        if self.primary:
            avatars = Avatar.objects.filter(user=self.user, primary=True)\
            .exclude(id=self.id)
            avatars.update(primary=False)

        super(Avatar, self).save(*args, **kwargs)

        # Invalidate info bar cache.
        cache_mgr.invalidate_template_cache("RIB", self.user.username)


    def thumbnail_exists(self, size):
        return self.avatar.storage.exists(self.avatar_name(size))

    def create_thumbnail(self, size):
        try:
            orig = self.avatar.storage.open(self.avatar.name, 'rb').read()
{'fraction_non_alphanumeric': 0.08695652173913043, 'fraction_numerical': 0.002229654403567447, 'mean_word_length': 2.7739221871713986, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '504009', 'n_tokens_mistral': 1074, 'n_tokens_neox': 1039, 'n_words': 236}