<commit_before>from __future__ import division

from daploader import dapver
from django.conf import settings
from django.contrib.auth import models as auth_models
from django.core import urlresolvers
from django.core import validators
from django.db import models
from django.db.models import signals
from django.dispatch import receiver
from social.apps.django_app.default import models as social_models
from taggit import managers as taggit_managers


class MetaDap(models.Model):
    '''Model represents a dap in no version.
    It holds the (meta)data related to all versions'''
    package_name = models.CharField(max_length=200, unique=True)
    user = models.ForeignKey(auth_models.User)
    comaintainers = models.ManyToManyField(auth_models.User, null=True, blank=True,
                                           default=None, related_name='codap_set')
    latest = models.ForeignKey('Dap', null=True, blank=True, default=None, related_name='+',
                               on_delete=models.SET_DEFAULT)
    latest_stable = models.ForeignKey('Dap', null=True, blank=True, default=None,
                                      related_name='+', on_delete=models.SET_DEFAULT)
    tags = taggit_managers.TaggableManager(blank=True)
    active = models.BooleanField(default=True)
    ranks = models.ManyToManyField(auth_models.User, through='Rank', related_name='ranked_set',
                                   null=True, blank=True, default=None)
    reports = models.ManyToManyField(auth_models.User, through='Report',
                                     related_name='reported_set', null=True,
                                     blank=True, default=None)
    average_rank = models.FloatField(null=True, blank=True, default=None)
    rank_count = models.IntegerField(default=0)

    def __unicode__(self):
        '''Returns package name'''
        return self.package_name

    def sorted_versions(self):
        '''Returns a sorted list of version strings by the rules defined in daploader'''
        return sorted([dap.version for dap in self.dap_set.all()], cmp=dapver.compare, reverse=True)

    def _get_latest(self):
        '''Returns the dap with the latest version (if any).
        The latest attribute should be used to obtain this value from DB.'''
        versions = self.sorted_versions()
        if not versions:
            return None
        return Dap.objects.get(metadap=self, version=versions[0])

    def _get_latest_stable(self):
        '''Returns the dap with the latest stable version (if any).
        The latest_stable attribute should be used to obtain this value from DB.'''
        versions = self.sorted_versions()
        for version in versions:
            if version[-1].isdigit():
                return Dap.objects.get(metadap=self, version=version)
        return None

    def similar_active_daps(self):
        '''Returns active daps with similar tags'''
        return [dap for dap in self.tags.similar_objects() if dap.active]

    def similar_active_daps_api(self):
        '''Dirty trick to return API links of similar daps'''
        return [settings.SITE_URL +
                urlresolvers.reverse('metadap-detail', args=(dap.package_name, ))
                for dap in self.similar_active_daps()]

    def _get_average_rank(self):
        '''Calculates the average rank of the dap.
        The average_rank attribute should be used to obtain this value from DB.'''
        total = 0
        for rank in self.rank_set.all():
            total += rank.rank
        if not total:
            return None
        return total / self.rank_set.count()

    def _get_rank_count(self):
        '''Gets the count of ranks of the dap.
        The rank_count attribute should be used to obtain this value from DB.'''
        return self.rank_set.count()

    def get_unsolved_reports_count(self):
        '''Gets the number of active reports'''
        return self.report_set.filter(solved=False).count()

    def get_human_link(self, absolute=True):
        '''Gets the link to website, where the latest dap lives'''
        link = urlresolvers.reverse('dapi.views.dap', args=(self.package_name, ))
        if absolute:
            return settings.SITE_URL + link
        return link


class Dap(models.Model):
    '''Model representing a specific version of a dap (of MetaDap instance)'''
    file = models.FileField(upload_to=lambda instance, filename: filename)
    metadap = models.ForeignKey(MetaDap)
    version = models.CharField(max_length=200)
    license = models.CharField(max_length=200)
    homepage = models.CharField(max_length=200, blank=True)
    bugreports = models.CharField(max_length=200, blank=True)
    summary = models.CharField(max_length=500)
    description = models.TextField(blank=True)

    def __unicode__(self):
        '''Returns dap's name followed by a dash and version'''
        return self.metadap.package_name + '-' + self.version

    def is_pre(self):
        '''Returns True if this is a pre_release'''
        return not self.version[-1].isdigit()

    def is_latest(self):
        '''Whether the Dap is latest version of it's MetaDap.'''
        return self == self.metadap.latest

    def is_latest_stable(self):
        '''Whether the Dap is latest stable version of it's MetaDap.'''
        return self == self.metadap.latest_stable

    def get_authors(self):
        '''Returns the list of authors as strings'''
        return [author.author for author in self.author_set.all()]

    def bugreports_link(self):
        '''Returns URL to add to bugreports link'''
        if not self.bugreports.startswith('http'):
            return 'mailto:' + self.bugreports
        else:
            return self.bugreports

    def get_human_link(self, absolute=True):
        '''Gets the link to website, where this dap lives'''
        link = urlresolvers.reverse('dapi.views.dap_version',
                                    args=(self.metadap.package_name, self.version))
        if absolute:
            return settings.SITE_URL + link
        return link

    class Meta:
        unique_together = ('metadap', 'version',)


class Author(models.Model):
    '''Author field of dap's metadata.
    It allows to associate more authors to one dap.
    More authors of the same name and address are kept in the DB, but that's OK.'''
    dap = models.ForeignKey(Dap)
    author = models.CharField(max_length=200)
<commit_msg>Do not add mailto: to ftp:// links<commit_after>from __future__ import division

