# 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 django.contrib.auth.models import User
from django.db import models
from django.db.models.fields import EmailField, URLField, CharField, TextField, \
    PositiveIntegerField, SlugField, DateTimeField, BooleanField
from django.db.models.fields.related import ForeignKey
from django.db.models.signals import post_save, pre_save
from repowatcher.main.utils import expire_view_cache
from social_auth.fields import JSONField
import hashlib
import logging
    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)

LINK_TYPES = (('starred','starred'),
              ('watched','wathed'),
              ('owned', 'owned'))

class LinkType(models.Model):
    name = CharField(max_length = 100, unique = True, choices = LINK_TYPES)

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 UserProfile(models.Model):
    user = models.OneToOneField(User)

# 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/models.py
from django.contrib.auth.models import User
from django.db import models
from django.db.models.fields import EmailField, URLField, CharField, TextField, \
    PositiveIntegerField, SlugField, DateTimeField, BooleanField
from django.db.models.fields.related import ForeignKey
from django.db.models.signals import post_save, pre_save
from repowatcher.main.utils import expire_view_cache
from social_auth.fields import JSONField
import hashlib
import logging

    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)

LINK_TYPES = (('starred','starred'),
              ('watched','wathed'),
              ('owned', 'owned'))

class LinkType(models.Model):
    name = CharField(max_length = 100, unique = True, choices = LINK_TYPES)

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 UserProfile(models.Model):
    user = models.OneToOneField(User)

    repositories = models.ManyToManyField(Repository, through='UserRepositoryLink')