class Tag(models.Model):
    identity = models.CharField(max_length=127, editable=False)
    name = models.CharField(max_length=127)

    def __str__(self):
        return self.name

    def get_tag_name(self):
        return self.name class App(models.Model):
    TYPES = [('F', 'Fork'),
             ('M', 'Module'),
             ]
    name = models.CharField(max_length=127, unique=True)
    title = models.CharField(max_length=127, unique=True)
    app_type = models.CharField(max_length=6, choices=TYPES)
    abstract = models.CharField(max_length=255, default="NA")
    description = MarkdownxField()
    icon = models.ImageField(
        upload_to='app_icon_thumbnail/%Y-%m-%d/',
        blank=True)
    tags = models.ManyToManyField(Tag, blank=True)
    editors = models.ManyToManyField(settings.AUTH_USER_MODEL)
    latest_release_date = models.DateField(auto_now_add=True)
    website = models.URLField(blank=True, null=True)
    documentation = models.URLField(blank=True, null=True)
    coderepo = models.URLField(blank=True, null=True)
    contact = models.EmailField(blank=True, null=True)
    active = models.BooleanField(default=False)
    stars = models.PositiveIntegerField(default=0)
    votes = models.PositiveIntegerField(default=0)
    downloads = models.PositiveIntegerField(default=0)
    has_releases = models.BooleanField(default=False)
    issue = models.URLField(blank=True, null=True)
    mailing_list = models.URLField(blank=True, null=True)

    def __str__(self):
        return self.title

    @property
    def stars_percentage(self):
        return 100 * self.stars / self.votes / 5 if self.votes != 0 else 0

    def update_latest_release_date(self):
        self.latest_release_date = (
            Release.objects.filter(
                app=self).latest('date')).date
        self.save() class Release(models.Model):
    app = models.ForeignKey(App, on_delete=models.CASCADE)
    version = models.CharField(max_length=31)
    require = models.ForeignKey(
        NsRelease,
        blank=True,
        null=True,
        on_delete=models.CASCADE)
    date = models.DateField(default=datetime.now, blank=True)
    notes = MarkdownxField()
    filename = models.FileField(
        upload_to='release_files/',
        blank=True,
        null=True)
    url = models.URLField(blank=True, null=True)

    def __str__(self):
        return '%s %s' % (self.app.title, self.version) class NsRelease(models.Model):
    name = models.CharField(max_length=5)
    url = models.URLField(default="https://www.nsnam.org/ns-3.26/")

    def __str__(self):
        return u'%s-%s' % ("ns", self.name)

    class Meta:
        ordering = ['-name'] class Comment(models.Model):
    app = models.ForeignKey(App, on_delete=models.CASCADE)
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE)
    title = models.CharField(max_length=50)
    content = MarkdownxField()
    timestamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return 'Comment on %s by %s' % (
            self.app.title, self.user.get_full_name) class Screenshot(models.Model):
    app = models.ForeignKey(App, on_delete=models.CASCADE)
    screenshot = models.ImageField(upload_to='screenshot/%Y-%m-%d/')
    thumbnail = models.ImageField(upload_to='thumbnail/%Y-%m-%d/')

    def __str__(self):
        return 'Screenshot of %s' % (self.app.title) class Maintenance(models.Model):
    app = models.OneToOneField(App, on_delete=models.CASCADE)
    notes = MarkdownxField()

    def __str__(self):
        return '%s Maintenance' % (self.app.title)

    class Meta:
        verbose_name = 'Maintenance'
        verbose_name_plural = 'Maintenance Notes' class Installation(models.Model):
    app = models.OneToOneField(App, on_delete=models.CASCADE)
    installation = MarkdownxField()

    def __str__(self):
        return '%s Installation' % (self.app.title)

    class Meta:
        verbose_name = 'Installation'
        verbose_name_plural = 'Installation Notes' class Download(models.Model):
    CHOICES = [('I', 'Point to the Installation tab'),
               ('D', 'Point to the Default Release'),
               ('U', 'Point to a URL of your Choice'),
               ]
    app = models.OneToOneField(App, on_delete=models.CASCADE)
    download_option = models.CharField(
        max_length=1, default='I', choices=CHOICES)
    default_release = models.OneToOneField(Release, on_delete=models.CASCADE)
    external_url = models.URLField(blank=True, null=True)
    download_link = models.URLField(editable=False)

    def __str__(self):
        return '%s Download Details' % (self.app.title) class Development(models.Model):
    app = models.OneToOneField(App, on_delete=models.CASCADE)
    notes = MarkdownxField()
    filename = models.FileField(
        upload_to='release_files/',
        blank=True,
        null=True)

    def __str__(self):
        return '%s Development Version' % (self.app.title)

    class Meta:
        verbose_name = 'Development Version'
        verbose_name_plural = 'Development Versions'
from django.contrib import admin
from .models import Tag, App, Release, NsRelease, Comment, Screenshot, \
    Maintenance, Installation, Download, Development
# -*- coding: utf-8 -*-
from __future__ import unicode_literals



# Register your models here.
admin.site.register(Tag)
admin.site.register(App)
admin.site.register(Release)
admin.site.register(NsRelease)
admin.site.register(Comment)
admin.site.register(Screenshot)
admin.site.register(Maintenance)
admin.site.register(Installation)
admin.site.register(Download)

admin.site.register(Development)