# Path: repowatcher/main/models.py
# class RepositoryUser(models.Model):
#     login = CharField(max_length=100,db_index = True)
#     name = CharField(max_length=100, null = True)
#     slug = SlugField(max_length=201, unique = True)
#     email = EmailField(max_length=254, null = True)
#     blog = URLField(null = True)
#     followers = PositiveIntegerField(null = True)
#     following = PositiveIntegerField(null = True)
#     public_repos = PositiveIntegerField(null = True)
#     created_at = DateTimeField(null=True)
#     extra_data = JSONField(null = True)
#     last_modified = DateTimeField(auto_now=True)
#     repositories = models.ManyToManyField(Repository, through='RepositoryUserRepositoryLink')
#     starred = PositiveIntegerField(null = True)
#     watched = PositiveIntegerField(null = True)
#     host = CharField(max_length=100, choices=HOST_CHOICES, db_index = True)
# 
#     class Meta:
#         unique_together = ("login", "host")
# 
#     def save(self, *args, **kwargs):
#         self.slug = self.host + '/' + self.login.lower()
#         super(RepositoryUser, self).save(*args, **kwargs)
# 
# class RepositoryCategory(models.Model):
#     name = CharField(max_length=100)
# 
# class Repository(models.Model):
#     owner = CharField(max_length=100)
#     name = CharField(max_length=100)
#     slug = SlugField(max_length=201)
#     host_slug = SlugField(max_length=302, unique = True)
#     language = CharField(max_length=100, null = True)
#     html_url = URLField(null = True, max_length=400)
#     homepage = URLField(null = True, max_length=400)
#     watchers = PositiveIntegerField(null = True)
#     created_at = DateTimeField(null = True)
#     pushed_at = DateTimeField(null = True)
#     description = TextField(null = True)
#     extra_data = JSONField(null = True)
#     last_modified = DateTimeField(auto_now=True)
#     scm = CharField(max_length=100, choices=SCM_CHOICES, null = True)
#     host = CharField(max_length=100, choices=HOST_CHOICES)
#     private = BooleanField(default = False)
# 
#     class Meta:
#         unique_together = ("owner", "name", "host")
#         ordering = ['-watchers']
# 
#     def save(self, *args, **kwargs):
#         self.slug = self.owner.lower() + '/' + self.name.lower()
#         self.host_slug = self.host+'/'+self.slug
#         if self.html_url == None or self.html_url =='':
#             if self.host =='bitbucket':
#                 self.html_url = 'https://bitbucket.org/%s/%s' % (self.owner,self.name)
#             if self.host =='github':
#                 self.html_url = 'https://github.com/%s/%s' % (self.owner,self.name)
# 
#         super(Repository, self).save(*args, **kwargs)
# 
# class UserRepositoryLink(models.Model):
#     user = ForeignKey(UserProfile)
#     repository = ForeignKey(Repository)
#     order = PositiveIntegerField()
#     repository_category = ForeignKey(RepositoryCategory)
#     link_type = ForeignKey(LinkType)
#     last_modified = DateTimeField(auto_now=True)
# 
#     class Meta:
#         ordering = ['repository_category__name','order']
#         unique_together = ("user", "repository", 'link_type')
# 
# class RepositoryUserRepositoryLink(models.Model):
#     user = ForeignKey(RepositoryUser)
#     repository = ForeignKey(Repository)
#     last_modified = DateTimeField(auto_now=True)
#     link_type = ForeignKey(LinkType)
# 
#     class Meta:
#         ordering = ['repository__language', '-repository__watchers']
#         unique_together = ("user", "repository", "link_type")
# 
# class LinkType(models.Model):
#     name = CharField(max_length = 100, unique = True, choices = LINK_TYPES)


from abc import ABCMeta, abstractmethod
from collections import defaultdict
from datetime import datetime, timedelta
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.aggregates import Max, Min
from repowatcher.main.models import RepositoryUser, RepositoryCategory, \
    Repository, UserRepositoryLink, RepositoryUserRepositoryLink, LinkType
import logging
import urllib
            except ObjectDoesNotExist:
                pass
        try:
            repository_user = RepositoryUser.objects.get(slug=self.host+'/'+username.lower())
            if api_only:
                repository_links = repository_user.repositoryuserrepositorylink_set.filter(link_type = repo_link_type).filter(repository__host=self.host).select_related('repository')
                oldest_modification = repository_links.aggregate(oldest_modification=Min('last_modified'))['oldest_modification']
                if len(repository_links)==0 or (datetime.now() - oldest_modification) > timedelta(days = 1):
                    repository_links.delete()
                else:
                    for repository_link in repository_links:
                        repositories_by_language[repository_link.repository.language].append(repository_link.repository)
        except ObjectDoesNotExist:
            user_dict = self.get_user_details(username)
            repository_user = self.create_or_update_user_details(user_dict)
        return repositories_by_language, repository_user

    def retrieve_watched_repositories_list(self, username):
        return self.retrieve_repositories_list(username = username, link_type = "watched")

    def retrieve_starred_repositories_list(self, username):
        return self.retrieve_repositories_list(username = username, link_type = "starred")

    def retrieve_owned_repositories_list(self, username):
        return self.retrieve_repositories_list(username = username, link_type = "owned")

    def retrieve_watched_repositories_dict(self, username):
        return self.retrieve_repositories_dict(username = username, link_type = "watched")

    def retrieve_owned_repositories_dict(self, username):

