# Path: repowatcher/main/models.py
# 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)


from GithubProvider import GithubProvider
from collections import defaultdict
from datetime import timedelta, datetime
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.views.decorators.cache import never_cache, cache_control
from oauth_hook import OAuthHook
from repowatcher.main.models import RepositoryCategory, Repository
from urllib import urlencode, quote
import json
import logging
import requests
import urllib
                repository = Repository()
            if update or (datetime.now() - repository.last_modified) > timedelta(days = 1):
                repository = github_provider.create_or_update_repository_details(repo, repository)
                if not repository.private:
                    repository.save()
            repositories_by_language[repository.language].append(repository)
        for repo in bitbucket_repositories:
            update = False
            repo['owner'] = repo['owner'].lower().replace("/", "")
            repo['name'] = repo['name'].lower().replace("/", "")
            try:
                repository = Repository.objects.get(slug= repo['owner'] + '/' + repo['name'], host='bitbucket')
            except ObjectDoesNotExist:
                update = True
                repository = Repository()
            if update or (datetime.now() - repository.last_modified) > timedelta(days = 1):
                extra_data = {}
                key_map={'owner':'owner','name':'name', 'website':'homepage','language':'language','description':'description','created_on':'created_at','last_updated':'pushed_at','scm':'scm','is_private':'private'}
                for key,value in repo.iteritems():
                    if key in ['owner','name', 'website','language','description','created_on','last_updated','scm','is_private']:
                        setattr(repository,key_map[key],value)
                    else:
                        extra_data[key] = value
                repository.extra_data = json.dumps(extra_data)
                if repository.language == "" or repository.language == None:
                    repository.language = "other"
                repository.host ='bitbucket'
                if not repository.private:
                    repository.save()
            for category in repositories_by_language.keys():

# Path: repowatcher/main/models.py
# 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)


# Path: repowatcher/main/views/general.py
from GithubProvider import GithubProvider
from collections import defaultdict
from datetime import timedelta, datetime
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.views.decorators.cache import never_cache, cache_control
from oauth_hook import OAuthHook
from repowatcher.main.models import RepositoryCategory, Repository
from urllib import urlencode, quote
import json
import logging
import requests
import urllib

                repository = Repository()
            if update or (datetime.now() - repository.last_modified) > timedelta(days = 1):
                repository = github_provider.create_or_update_repository_details(repo, repository)
                if not repository.private:
                    repository.save()
            repositories_by_language[repository.language].append(repository)
        for repo in bitbucket_repositories:
            update = False
            repo['owner'] = repo['owner'].lower().replace("/", "")
            repo['name'] = repo['name'].lower().replace("/", "")
            try:
                repository = Repository.objects.get(slug= repo['owner'] + '/' + repo['name'], host='bitbucket')
            except ObjectDoesNotExist:
                update = True
                repository = Repository()
            if update or (datetime.now() - repository.last_modified) > timedelta(days = 1):
                extra_data = {}
                key_map={'owner':'owner','name':'name', 'website':'homepage','language':'language','description':'description','created_on':'created_at','last_updated':'pushed_at','scm':'scm','is_private':'private'}
                for key,value in repo.iteritems():
                    if key in ['owner','name', 'website','language','description','created_on','last_updated','scm','is_private']:
                        setattr(repository,key_map[key],value)
                    else:
                        extra_data[key] = value
                repository.extra_data = json.dumps(extra_data)
                if repository.language == "" or repository.language == None:
                    repository.language = "other"
                repository.host ='bitbucket'
                if not repository.private:
                    repository.save()
            for category in repositories_by_language.keys():

                RepositoryCategory.objects.get_or_create(name = category)