# 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 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 LinkType(models.Model):
#     name = CharField(max_length = 100, unique = True, choices = LINK_TYPES)
# 
# Path: repowatcher/main/tasks.py
# @task
# def get_events(url):
#     try:
#         body = urllib2.urlopen(url).read()
#         events = json.loads(body)
#     except Exception:
#         events = None
#     return events


from ProviderBase import ProviderBase
from datetime import datetime
from django.core.exceptions import ObjectDoesNotExist
from django.http import Http404
from oauth_hook.hook import OAuthHook
from operator import itemgetter
from repowatcher.main.models import RepositoryUser, Repository, LinkType
from repowatcher.main.tasks import get_events
import json
import requests
import slumber
import logging
    def create_or_update_user_details(self, user_dict, repository_user = None):
        extra_data = {}
        if repository_user is None:
            repository_user = RepositoryUser()
        if ('first_name' in user_dict and user_dict['first_name']!='')  or ('last_name' in user_dict and user_dict['last_name']!=''):
            if 'first_name' in user_dict and user_dict['first_name']!='' and 'last_name' in user_dict and user_dict['last_name']!='':
                repository_user.name = ' '.join((user_dict['first_name'],user_dict['last_name']))
            elif 'first_name' in user_dict and user_dict['first_name']!='':
                repository_user.name = user_dict['first_name']
            elif 'last_name' in user_dict and user_dict['last_name']!='':
                repository_user.name = user_dict['last_name']
        for key,value in user_dict.iteritems():

            if key == 'username':
                repository_user.login = value
            elif key =='first_name':
                pass
            elif key =='last_name':
                pass
            else:
                if isinstance(value, datetime):
                    extra_data[key] = value.__str__()
                else:
                    extra_data[key] = value

        repository_user.extra_data = json.dumps(extra_data)
        repository_user.host = self.host
        return repository_user

    def retrieve_user_details(self, username):

# 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 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 LinkType(models.Model):
#     name = CharField(max_length = 100, unique = True, choices = LINK_TYPES)
# 
# Path: repowatcher/main/tasks.py
# @task
# def get_events(url):
#     try:
#         body = urllib2.urlopen(url).read()
#         events = json.loads(body)
#     except Exception:
#         events = None
#     return events


# Path: repowatcher/main/views/BitbucketProvider.py
from ProviderBase import ProviderBase
from datetime import datetime
from django.core.exceptions import ObjectDoesNotExist
from django.http import Http404
from oauth_hook.hook import OAuthHook
from operator import itemgetter
from repowatcher.main.models import RepositoryUser, Repository, LinkType
from repowatcher.main.tasks import get_events
import json
import requests
import slumber
import logging

    def create_or_update_user_details(self, user_dict, repository_user = None):
        extra_data = {}
        if repository_user is None:
            repository_user = RepositoryUser()
        if ('first_name' in user_dict and user_dict['first_name']!='')  or ('last_name' in user_dict and user_dict['last_name']!=''):
            if 'first_name' in user_dict and user_dict['first_name']!='' and 'last_name' in user_dict and user_dict['last_name']!='':
                repository_user.name = ' '.join((user_dict['first_name'],user_dict['last_name']))
            elif 'first_name' in user_dict and user_dict['first_name']!='':
                repository_user.name = user_dict['first_name']
            elif 'last_name' in user_dict and user_dict['last_name']!='':
                repository_user.name = user_dict['last_name']
        for key,value in user_dict.iteritems():

            if key == 'username':
                repository_user.login = value
            elif key =='first_name':
                pass
            elif key =='last_name':
                pass
            else:
                if isinstance(value, datetime):
                    extra_data[key] = value.__str__()
                else:
                    extra_data[key] = value

        repository_user.extra_data = json.dumps(extra_data)
        repository_user.host = self.host
        return repository_user

    def retrieve_user_details(self, username):

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