# 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
                        pass
                    elif repo_link_type.name == "starred":
                        repository_user.starred = repository_links_size
                    elif repo_link_type.name == "watched":
                        repository_user.watched = repository_links_size
                    for repository_link in repository_links:
                        repositories.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, repository_user

    def retrieve_repositories_dict(self, username, link_type):
        repositories_by_language = defaultdict(list)
        repo_link_type, _ = LinkType.objects.get_or_create(name = link_type)
        api_only = True
        if self.user.is_authenticated():
            try:
                if username == self.user.social_auth.get(provider=self.host).extra_data['username']:
                    profile = self.user.get_profile()
                    links = profile.userrepositorylink_set.filter(link_type = repo_link_type).filter(repository__host=self.host).select_related('repository').select_related('repository','repository_category')
                    if len(links) != 0:
                        for link in links:
                            repositories_by_language[link.repository_category.name].append(link.repository)
                        api_only = False
                    else:
                        api_only = True
            except ObjectDoesNotExist:
                pass
        try:

# 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

                        pass
                    elif repo_link_type.name == "starred":
                        repository_user.starred = repository_links_size
                    elif repo_link_type.name == "watched":
                        repository_user.watched = repository_links_size
                    for repository_link in repository_links:
                        repositories.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, repository_user

    def retrieve_repositories_dict(self, username, link_type):
        repositories_by_language = defaultdict(list)
        repo_link_type, _ = LinkType.objects.get_or_create(name = link_type)
        api_only = True
        if self.user.is_authenticated():
            try:
                if username == self.user.social_auth.get(provider=self.host).extra_data['username']:
                    profile = self.user.get_profile()
                    links = profile.userrepositorylink_set.filter(link_type = repo_link_type).filter(repository__host=self.host).select_related('repository').select_related('repository','repository_category')
                    if len(links) != 0:
                        for link in links:
                            repositories_by_language[link.repository_category.name].append(link.repository)
                        api_only = False
                    else:
                        api_only = True
            except ObjectDoesNotExist:
                pass
        try:

            repository_user = RepositoryUser.objects.get(slug=self.host+'/'+username.lower())