<commit_before>from django.contrib.auth.models import User
from django.db import models
from django.urls import reverse
from model_utils.models import TimeStampedModel

from core.behaviors import Expirationable
from main.utils import generate_token


class Plugin(TimeStampedModel, models.Model):
    name = models.CharField(max_length=120)
    description = models.TextField()
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    nb_download = models.IntegerField(default=0)
    nb_upvote = models.IntegerField(default=0)

    def update_from_manifest(self, manifest):
        """
        This fonction update the plugin information from the manifest file
        """
        manifest['description'] = "TODO IMPLEMENT DESCRIPTION"
        self.description = manifest['description']

    @property
    def url(self):
        return reverse('plugins:detail', args=[self.author.username, self.name])

    @property
    def url_download(self):
        return reverse('plugins:download', args=[self.author.username, self.name])

    def user_has_upvoted(self, user):
        query = Upvote.objects.filter(plugin=self, user=user)
        return True if query.count() else False


def plugin_directory_path(instance, filename):
    filename = '{}.zip'.format(str(instance.version))
    return 'plugins/{}/{}/{}'.format(instance.plugin.author.username, instance.plugin.name, filename)


class Release(TimeStampedModel, models.Model):
    plugin = models.ForeignKey(Plugin, on_delete=models.CASCADE)
    version = models.IntegerField(default=0)
    archive = models.FileField(upload_to=plugin_directory_path)


class DownloadRelease(Expirationable, TimeStampedModel, models.Model):<commit_msg>fix error 500 plugin detail if user not authenticated<commit_after>from django.contrib.auth.models import User
from django.db import models
from django.urls import reverse
from model_utils.models import TimeStampedModel

from core.behaviors import Expirationable
from main.utils import generate_token


class Plugin(TimeStampedModel, models.Model):
    name = models.CharField(max_length=120)
    description = models.TextField()
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    nb_download = models.IntegerField(default=0)
    nb_upvote = models.IntegerField(default=0)

    def update_from_manifest(self, manifest):
        """
        This fonction update the plugin information from the manifest file
        """
        manifest['description'] = "TODO IMPLEMENT DESCRIPTION"
        self.description = manifest['description']

    @property
    def url(self):
        return reverse('plugins:detail', args=[self.author.username, self.name])

    @property
    def url_download(self):
        return reverse('plugins:download', args=[self.author.username, self.name])

    def user_has_upvoted(self, user):
        if not user.is_authenticated():
            return False
        query = Upvote.objects.filter(plugin=self, user=user)
        return True if query.count() else False


def plugin_directory_path(instance, filename):
    filename = '{}.zip'.format(str(instance.version))
    return 'plugins/{}/{}/{}'.format(instance.plugin.author.username, instance.plugin.name, filename)


class Release(TimeStampedModel, models.Model):
    plugin = models.ForeignKey(Plugin, on_delete=models.CASCADE)
    version = models.IntegerField(default=0)
    archive = models.FileField(upload_to=plugin_directory_path)


class DownloadRelease(Expirationable, TimeStampedModel, models.Model):
{'fraction_non_alphanumeric': 0.10333999429060806, 'fraction_numerical': 0.004282043962318013, 'mean_word_length': 4.333333333333333, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3721345', 'n_tokens_mistral': 996, 'n_tokens_neox': 958, 'n_words': 224}