# Path: repowatcher/main/models.py
# class RepositoryUser(models.Model):
#     login = CharField(max_length=100,db_index = True)
#     name = CharField(max_length=100, null = True)
#     slug = SlugField(max_length=201, unique = True)
#     email = EmailField(max_length=254, null = True)
#     blog = URLField(null = True)
#     followers = PositiveIntegerField(null = True)
#     following = PositiveIntegerField(null = True)
#     public_repos = PositiveIntegerField(null = True)
#     created_at = DateTimeField(null=True)
#     extra_data = JSONField(null = True)
#     last_modified = DateTimeField(auto_now=True)
#     repositories = models.ManyToManyField(Repository, through='RepositoryUserRepositoryLink')
#     starred = PositiveIntegerField(null = True)
#     watched = PositiveIntegerField(null = True)
#     host = CharField(max_length=100, choices=HOST_CHOICES, db_index = True)
# 
#     class Meta:
#         unique_together = ("login", "host")
# 
#     def save(self, *args, **kwargs):
#         self.slug = self.host + '/' + self.login.lower()
#         super(RepositoryUser, self).save(*args, **kwargs)
# 
# class RepositoryCategory(models.Model):
#     name = CharField(max_length=100)
# 
# class Repository(models.Model):
#     owner = CharField(max_length=100)
#     name = CharField(max_length=100)
#     slug = SlugField(max_length=201)
#     host_slug = SlugField(max_length=302, unique = True)
#     language = CharField(max_length=100, null = True)
#     html_url = URLField(null = True, max_length=400)
#     homepage = URLField(null = True, max_length=400)
#     watchers = PositiveIntegerField(null = True)
#     created_at = DateTimeField(null = True)
#     pushed_at = DateTimeField(null = True)
#     description = TextField(null = True)
#     extra_data = JSONField(null = True)
#     last_modified = DateTimeField(auto_now=True)
#     scm = CharField(max_length=100, choices=SCM_CHOICES, null = True)
#     host = CharField(max_length=100, choices=HOST_CHOICES)
#     private = BooleanField(default = False)
# 
#     class Meta:
#         unique_together = ("owner", "name", "host")
#         ordering = ['-watchers']
# 
#     def save(self, *args, **kwargs):
#         self.slug = self.owner.lower() + '/' + self.name.lower()
#         self.host_slug = self.host+'/'+self.slug
#         if self.html_url == None or self.html_url =='':
#             if self.host =='bitbucket':
#                 self.html_url = 'https://bitbucket.org/%s/%s' % (self.owner,self.name)
#             if self.host =='github':
#                 self.html_url = 'https://github.com/%s/%s' % (self.owner,self.name)
# 
#         super(Repository, self).save(*args, **kwargs)
# 
# class UserRepositoryLink(models.Model):
#     user = ForeignKey(UserProfile)
#     repository = ForeignKey(Repository)
#     order = PositiveIntegerField()
#     repository_category = ForeignKey(RepositoryCategory)
#     link_type = ForeignKey(LinkType)
#     last_modified = DateTimeField(auto_now=True)
# 
#     class Meta:
#         ordering = ['repository_category__name','order']
#         unique_together = ("user", "repository", 'link_type')
# 
# class RepositoryUserRepositoryLink(models.Model):
#     user = ForeignKey(RepositoryUser)
#     repository = ForeignKey(Repository)
#     last_modified = DateTimeField(auto_now=True)
#     link_type = ForeignKey(LinkType)
# 
#     class Meta:
#         ordering = ['repository__language', '-repository__watchers']
#         unique_together = ("user", "repository", "link_type")
# 
# class LinkType(models.Model):
#     name = CharField(max_length = 100, unique = True, choices = LINK_TYPES)


# Path: repowatcher/main/views/ProviderBase.py
from abc import ABCMeta, abstractmethod
from collections import defaultdict
from datetime import datetime, timedelta
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.aggregates import Max, Min
from repowatcher.main.models import RepositoryUser, RepositoryCategory, \
    Repository, UserRepositoryLink, RepositoryUserRepositoryLink, LinkType
import logging
import urllib

            except ObjectDoesNotExist:
                pass
        try:
            repository_user = RepositoryUser.objects.get(slug=self.host+'/'+username.lower())
            if api_only:
                repository_links = repository_user.repositoryuserrepositorylink_set.filter(link_type = repo_link_type).filter(repository__host=self.host).select_related('repository')
                oldest_modification = repository_links.aggregate(oldest_modification=Min('last_modified'))['oldest_modification']
                if len(repository_links)==0 or (datetime.now() - oldest_modification) > timedelta(days = 1):
                    repository_links.delete()
                else:
                    for repository_link in repository_links:
                        repositories_by_language[repository_link.repository.language].append(repository_link.repository)
        except ObjectDoesNotExist:
            user_dict = self.get_user_details(username)
            repository_user = self.create_or_update_user_details(user_dict)
        return repositories_by_language, repository_user

    def retrieve_watched_repositories_list(self, username):
        return self.retrieve_repositories_list(username = username, link_type = "watched")

    def retrieve_starred_repositories_list(self, username):
        return self.retrieve_repositories_list(username = username, link_type = "starred")

    def retrieve_owned_repositories_list(self, username):
        return self.retrieve_repositories_list(username = username, link_type = "owned")

    def retrieve_watched_repositories_dict(self, username):
        return self.retrieve_repositories_dict(username = username, link_type = "watched")

    def retrieve_owned_repositories_dict(self, username):

        return self.retrieve_repositories_dict(username = username, link_type = "owned")