# 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
        for slug in removed_slugs:
            repository = Repository.objects.get(host_slug = self.host+'/'+slug)
            UserRepositoryLink.objects.get(user = profile, repository = repository, link_type = repo_link_type).delete()

        # finds the highest order value for the category in user repository links
        order = UserRepositoryLink.objects.filter(user=profile).filter(userrepositorylink__link_type = repo_link_type).filter(repository_category=category_object).aggregate(max_order=Max('order'))['max_order']

        # saves the repositories and adds any new ones at an order value above the max for the category
        watched_filtered = []
        for repo in watched:
            try:
                repository = Repository.objects.get(host_slug=self.host+'/'+repo['owner'].lower()+'/'+repo['name'].lower())
            except ObjectDoesNotExist:
                repository = Repository()
            repository = self.create_or_update_repository_details(repo, repository)
            if not repository.private:
                repository.save()
            try:
                user_repository_link = UserRepositoryLink.objects.get(user = profile,repository = repository, link_type = repo_link_type)
            except ObjectDoesNotExist:
                if repository.language == category_lower:
                    order = order + 1
                    watched_filtered.append(repository)
                    user_repository_link = UserRepositoryLink(user = profile,repository = repository, order = order, link_type = repo_link_type)
                    user_repository_link.repository_category,_ = RepositoryCategory.objects.get_or_create(name = repository.language)
                    user_repository_link.save()
#            if not repository.private:
#                RepositoryUserRepositoryLink.objects.get_or_create(user = repository_user, repository = repository, owned = owned, starred = starred)
        if repo_link_type.name == "owned":
            repository_user.public_repos = len(watched)

# 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

        for slug in removed_slugs:
            repository = Repository.objects.get(host_slug = self.host+'/'+slug)
            UserRepositoryLink.objects.get(user = profile, repository = repository, link_type = repo_link_type).delete()

        # finds the highest order value for the category in user repository links
        order = UserRepositoryLink.objects.filter(user=profile).filter(userrepositorylink__link_type = repo_link_type).filter(repository_category=category_object).aggregate(max_order=Max('order'))['max_order']

        # saves the repositories and adds any new ones at an order value above the max for the category
        watched_filtered = []
        for repo in watched:
            try:
                repository = Repository.objects.get(host_slug=self.host+'/'+repo['owner'].lower()+'/'+repo['name'].lower())
            except ObjectDoesNotExist:
                repository = Repository()
            repository = self.create_or_update_repository_details(repo, repository)
            if not repository.private:
                repository.save()
            try:
                user_repository_link = UserRepositoryLink.objects.get(user = profile,repository = repository, link_type = repo_link_type)
            except ObjectDoesNotExist:
                if repository.language == category_lower:
                    order = order + 1
                    watched_filtered.append(repository)
                    user_repository_link = UserRepositoryLink(user = profile,repository = repository, order = order, link_type = repo_link_type)
                    user_repository_link.repository_category,_ = RepositoryCategory.objects.get_or_create(name = repository.language)
                    user_repository_link.save()
#            if not repository.private:
#                RepositoryUserRepositoryLink.objects.get_or_create(user = repository_user, repository = repository, owned = owned, starred = starred)
        if repo_link_type.name == "owned":
            repository_user.public_repos = len(watched)

        elif repo_link_type.name == "starred":