from daploader import dapver
from django.conf import settings
from django.contrib.auth import models as auth_models
from django.core import urlresolvers
from django.core import validators
from django.db import models
from django.db.models import signals
from django.dispatch import receiver
from social.apps.django_app.default import models as social_models
from taggit import managers as taggit_managers


class MetaDap(models.Model):
    '''Model represents a dap in no version.
    It holds the (meta)data related to all versions'''
    package_name = models.CharField(max_length=200, unique=True)
    user = models.ForeignKey(auth_models.User)
    comaintainers = models.ManyToManyField(auth_models.User, null=True, blank=True,
                                           default=None, related_name='codap_set')
    latest = models.ForeignKey('Dap', null=True, blank=True, default=None, related_name='+',
                               on_delete=models.SET_DEFAULT)
    latest_stable = models.ForeignKey('Dap', null=True, blank=True, default=None,
                                      related_name='+', on_delete=models.SET_DEFAULT)
    tags = taggit_managers.TaggableManager(blank=True)
    active = models.BooleanField(default=True)
    ranks = models.ManyToManyField(auth_models.User, through='Rank', related_name='ranked_set',
                                   null=True, blank=True, default=None)
    reports = models.ManyToManyField(auth_models.User, through='Report',
                                     related_name='reported_set', null=True,
                                     blank=True, default=None)
    average_rank = models.FloatField(null=True, blank=True, default=None)
    rank_count = models.IntegerField(default=0)

    def __unicode__(self):
        '''Returns package name'''
        return self.package_name

    def sorted_versions(self):
        '''Returns a sorted list of version strings by the rules defined in daploader'''
        return sorted([dap.version for dap in self.dap_set.all()], cmp=dapver.compare, reverse=True)

    def _get_latest(self):
        '''Returns the dap with the latest version (if any).
        The latest attribute should be used to obtain this value from DB.'''
        versions = self.sorted_versions()
        if not versions:
            return None
        return Dap.objects.get(metadap=self, version=versions[0])

    def _get_latest_stable(self):
        '''Returns the dap with the latest stable version (if any).
        The latest_stable attribute should be used to obtain this value from DB.'''
        versions = self.sorted_versions()
        for version in versions:
            if version[-1].isdigit():
                return Dap.objects.get(metadap=self, version=version)
        return None

    def similar_active_daps(self):
        '''Returns active daps with similar tags'''
        return [dap for dap in self.tags.similar_objects() if dap.active]

    def similar_active_daps_api(self):
        '''Dirty trick to return API links of similar daps'''
        return [settings.SITE_URL +
                urlresolvers.reverse('metadap-detail', args=(dap.package_name, ))
                for dap in self.similar_active_daps()]

    def _get_average_rank(self):
        '''Calculates the average rank of the dap.
        The average_rank attribute should be used to obtain this value from DB.'''
        total = 0
        for rank in self.rank_set.all():
            total += rank.rank
        if not total:
            return None
        return total / self.rank_set.count()

    def _get_rank_count(self):
        '''Gets the count of ranks of the dap.
        The rank_count attribute should be used to obtain this value from DB.'''
        return self.rank_set.count()

    def get_unsolved_reports_count(self):
        '''Gets the number of active reports'''
        return self.report_set.filter(solved=False).count()

    def get_human_link(self, absolute=True):
        '''Gets the link to website, where the latest dap lives'''
        link = urlresolvers.reverse('dapi.views.dap', args=(self.package_name, ))
        if absolute:
            return settings.SITE_URL + link
        return link


class Dap(models.Model):
    '''Model representing a specific version of a dap (of MetaDap instance)'''
    file = models.FileField(upload_to=lambda instance, filename: filename)
    metadap = models.ForeignKey(MetaDap)
    version = models.CharField(max_length=200)
    license = models.CharField(max_length=200)
    homepage = models.CharField(max_length=200, blank=True)
    bugreports = models.CharField(max_length=200, blank=True)
    summary = models.CharField(max_length=500)
    description = models.TextField(blank=True)

    def __unicode__(self):
        '''Returns dap's name followed by a dash and version'''
        return self.metadap.package_name + '-' + self.version

    def is_pre(self):
        '''Returns True if this is a pre_release'''
        return not self.version[-1].isdigit()

    def is_latest(self):
        '''Whether the Dap is latest version of it's MetaDap.'''
        return self == self.metadap.latest

    def is_latest_stable(self):
        '''Whether the Dap is latest stable version of it's MetaDap.'''
        return self == self.metadap.latest_stable

    def get_authors(self):
        '''Returns the list of authors as strings'''
        return [author.author for author in self.author_set.all()]

    def bugreports_link(self):
        '''Returns URL to add to bugreports link'''
        if not '://' in self.bugreports:
            return 'mailto:' + self.bugreports
        else:
            return self.bugreports

    def get_human_link(self, absolute=True):
        '''Gets the link to website, where this dap lives'''
        link = urlresolvers.reverse('dapi.views.dap_version',
                                    args=(self.metadap.package_name, self.version))
        if absolute:
            return settings.SITE_URL + link
        return link

    class Meta:
        unique_together = ('metadap', 'version',)


class Author(models.Model):
    '''Author field of dap's metadata.
    It allows to associate more authors to one dap.
    More authors of the same name and address are kept in the DB, but that's OK.'''
    dap = models.ForeignKey(Dap)
    author = models.CharField(max_length=200)

{'fraction_non_alphanumeric': 0.09475570794046599, 'fraction_numerical': 0.004052053300085717, 'mean_word_length': 2.7548273844353424, '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': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4291380', 'n_tokens_mistral': 3649, 'n_tokens_neox': 3536, 'n_words': 1069}