# Path: repowatcher/main/models.py
# 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 RepositoryCategory(models.Model):
#     name = CharField(max_length=100)
# 
# 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 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/utils.py
# def expire_view_cache(view_name, args = [], kwargs={}, namespace=None, key_prefix=None):
#     """
#     This function allows you to invalidate any view-level cache. 
#         view_name: view function you wish to invalidate or it's named url pattern
#         args: any arguments passed to the view function
#         namepace: optioal, if an application namespace is needed
#         key prefix: for the @cache_page decorator for the function (if any)
#     """
# 
#     # create a fake request object
#     request = HttpRequest()
#     # Loookup the request path:
#     if namespace:
#         view_name = namespace + ":" + view_name
#     request.path = reverse(view_name, args = args, kwargs=kwargs)
#     # get cache key, expire if the cached item exists:
#     key = cache.make_key(get_cache_key(request, key_prefix=key_prefix))
#     if key:
#         if cache.get(key):
#             cache.delete(key)
#             return True
#     return False


from GithubProvider import GithubProvider
from collections import defaultdict
from datetime import timedelta, datetime
from django.contrib.auth.decorators import login_required
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 repowatcher.main.decorators import ajax_required
from repowatcher.main.models import Repository, UserRepositoryLink, \
    RepositoryCategory, RepositoryUser, RepositoryUserRepositoryLink, LinkType
from repowatcher.main.utils import expire_view_cache
import hashlib
import json
import logging
import urllib
logger = logging.getLogger(__name__)

@never_cache
def anonymous_github_user(request):
    username = request.GET.get('username', '')
    return HttpResponseRedirect(reverse(github_username, kwargs={'username': username}))


@login_required
@never_cache

# Path: repowatcher/main/models.py
# 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 RepositoryCategory(models.Model):
#     name = CharField(max_length=100)
# 
# 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 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/utils.py
# def expire_view_cache(view_name, args = [], kwargs={}, namespace=None, key_prefix=None):
#     """
#     This function allows you to invalidate any view-level cache. 
#         view_name: view function you wish to invalidate or it's named url pattern
#         args: any arguments passed to the view function
#         namepace: optioal, if an application namespace is needed
#         key prefix: for the @cache_page decorator for the function (if any)
#     """
# 
#     # create a fake request object
#     request = HttpRequest()
#     # Loookup the request path:
#     if namespace:
#         view_name = namespace + ":" + view_name
#     request.path = reverse(view_name, args = args, kwargs=kwargs)
#     # get cache key, expire if the cached item exists:
#     key = cache.make_key(get_cache_key(request, key_prefix=key_prefix))
#     if key:
#         if cache.get(key):
#             cache.delete(key)
#             return True
#     return False


# Path: repowatcher/main/views/github.py
from GithubProvider import GithubProvider
from collections import defaultdict
from datetime import timedelta, datetime
from django.contrib.auth.decorators import login_required
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 repowatcher.main.decorators import ajax_required
from repowatcher.main.models import Repository, UserRepositoryLink, \
    RepositoryCategory, RepositoryUser, RepositoryUserRepositoryLink, LinkType
from repowatcher.main.utils import expire_view_cache
import hashlib
import json
import logging
import urllib

logger = logging.getLogger(__name__)

@never_cache
def anonymous_github_user(request):
    username = request.GET.get('username', '')
    return HttpResponseRedirect(reverse(github_username, kwargs={'username': username}))


@login_required
@never_cache

def github(request):