<commit_before>from django.conf import settings
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.contrib.humanize.templatetags import humanize
from django.template.defaultfilters import slugify
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType


from AppDistribution.PackageHandlers import PackageHandler

from bitfield import BitField

from django.contrib.sites.models import Site

from os import remove
from os.path import splitext
from datetime import datetime


APP_TYPE_CHOICES = (("ANDROID", "Android"),
                    ("IOS", "iOS"),
                    ("BLACKBERRY", "BlackBerry"))

GROUP_BY_CHOICES = (("product", "Product"),
                  ("tag", "Tag"))

SITE_CACHE = {}

class SpoutSiteManager(models.Manager):

    def get_current(self):
        """
        Returns the current ``Site`` based on the SITE_ID in the
        project's settings. The ``Site`` object is cached the first
        time it's retrieved from the database.
        """
        from django.conf import settings
        try:
            sid = settings.SITE_ID
        except AttributeError:
            from django.core.exceptions import ImproperlyConfigured
            raise ImproperlyConfigured("You're using the Django \"sites framework\" without having set the SITE_ID setting. Create a site in your database and set the SITE_ID setting to fix this error.")
        try:
            current_site = SITE_CACHE[sid]
        except KeyError:
            current_site = self.get(pk=sid)
            SITE_CACHE[sid] = current_site
        return current_site

    def clear_cache(self):
        """Clears the ``Site`` object cache."""
        global SITE_CACHE
        SITE_CACHE = {}


class SpoutSite(models.Model):

    domain = models.CharField('domain name', max_length=100)
    name = models.CharField('display name', max_length=50)
    home_page = models.ForeignKey('Page', null=True)



    objects = SpoutSiteManager()

    class Meta:
        verbose_name = 'site'
        verbose_name_plural = 'sites'
        ordering = ('domain',)

    def __str__(self):
        return self.domain

    def save(self, *args, **kwargs):
        super(SpoutSite, self).save(*args, **kwargs)
        # Cached information will likely be incorrect now.
        if self.id in SITE_CACHE:
            del SITE_CACHE[self.id]

    def delete(self):
        pk = self.pk
        super(Site, self).delete()
        try:
            del SITE_CACHE[pk]
        except KeyError:
            pass


class App(models.Model):

    def __unicode__(self):
        return "%s - %s" % (self.name, self.version)


    download_count = models.IntegerField(default=0)
    product = models.ForeignKey('Product')
    tags = models.ManyToManyField('Tag', related_name='apps', blank=True, null=True)
    note = models.CharField(max_length=255, blank=True, null=True)

    icon = models.ImageField(upload_to=settings.APP_ICON_ROOT, blank=True, null=True)
    version = models.CharField(max_length=255)
    name = models.CharField(max_length=255)
    creation_date = models.DateTimeField(blank=True, null=True)
    device_type = models.CharField(choices=APP_TYPE_CHOICES, default="IOS", max_length=255) #TODO This should be a function, not stored

    def save(self, *args, **kwargs):
        if self.id is None:
            self.creation_date = datetime.now()
        super(App, self).save(*args, **kwargs)

    @property
    def primary_asset(self):

        try:
            primary_asset = self.assets.get(primary=True)
            return primary_asset
        except AppAsset.DoesNotExist:
            return None

    def _formatted_age(self):
        return humanize.naturaltime(self.creation_date)
    def _icon_url(self):

        try:

            if self.icon.url != None:
                return self.icon.url.replace(settings.MEDIA_ROOT, "/media")
        except ValueError:
                return ""

    icon_url = property(_icon_url)
    formatted_age = property(_formatted_age)
 
class AppAsset(models.Model):

    def __unicode__(self):
        return self.asset_file.name

    @property
    def filename(self):
        tags = self.app.tags.all()
        tag_string = ""
        if tags.count > 0:
            tag_string = "-"
            tag_string += ("-".join([t.name for t in tags]))

           
        filename = "%s%s-%s" % (self.app.product.name, tag_string, self.app.version)
        return filename

    app = models.ForeignKey('App', related_name='assets', null=True)

    primary = models.BooleanField()
    uuid = models.CharField(max_length=255, null=True, blank=True)
    asset_file = models.FileField(upload_to=settings.APP_PACKAGE_ROOT)
    asset_type = models.ForeignKey('AssetType', null=True, blank=True)

       
class AssetType(models.Model):

    @classmethod
    def get_or_create(self, extension):

        try:
            asset_type = AssetType.objects.get(extension=extension)
        except AssetType.DoesNotExist:
            asset_type = AssetType(extension=extension)

        return asset_type

    name = models.CharField(max_length=255, blank=True, null=True)
    extension = models.CharField(max_length=10)

class Tag(models.Model):<commit_msg>Tweaked filename function on AppAsset and now saving assettype on get_or_create.<commit_after>from django.conf import settings
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.contrib.humanize.templatetags import humanize
from django.template.defaultfilters import slugify
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType


from AppDistribution.PackageHandlers import PackageHandler

from bitfield import BitField

from django.contrib.sites.models import Site

from os import remove
from os.path import splitext
from datetime import datetime


APP_TYPE_CHOICES = (("ANDROID", "Android"),
                    ("IOS", "iOS"),
                    ("BLACKBERRY", "BlackBerry"))

GROUP_BY_CHOICES = (("product", "Product"),
                  ("tag", "Tag"))

SITE_CACHE = {}

class SpoutSiteManager(models.Manager):

    def get_current(self):
        """
        Returns the current ``Site`` based on the SITE_ID in the
        project's settings. The ``Site`` object is cached the first
        time it's retrieved from the database.
        """
        from django.conf import settings
        try:
            sid = settings.SITE_ID
        except AttributeError:
            from django.core.exceptions import ImproperlyConfigured
            raise ImproperlyConfigured("You're using the Django \"sites framework\" without having set the SITE_ID setting. Create a site in your database and set the SITE_ID setting to fix this error.")
        try:
            current_site = SITE_CACHE[sid]
        except KeyError:
            current_site = self.get(pk=sid)
            SITE_CACHE[sid] = current_site
        return current_site

    def clear_cache(self):
        """Clears the ``Site`` object cache."""
        global SITE_CACHE
        SITE_CACHE = {}


class SpoutSite(models.Model):

    domain = models.CharField('domain name', max_length=100)
    name = models.CharField('display name', max_length=50)
    home_page = models.ForeignKey('Page', null=True)



    objects = SpoutSiteManager()

    class Meta:
        verbose_name = 'site'
        verbose_name_plural = 'sites'
        ordering = ('domain',)

    def __str__(self):
        return self.domain

    def save(self, *args, **kwargs):
        super(SpoutSite, self).save(*args, **kwargs)
        # Cached information will likely be incorrect now.
        if self.id in SITE_CACHE:
            del SITE_CACHE[self.id]

    def delete(self):
        pk = self.pk
        super(Site, self).delete()
        try:
            del SITE_CACHE[pk]
        except KeyError:
            pass


class App(models.Model):

    def __unicode__(self):
        return "%s - %s" % (self.name, self.version)


    download_count = models.IntegerField(default=0)
    product = models.ForeignKey('Product')
    tags = models.ManyToManyField('Tag', related_name='apps', blank=True, null=True)
    note = models.CharField(max_length=255, blank=True, null=True)

    icon = models.ImageField(upload_to=settings.APP_ICON_ROOT, blank=True, null=True)
    version = models.CharField(max_length=255)
    name = models.CharField(max_length=255)
    creation_date = models.DateTimeField(blank=True, null=True)
    device_type = models.CharField(choices=APP_TYPE_CHOICES, default="IOS", max_length=255) #TODO This should be a function, not stored

    def save(self, *args, **kwargs):
        if self.id is None:
            self.creation_date = datetime.now()
        super(App, self).save(*args, **kwargs)

    @property
    def primary_asset(self):

        try:
            primary_asset = self.assets.get(primary=True)
            return primary_asset
        except AppAsset.DoesNotExist:
            return None

    def _formatted_age(self):
        return humanize.naturaltime(self.creation_date)
    def _icon_url(self):

        try:

            if self.icon.url != None:
                return self.icon.url.replace(settings.MEDIA_ROOT, "/media")
        except ValueError:
                return ""

    icon_url = property(_icon_url)
    formatted_age = property(_formatted_age)
 
class AppAsset(models.Model):

    def __unicode__(self):
        return self.asset_file.name

    @property
    def filename(self):
        tags = self.app.tags.all()
        tag_string = ""
        if tags.count() > 0:
            tag_string = "-"
            tag_string += ("-".join([t.name for t in tags]))

        filename = "%s%s-%s%s" % (self.app.product.name, tag_string, self.app.version, self.asset_type.extension)
        return filename

    app = models.ForeignKey('App', related_name='assets', null=True)

    primary = models.BooleanField()
    uuid = models.CharField(max_length=255, null=True, blank=True)
    asset_file = models.FileField(upload_to=settings.APP_PACKAGE_ROOT)
    asset_type = models.ForeignKey('AssetType', null=True, blank=True)

       
class AssetType(models.Model):

    @classmethod
    def get_or_create(self, extension):

        try:
            asset_type = AssetType.objects.get(extension=extension)
        except AssetType.DoesNotExist:
            asset_type = AssetType(extension=extension)
            asset_type.save()

        return asset_type

    name = models.CharField(max_length=255, blank=True, null=True)
    extension = models.CharField(max_length=10)

class Tag(models.Model):
{'fraction_non_alphanumeric': 0.10149812734082397, 'fraction_numerical': 0.0050561797752808986, 'mean_word_length': 2.794316163410302, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2622710', 'n_tokens_mistral': 3204, 'n_tokens_neox': 3035, 'n_words': 759}