<commit_before>import datetime
import os.path

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 _

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

try:
    from PIL import Image
except ImportError:
    import Image

from avatar import AVATAR_STORAGE_DIR, AVATAR_RESIZE_METHOD, \
                   AVATAR_MAX_AVATARS_PER_USER

def avatar_file_path(instance=None, filename=None, user=None):
    user = user or instance.user
    return os.path.join(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, force_insert=False, force_update=False):
        if AVATAR_MAX_AVATARS_PER_USER == 1:
            first = Avatar.objects.filter(user=self.user).only('id')[0]
            first.delete()
        elif self.primary and AVATAR_MAX_AVATARS_PER_USER > 1:
            avatars = Avatar.objects.filter(user=self.user, primary=True)\<commit_msg>Don't use only(id) now that we are deleting instead of updating<commit_after>import datetime
import os.path

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 _

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

try:
    from PIL import Image
except ImportError:
    import Image

from avatar import AVATAR_STORAGE_DIR, AVATAR_RESIZE_METHOD, \
                   AVATAR_MAX_AVATARS_PER_USER

def avatar_file_path(instance=None, filename=None, user=None):
    user = user or instance.user
    return os.path.join(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, force_insert=False, force_update=False):
        if AVATAR_MAX_AVATARS_PER_USER == 1:
            first = Avatar.objects.filter(user=self.user)[0]
            first.delete()
        elif self.primary and AVATAR_MAX_AVATARS_PER_USER > 1:
            avatars = Avatar.objects.filter(user=self.user, primary=True)\
{'fraction_non_alphanumeric': 0.08282319049333814, 'fraction_numerical': 0.005041411595246669, 'mean_word_length': 3.8229166666666665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6670238', 'n_tokens_mistral': 890, 'n_tokens_neox': 860, 'n_words': 201}