Ejemplo n.º 1
0
def github_repo_star(request, owner, repo):
    user = request.user
    if user.is_authenticated():
        try:
            github_provider = GithubProvider(user)
            github_provider.star(owner, repo)
            data= {'outcome':'success'}
            github_username = None
            bitbucket_username = None
            try:
                github_username = user.social_auth.get(provider='github').extra_data['username']
                github_prefix = github_username
            except:
                github_prefix = ''
            try:
                bitbucket_username = user.social_auth.get(provider='bitbucket').extra_data['username']
                bitbucket_prefix=bitbucket_username
            except:
                bitbucket_prefix = ''
            custom_prefix = '.'.join((hashlib.md5(github_prefix).hexdigest(),hashlib.md5(bitbucket_prefix).hexdigest()))
            expire_view_cache('repowatcher.main.views.github_repo', kwargs = {'owner':owner,'repo':repo}, key_prefix=custom_prefix)
            return HttpResponse(json.dumps(data),mimetype="application/json")
        except ObjectDoesNotExist:
            pass
    res = HttpResponse("Unauthorized")
    res.status_code = 401
    return res
Ejemplo n.º 2
0
def github_repo_unstar(request, owner, repo):
    repo_link_type, _ = LinkType.objects.get_or_create(name = "starred")
    user = request.user
    if user.is_authenticated():
        try:
            host_slug = ('/'.join(('github',owner,repo))).lower()
            profile = user.get_profile()
            repository = Repository.objects.get(host_slug= host_slug)
            UserRepositoryLink.objects.filter(user = profile).filter(repository = repository).filter(link_type = repo_link_type).delete()
            github_provider = GithubProvider(user)
            github_provider.unstar(owner, repo)
            data= {'outcome':'success'}
            github_username = None
            bitbucket_username = None
            try:
                github_username = user.social_auth.get(provider='github').extra_data['username']
                github_prefix = github_username
            except:
                github_prefix = ''
            try:
                bitbucket_username = user.social_auth.get(provider='bitbucket').extra_data['username']
                bitbucket_prefix=bitbucket_username
            except:
                bitbucket_prefix = ''
            custom_prefix = '.'.join((hashlib.md5(github_prefix).hexdigest(),hashlib.md5(bitbucket_prefix).hexdigest()))
            expire_view_cache('repowatcher.main.views.github_repo', kwargs = {'owner':owner,'repo':repo}, key_prefix=custom_prefix)
            return HttpResponse(json.dumps(data),mimetype="application/json")
        except ObjectDoesNotExist:
            pass
    res = HttpResponse("Unauthorized")
    res.status_code = 401
    return res
Ejemplo n.º 3
0
def github_username_watched(request,username, link_type):
    """has all github repos for a username"""
    owned = False
    starred = False
    if link_type == "owned":
        owned = True
    elif link_type == "starred":
        starred = True
    repo_link_type, _ = LinkType.objects.get_or_create(name = link_type)
    username = urllib.unquote(username)
    user = request.user
    github_provider = GithubProvider(user)

    repositories_by_language, repository_user = github_provider.retrieve_repositories_dict(username, link_type = link_type)
    repository_user.save()
    if len(repositories_by_language) == 0:
        watched = github_provider.get_repositories(username, link_type = link_type)
        count = 0
        for repo in watched:
            update = False
            try:
                repository = Repository.objects.get(host_slug= 'github/'+repo['owner'].lower() + '/' + repo['name'].lower())
            except ObjectDoesNotExist:
                update = True
                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()
            if not repository.private:
                count += 1
                RepositoryUserRepositoryLink.objects.get_or_create(user = repository_user, repository = repository, link_type = repo_link_type)

            repositories_by_language[repository.language].append(repository)
        if repo_link_type.name == "owned":
            owned = True
            pass
        elif repo_link_type.name == "starred":
            repository_user.starred = count
            starred = True
        elif repo_link_type.name == "watched":
            repository_user.watched = count
        repository_user.save()
        for category in repositories_by_language.keys():
            RepositoryCategory.objects.get_or_create(name = category)
            repositories_by_language[category].sort(key=lambda x: x.watchers, reverse = True)
    return render_to_response('github_username_watched.html', {'username':username,'repositories_by_language':sorted(dict(repositories_by_language).iteritems(),key=lambda (k, v): len(v),reverse = True),'owned':owned,"starred":starred},context_instance=RequestContext(request))
Ejemplo n.º 4
0
def github_username_watched_update(request,username, link_type):
    username = urllib.unquote(username)
    user = request.user
    try:
        github_username = user.social_auth.get(provider='github').extra_data['username']
        if username == github_username:
            github_provider = GithubProvider(user)
            repository_user = github_provider.update_repositories(username, link_type)
            repository_user.save()
            if link_type == "owned":
                return HttpResponseRedirect(reverse('github_username_owned', kwargs={'username': username}))
            elif link_type == "starred":
                return HttpResponseRedirect(reverse('github_username_starred', kwargs={'username': username}))
            elif link_type == "watched":
                return HttpResponseRedirect(reverse('github_username_watched', kwargs={'username': username}))
    except ObjectDoesNotExist:
        pass
    res = HttpResponse("Unauthorized")
    res.status_code = 401
    return res
Ejemplo n.º 5
0
def github_username_watched_save(request,username, link_type):
    user = request.user
    username = urllib.unquote(username)
    try:
        if user.is_authenticated() and username == user.social_auth.get(provider='github').extra_data['username']:
            profile = user.get_profile()
            try:
                updated_dictionary = json.loads(request.POST["order"])
            except ValueError:
                updated_dictionary ={}
            profile.save()
            github_provider = GithubProvider(user)
            github_provider.save_repositories(updated_dictionary, link_type)
            data= {'outcome':'success'}
            return HttpResponse(json.dumps(data),mimetype="application/json")
    except ObjectDoesNotExist:
        pass
    res = HttpResponse("Unauthorized")
    res.status_code = 401
    return res
Ejemplo n.º 6
0
def github_repo(request, owner, repo):
    owner = urllib.unquote(owner)
    repo = urllib.unquote(repo)
    user = request.user
    github_provider = GithubProvider(user)
    # check to see if the repo is being watched by the authed user or not
    watched = github_provider.get_watched_status(owner, repo)
    starred = github_provider.get_starred_status(owner, repo)
    update = False
    try:
        repository = github_provider.retrieve_repository_details(owner, repo)
    except ObjectDoesNotExist:
        update = True
        repository = Repository()
    if update or (datetime.now() - repository.last_modified) > timedelta(days = 1):
        repository_dict = github_provider.get_repository_details(owner, repo)
        repository = github_provider.create_or_update_repository_details(repository_dict, repository)
        if not repository.private:
            repository.save()
    repo_events = github_provider.get_repository_events(owner, repo)
    return render_to_response('github_repo.html', {'repository': repository, 'repo_events': repo_events, 'watched':watched, 'starred':starred}, RequestContext(request))
Ejemplo n.º 7
0
def search(request):
    user = request.user
    query = request.GET['query']
    repositories_by_language = defaultdict(list)
    if query != '':
        github_provider = GithubProvider(user)
        if user.is_authenticated():
            try:
                tokens = user.social_auth.get(provider='bitbucket').tokens
                oauth_hook = OAuthHook(tokens['oauth_token'],
                                       tokens['oauth_token_secret'],
                                       header_auth=False)
                client = requests.session(hooks={'pre_request': oauth_hook})
            except ObjectDoesNotExist:
                client = requests.session()
        else:
            client = requests.session()
        if '/' in query:
            user_query = query.split('/')[0]
            repo_query = query.split('/')[1]
            github_repositories = github_provider.search_repository(repo_query)
            users = github_provider.search_user(user_query)
            try:
                response = client.get(
                    'https://api.bitbucket.org/1.0/repositories/',
                    params={
                        'name': repo_query,
                        'limit': 100
                    })
                bitbucket_repositories = json.loads(
                    response.text)['repositories'][:100]
            except Exception:
                bitbucket_repositories = []
        else:
            github_repositories = github_provider.search_repository(query)
            users = github_provider.search_user(query)
            try:
                response = client.get(
                    'https://api.bitbucket.org/1.0/repositories/',
                    params={
                        'name': query,
                        'limit': 100
                    })
                bitbucket_repositories = json.loads(
                    response.text)['repositories'][:100]
            except Exception:
                bitbucket_repositories = []
        for repo in github_repositories:
            update = False
            repo['owner'] = repo['owner'].lower().replace("/", "")
            repo['name'] = repo['name'].lower().replace("/", "")
            try:
                repository = Repository.objects.get(
                    slug=repo['owner'].lower() + '/' + repo['name'],
                    host='github')
            except ObjectDoesNotExist:
                update = True
                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)
            repositories_by_language[repository.language].append(repository)
        return render_to_response('search.html', {
            'repositories_by_language':
            sorted(dict(repositories_by_language).iteritems(),
                   key=lambda (k, v): len(v),
                   reverse=True),
            'users':
            users
        },
                                  context_instance=RequestContext(request))
    else:
        return render_to_response('search.html', {
            'repositories_by_language': {},
            'users': []
        },
                                  context_instance=RequestContext(request))
Ejemplo n.º 8
0
def authed_category_owned(request,category):
    """has all github repos and the latest 30 events for a username with a specific category"""
    repo_link_type, _ = LinkType.objects.get_or_create(name = "owned")
    owned = True
    category = urllib.unquote(category).lower()
    github_repo_events = []
    bitbucket_repo_events = []
    github_watched_filtered = []
    bitbucket_watched_filtered = []
    user = request.user
    repository_user = None
    github_username = None
    bitbucket_username = None
    bitbucket_authed = True
    github_authed = True
    watched_filtered = []
    try:
        github_username = user.social_auth.get(provider='github').extra_data['username']
        github_provider = GithubProvider(user)
        github_watched_filtered, github_repository_user = github_provider.retrieve_category_repositories(github_username, category, "owned")
        watched_filtered.extend(github_watched_filtered)
        github_repository_user.save()
        if len(github_watched_filtered) == 0:
            watched = github_provider.get_repositories(github_username, "owned")
            category_lower = category.lower()
            for repo in watched:
                update = False
                try:
                    repository = Repository.objects.get(host_slug= 'github/'+repo['owner'].lower() + '/' + repo['name'].lower())
                except ObjectDoesNotExist:
                    update = True
                    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()
                if repository.language == category_lower:
                    github_watched_filtered.append(repository)
                if not repository.private:
                    RepositoryUserRepositoryLink.objects.get_or_create(user = github_repository_user, repository = repository, link_type = repo_link_type)
            watched_filtered.extend(github_watched_filtered)
            watched_filtered.sort(key=lambda x: x.watchers, reverse = True)
            RepositoryCategory.objects.get_or_create(name = category.lower())
            github_repository_user.save()


        # Get repository events

        github_repo_events = github_provider.get_repositories_events(github_watched_filtered)
        for github_repo_event in github_repo_events:
            github_repo_event['host'] = 'github'
            github_repo_event['created_on'] = dateutil.parser.parse(github_repo_event['created_at'])
    except ObjectDoesNotExist:
        github_authed = False
    user = request.user
    try:
        bitbucket_username = user.social_auth.get(provider='bitbucket').extra_data['username']
        profile = user.get_profile()
        bitbucket_provider = BitbucketProvider(user)
        bitbucket_watched_filtered, bitbucket_repository_user = bitbucket_provider.retrieve_owned_category_repositories(bitbucket_username, category)
        watched_filtered.extend(bitbucket_watched_filtered)
        if len(bitbucket_watched_filtered) == 0:
            owned = bitbucket_provider.get_owned_repositories(bitbucket_username)
            for repo in owned:
                update = False
                try:
                    repository = Repository.objects.get(host_slug= 'bitbucket/'+repo['owner'].lower() + '/' + repo['name'].lower())
                except ObjectDoesNotExist:
                    update = True
                    repository = Repository()
                if update or (datetime.now() - repository.last_modified) > timedelta(days = 1):
                    repository = bitbucket_provider.create_or_update_repository_details(repo, repository)
                    if not repository.private:
                        repository.save()


                if repository.language == category_lower:
                    bitbucket_watched_filtered.append(repository)
            watched_filtered.extend(bitbucket_watched_filtered)
            watched_filtered.sort(key=lambda x: x.watchers, reverse = True)
            RepositoryCategory.objects.get_or_create(name = category.lower())
        bitbucket_repo_events = bitbucket_provider.get_repositories_events(bitbucket_watched_filtered)
        for bitbucket_repo_event in bitbucket_repo_events:
                bitbucket_repo_event['host'] = 'bitbucket'
                bitbucket_repo_event['created_on'] = dateutil.parser.parse(bitbucket_repo_event['utc_created_on'])
    except ObjectDoesNotExist:
        bitbucket_authed = False
    repo_events = sorted(github_repo_events + bitbucket_repo_events,key=itemgetter('created_on'), reverse = True)[:30]
    if bitbucket_authed or github_authed:
        return render_to_response('username_category_watched.html', {'github_username': github_username,'github_authed': github_authed,'bitbucket_username':bitbucket_username, 'bitbucket_authed':bitbucket_authed, 'watched':watched_filtered, 'category':category, 'repo_events':repo_events,'owned':owned},context_instance=RequestContext(request))
    else:
        return HttpResponseRedirect(reverse('repowatcher.main.views.authed'))
Ejemplo n.º 9
0
def authed(request):
    user = request.user
    bitbucket_authed = True
    github_authed = True
    bitbucket_user_events = []
    github_user_events = []
    github_repository_user = None
    bitbucket_repository_user = None
    try:
        github_username = user.social_auth.get(provider='github').extra_data['username']
        github_provider = GithubProvider(user)
        update = False

        # Get user information
        try:
            github_repository_user = github_provider.retrieve_user_details(github_username)
        except ObjectDoesNotExist:
            update = True
            github_repository_user = RepositoryUser()
        if update or (datetime.now() - github_repository_user.last_modified) > timedelta(days = 1):
            github_user_dict = github_provider.get_user_details(github_username)
            github_repository_user = github_provider.create_or_update_user_details(github_user_dict, github_repository_user)
            github_repository_user.save()
        github_user_events = github_provider.get_user_events(github_username)
        for github_user_event in github_user_events:
            github_user_event['host'] = 'github'
            github_user_event['created_on'] = dateutil.parser.parse(github_user_event['created_at'])

        # Get repository information
        repositories, _ = github_provider.retrieve_starred_repositories_list(github_username)

        if len(repositories) == 0:
            repo_link_type, _ = LinkType.objects.get_or_create(name = "starred")
            if not github_repository_user:
                github_repository_user,_ = RepositoryUser.objects.get_or_create(login=github_username,host='github')
            watched = github_provider.get_starred_repositories(github_username)
            for repo in watched:
                update = False
                try:
                    repository = Repository.objects.get(host_slug= 'github/'+repo['owner'].lower() + '/' + repo['name'].lower())
                except ObjectDoesNotExist:
                    update = True
                    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()
                    RepositoryCategory.objects.get_or_create(name = repository.language)
                if not repository.private:
                    RepositoryUserRepositoryLink.objects.get_or_create(user = github_repository_user, repository = repository, link_type = repo_link_type)

                repositories.append(repository)
        github_repository_user.starred = len(repositories)
        github_repository_user.save()

        # Get repository information
        repositories, _ = github_provider.retrieve_watched_repositories_list(github_username)

        if len(repositories) == 0:
            repo_link_type, _ = LinkType.objects.get_or_create(name = "watched")
            if not github_repository_user:
                github_repository_user,_ = RepositoryUser.objects.get_or_create(login=github_username,host='github')
            watched = github_provider.get_watched_repositories(github_username)
            for repo in watched:
                update = False
                try:
                    repository = Repository.objects.get(host_slug= 'github/'+repo['owner'].lower() + '/' + repo['name'].lower())
                except ObjectDoesNotExist:
                    update = True
                    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()
                    RepositoryCategory.objects.get_or_create(name = repository.language)
                if not repository.private:
                    RepositoryUserRepositoryLink.objects.get_or_create(user = github_repository_user, repository = repository, link_type = repo_link_type)

                repositories.append(repository)
        github_repository_user.watched = len(repositories)
        github_repository_user.save()
    except ObjectDoesNotExist:
        github_authed = False

    try:
        bitbucket_username = user.social_auth.get(provider='bitbucket').extra_data['username']
        bitbucket_provider = BitbucketProvider(user)
        # Get user information
        update = False
        try:
            bitbucket_repository_user = bitbucket_provider.retrieve_user_details(bitbucket_username)
        except ObjectDoesNotExist:
            update = True
            bitbucket_repository_user = RepositoryUser()
        if update or (datetime.now() - bitbucket_repository_user.last_modified) > timedelta(days = 1):
            user_dict = bitbucket_provider.get_user_details(bitbucket_username)
            bitbucket_repository_user = bitbucket_provider.create_or_update_user_details(user_dict, bitbucket_repository_user)
            bitbucket_repository_user.save()
        bitbucket_user_events = bitbucket_provider.get_user_events(bitbucket_username)
        for bitbucket_user_event in bitbucket_user_events:
            bitbucket_user_event['host'] = 'bitbucket'
            bitbucket_user_event['created_on'] = dateutil.parser.parse(bitbucket_user_event['utc_created_on'])
        # Get repository information
        owned_repositories, _ = bitbucket_provider.retrieve_owned_repositories_list(bitbucket_username)
        watched_repositories, _ = bitbucket_provider.retrieve_watched_repositories_list(bitbucket_username)

        if len(owned_repositories) == 0:
            owned = bitbucket_provider.get_owned_repositories(bitbucket_username)
            for repo in owned:
                update = False
                try:
                    repository = bitbucket_provider.retrieve_repository_details(repo['owner'], repo['name'])
                except ObjectDoesNotExist:
                    update = True
                    repository = Repository()
                if update or (datetime.now() - repository.last_modified) > timedelta(days = 1):
                    repository = bitbucket_provider.create_or_update_repository_details(repo, repository)
                    if not repository.private:
                        repository.save()
                    RepositoryCategory.objects.get_or_create(name=repository.language)
                if not repository.private:
                    RepositoryUserRepositoryLink.objects.get_or_create(user=bitbucket_repository_user, repository = repository, owned = True)
                owned_repositories.append(repository)
            bitbucket_repository_user.public_repos = len(owned_repositories)
            bitbucket_repository_user.save()

        if len(watched_repositories) == 0:
            watched = bitbucket_provider.get_watched_repositories(bitbucket_username)
            for repo in watched:
                update = False
                try:
                    repository = bitbucket_provider.retrieve_repository_details(repo['owner'], repo['name'])
                except ObjectDoesNotExist:
                    update = True
                    repository = Repository()
                if update or (datetime.now() - repository.last_modified) > timedelta(days = 1):
                    repository = bitbucket_provider.create_or_update_repository_details(repo, repository)
                    if not repository.private:
                        repository.save()
                watched_repositories.append(repository)
            bitbucket_repository_user.starred = len(watched_repositories)
    except ObjectDoesNotExist:
        bitbucket_authed = False

    user_events = sorted(github_user_events + bitbucket_user_events,key=itemgetter('created_on'), reverse = True)[:30]

    return render_to_response('authed.html', {'user_events':user_events,'github_repository_user':github_repository_user,'bitbucket_repository_user':bitbucket_repository_user,'github_authed':github_authed,'bitbucket_authed':bitbucket_authed}, context_instance=RequestContext(request))
Ejemplo n.º 10
0
def authed_owned(request):
    repo_link_type, _ = LinkType.objects.get_or_create(name = "owned")
    repositories_by_language = defaultdict(list)
    github_repositories_by_language = defaultdict(list)
    bitbucket_repositories_by_language = defaultdict(list)
    bitbucket_authed = True
    github_authed = True
    owned = True
    api_only = True
    user = request.user
    github_username = None
    bitbucket_username = None
    try:
        github_username = user.social_auth.get(provider='github').extra_data['username']
        github_provider = GithubProvider(user)
        github_repositories_by_language, github_repository_user = github_provider.retrieve_repositories_dict(github_username, "owned")
        if len(github_repositories_by_language) == 0:
            owned_repos = github_provider.get_repositories(github_username, "owned")

            count = 0
            for repo in owned_repos:
                update = False
                try:
                    repository = github_provider.retrieve_repository_details(repo['owner'], repo['name'])
                except ObjectDoesNotExist:
                    update = True
                    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()
                if not repository.private:
                    count += 1
                    RepositoryUserRepositoryLink.objects.get_or_create(user = github_repository_user, repository = repository, link_type = repo_link_type)

                github_repositories_by_language[repository.language.lower()].append(repository)
            for category in github_repositories_by_language.keys():
                RepositoryCategory.objects.get_or_create(name=category)
                repositories_by_language[category].extend(github_repositories_by_language[category])
            for category in repositories_by_language.keys():
                repositories_by_language[category].sort(key=lambda x: x.watchers, reverse = True)
        else:
            for category in github_repositories_by_language.keys():
                repositories_by_language[category].extend(github_repositories_by_language[category])
    except ObjectDoesNotExist:
        github_authed = False
    try:
        bitbucket_username = user.social_auth.get(provider='bitbucket').extra_data['username']
        bitbucket_provider = BitbucketProvider(user)
        bitbucket_repositories_by_language, bitbucket_repository_user = bitbucket_provider.retrieve_owned_repositories_dict(bitbucket_username)
        if len(bitbucket_repositories_by_language)==0:
            owned_repos = bitbucket_provider.get_owned_repositories(bitbucket_username)
            for repo in owned_repos:
                update = False
                try:
                    repository = bitbucket_provider.retrieve_repository_details(repo['owner'], repo['name'])
                except ObjectDoesNotExist:
                    update = True
                    repository = Repository()
                if update or (datetime.now() - repository.last_modified) > timedelta(days = 1):
                    repository = bitbucket_provider.create_or_update_repository_details(repo, repository)
                    if not repository.private:
                        repository.save()
                bitbucket_repositories_by_language[repository.language].append(repository)
            for category in bitbucket_repositories_by_language.keys():
                RepositoryCategory.objects.get_or_create(name=category)
                repositories_by_language[category].extend(bitbucket_repositories_by_language[category])
            for category in repositories_by_language.keys():
                repositories_by_language[category].sort(key=lambda x: x.watchers, reverse = True)
        else:
            for category in bitbucket_repositories_by_language.keys():
                repositories_by_language[category].extend(bitbucket_repositories_by_language[category])
    except ObjectDoesNotExist:
        bitbucket_authed = False
    if bitbucket_authed or github_authed:
        return render_to_response('username_watched.html', {'github_username': github_username,'github_authed': github_authed,'bitbucket_username':bitbucket_username, 'bitbucket_authed':bitbucket_authed,'repositories_by_language':sorted(dict(repositories_by_language).iteritems(),key=lambda (k, v): len(v),reverse = True),'owned':owned},context_instance=RequestContext(request))
    else:
        return HttpResponseRedirect(reverse('repowatcher.main.views.authed'))
Ejemplo n.º 11
0
def github_username(request, username):
    username = urllib.unquote(username)
    user = request.user
    repository_user = None
    update = False
    github_provider = GithubProvider(user)
    # Get user information
    try:
        repository_user = github_provider.retrieve_user_details(username)
    except ObjectDoesNotExist:
        update = True
        repository_user = RepositoryUser()
    if update or (datetime.now() - repository_user.last_modified) > timedelta(days = 1):

        github_user_dict = github_provider.get_user_details(username)
        repository_user = github_provider.create_or_update_user_details(github_user_dict, repository_user)
        repository_user.save()

    user_events = github_provider.get_user_events(username)

    # Get repository information
    repositories, _ = github_provider.retrieve_starred_repositories_list(username)
    if len(repositories) == 0:
        repo_link_type, _ = LinkType.objects.get_or_create(name = "starred")
        for repo in github_provider.get_starred_repositories(username):
            update = False
            try:
                repository = Repository.objects.get(host_slug= 'github/'+repo['owner'].lower() + '/' + repo['name'].lower())
            except ObjectDoesNotExist:
                update = True
                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()
                repository = repository
                RepositoryCategory.objects.get_or_create(name = repository.language)
            if not repository.private:
                RepositoryUserRepositoryLink.objects.get_or_create(user = repository_user, repository = repository, link_type = repo_link_type)

            repositories.append(repository)
    repository_user.starred = len(repositories)

    # Get repository information
    repositories, _ = github_provider.retrieve_watched_repositories_list(username)
    if len(repositories) == 0:
        repo_link_type, _ = LinkType.objects.get_or_create(name = "watched")
        for repo in github_provider.get_watched_repositories(username):
            update = False
            try:
                repository = Repository.objects.get(host_slug= 'github/'+repo['owner'].lower() + '/' + repo['name'].lower())
            except ObjectDoesNotExist:
                update = True
                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()
                repository = repository
                RepositoryCategory.objects.get_or_create(name = repository.language)
            if not repository.private:
                RepositoryUserRepositoryLink.objects.get_or_create(user = repository_user, repository = repository, link_type = repo_link_type)

            repositories.append(repository)
    repository_user.watched = len(repositories)
    repository_user.save()

    return render_to_response('github_username.html', {'user_events':user_events,'repository_user':repository_user},context_instance=RequestContext(request))
Ejemplo n.º 12
0
def github_username_category_watched(request,username,category, link_type):
    """has all github repos and the latest 30 events for a username with a specific category"""
    owned = False
    starred = False
    repo_link_type, _ = LinkType.objects.get_or_create(name = link_type)
    if request.GET.get('sorted_by_watchers', 'false') == 'true':
        sorted_by_watchers = True
    username = urllib.unquote(username)
    category = urllib.unquote(category).lower()
    user = request.user
    github_provider = GithubProvider(user)
    watched_filtered, repository_user = github_provider.retrieve_category_repositories(username, category, link_type)
    repository_user.save()
    if len(watched_filtered) == 0:
        watched = github_provider.get_repositories(username, link_type)
        count = 0
        for repo in watched:
            update = False
            try:
                repository = Repository.objects.get(host_slug= 'github/'+repo['owner'].lower() + '/' + repo['name'].lower())
            except ObjectDoesNotExist:
                update = True
                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()
            if repository.language.lower() == category:
                watched_filtered.append(repository)
                if not repository.private:
                    count += 1
            if not repository.private:
                RepositoryUserRepositoryLink.objects.get_or_create(user = repository_user, repository = repository, link_type = repo_link_type)
        RepositoryCategory.objects.get_or_create(name = category.lower())
        if repo_link_type.name == "owned":
            pass
        elif repo_link_type.name == "starred":
            repository_user.starred = count
        elif repo_link_type.name == "watched":
            repository_user.watched = count
        repository_user.save()
        watched_filtered.sort(key=lambda x: x.watchers, reverse = True)
    if repo_link_type.name == "owned":
        owned = True
    elif repo_link_type.name == "starred":
        starred = True
    elif repo_link_type.name == "watched":
        pass
    # Get repository events
    repo_events = []
#    page = 0
#    watched_filtered_length = len(watched_filtered)
#    user_path = False
#    if owned:
#        if repository_user is not None and repository_user.public_repos is not None and float(repository_user.public_repos) < 0.5*(watched_filtered_length**2):
#            user_path = True
#    else:
#        if repository_user is not None and repository_user.watched is not None and float(repository_user.watched) < 0.5*(watched_filtered_length**2):
#            user_path = True
#    if user_path:
#        while len(repo_events) < 30 and page <= watched_filtered_length:
#            page += 1
#            r = requests.get('https://api.github.com/users/'+ username + '/received_events', params = {'page':page})
#            try:
#                user_events = json.loads(r.text)
#            except ValueError:
#                user_events = []
#            for user_event in user_events:
#                for watched_filter in watched_filtered:
#                    if user_event['repo']['name'] == watched_filter.slug:
#                        repo_events.append(user_event)
#        repo_events = repo_events[:30]
#    else:
    repo_events = github_provider.get_repositories_events(watched_filtered)
    return render_to_response('github_username_category_watched.html', {'username': username, 'watched':watched_filtered, 'category':category, 'repo_events':repo_events,'owned':owned,"starred":starred},context_instance=RequestContext(request))
Ejemplo n.º 13
0
def authed_category_owned(request, category):
    """has all github repos and the latest 30 events for a username with a specific category"""
    repo_link_type, _ = LinkType.objects.get_or_create(name="owned")
    owned = True
    category = urllib.unquote(category).lower()
    github_repo_events = []
    bitbucket_repo_events = []
    github_watched_filtered = []
    bitbucket_watched_filtered = []
    user = request.user
    repository_user = None
    github_username = None
    bitbucket_username = None
    bitbucket_authed = True
    github_authed = True
    watched_filtered = []
    try:
        github_username = user.social_auth.get(
            provider='github').extra_data['username']
        github_provider = GithubProvider(user)
        github_watched_filtered, github_repository_user = github_provider.retrieve_category_repositories(
            github_username, category, "owned")
        watched_filtered.extend(github_watched_filtered)
        github_repository_user.save()
        if len(github_watched_filtered) == 0:
            watched = github_provider.get_repositories(github_username,
                                                       "owned")
            category_lower = category.lower()
            for repo in watched:
                update = False
                try:
                    repository = Repository.objects.get(
                        host_slug='github/' + repo['owner'].lower() + '/' +
                        repo['name'].lower())
                except ObjectDoesNotExist:
                    update = True
                    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()
                if repository.language == category_lower:
                    github_watched_filtered.append(repository)
                if not repository.private:
                    RepositoryUserRepositoryLink.objects.get_or_create(
                        user=github_repository_user,
                        repository=repository,
                        link_type=repo_link_type)
            watched_filtered.extend(github_watched_filtered)
            watched_filtered.sort(key=lambda x: x.watchers, reverse=True)
            RepositoryCategory.objects.get_or_create(name=category.lower())
            github_repository_user.save()

        # Get repository events

        github_repo_events = github_provider.get_repositories_events(
            github_watched_filtered)
        for github_repo_event in github_repo_events:
            github_repo_event['host'] = 'github'
            github_repo_event['created_on'] = dateutil.parser.parse(
                github_repo_event['created_at'])
    except ObjectDoesNotExist:
        github_authed = False
    user = request.user
    try:
        bitbucket_username = user.social_auth.get(
            provider='bitbucket').extra_data['username']
        profile = user.get_profile()
        bitbucket_provider = BitbucketProvider(user)
        bitbucket_watched_filtered, bitbucket_repository_user = bitbucket_provider.retrieve_owned_category_repositories(
            bitbucket_username, category)
        watched_filtered.extend(bitbucket_watched_filtered)
        if len(bitbucket_watched_filtered) == 0:
            owned = bitbucket_provider.get_owned_repositories(
                bitbucket_username)
            for repo in owned:
                update = False
                try:
                    repository = Repository.objects.get(
                        host_slug='bitbucket/' + repo['owner'].lower() + '/' +
                        repo['name'].lower())
                except ObjectDoesNotExist:
                    update = True
                    repository = Repository()
                if update or (datetime.now() -
                              repository.last_modified) > timedelta(days=1):
                    repository = bitbucket_provider.create_or_update_repository_details(
                        repo, repository)
                    if not repository.private:
                        repository.save()

                if repository.language == category_lower:
                    bitbucket_watched_filtered.append(repository)
            watched_filtered.extend(bitbucket_watched_filtered)
            watched_filtered.sort(key=lambda x: x.watchers, reverse=True)
            RepositoryCategory.objects.get_or_create(name=category.lower())
        bitbucket_repo_events = bitbucket_provider.get_repositories_events(
            bitbucket_watched_filtered)
        for bitbucket_repo_event in bitbucket_repo_events:
            bitbucket_repo_event['host'] = 'bitbucket'
            bitbucket_repo_event['created_on'] = dateutil.parser.parse(
                bitbucket_repo_event['utc_created_on'])
    except ObjectDoesNotExist:
        bitbucket_authed = False
    repo_events = sorted(github_repo_events + bitbucket_repo_events,
                         key=itemgetter('created_on'),
                         reverse=True)[:30]
    if bitbucket_authed or github_authed:
        return render_to_response('username_category_watched.html', {
            'github_username': github_username,
            'github_authed': github_authed,
            'bitbucket_username': bitbucket_username,
            'bitbucket_authed': bitbucket_authed,
            'watched': watched_filtered,
            'category': category,
            'repo_events': repo_events,
            'owned': owned
        },
                                  context_instance=RequestContext(request))
    else:
        return HttpResponseRedirect(reverse('repowatcher.main.views.authed'))
Ejemplo n.º 14
0
def authed_owned(request):
    repo_link_type, _ = LinkType.objects.get_or_create(name="owned")
    repositories_by_language = defaultdict(list)
    github_repositories_by_language = defaultdict(list)
    bitbucket_repositories_by_language = defaultdict(list)
    bitbucket_authed = True
    github_authed = True
    owned = True
    api_only = True
    user = request.user
    github_username = None
    bitbucket_username = None
    try:
        github_username = user.social_auth.get(
            provider='github').extra_data['username']
        github_provider = GithubProvider(user)
        github_repositories_by_language, github_repository_user = github_provider.retrieve_repositories_dict(
            github_username, "owned")
        if len(github_repositories_by_language) == 0:
            owned_repos = github_provider.get_repositories(
                github_username, "owned")

            count = 0
            for repo in owned_repos:
                update = False
                try:
                    repository = github_provider.retrieve_repository_details(
                        repo['owner'], repo['name'])
                except ObjectDoesNotExist:
                    update = True
                    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()
                if not repository.private:
                    count += 1
                    RepositoryUserRepositoryLink.objects.get_or_create(
                        user=github_repository_user,
                        repository=repository,
                        link_type=repo_link_type)

                github_repositories_by_language[
                    repository.language.lower()].append(repository)
            for category in github_repositories_by_language.keys():
                RepositoryCategory.objects.get_or_create(name=category)
                repositories_by_language[category].extend(
                    github_repositories_by_language[category])
            for category in repositories_by_language.keys():
                repositories_by_language[category].sort(
                    key=lambda x: x.watchers, reverse=True)
        else:
            for category in github_repositories_by_language.keys():
                repositories_by_language[category].extend(
                    github_repositories_by_language[category])
    except ObjectDoesNotExist:
        github_authed = False
    try:
        bitbucket_username = user.social_auth.get(
            provider='bitbucket').extra_data['username']
        bitbucket_provider = BitbucketProvider(user)
        bitbucket_repositories_by_language, bitbucket_repository_user = bitbucket_provider.retrieve_owned_repositories_dict(
            bitbucket_username)
        if len(bitbucket_repositories_by_language) == 0:
            owned_repos = bitbucket_provider.get_owned_repositories(
                bitbucket_username)
            for repo in owned_repos:
                update = False
                try:
                    repository = bitbucket_provider.retrieve_repository_details(
                        repo['owner'], repo['name'])
                except ObjectDoesNotExist:
                    update = True
                    repository = Repository()
                if update or (datetime.now() -
                              repository.last_modified) > timedelta(days=1):
                    repository = bitbucket_provider.create_or_update_repository_details(
                        repo, repository)
                    if not repository.private:
                        repository.save()
                bitbucket_repositories_by_language[repository.language].append(
                    repository)
            for category in bitbucket_repositories_by_language.keys():
                RepositoryCategory.objects.get_or_create(name=category)
                repositories_by_language[category].extend(
                    bitbucket_repositories_by_language[category])
            for category in repositories_by_language.keys():
                repositories_by_language[category].sort(
                    key=lambda x: x.watchers, reverse=True)
        else:
            for category in bitbucket_repositories_by_language.keys():
                repositories_by_language[category].extend(
                    bitbucket_repositories_by_language[category])
    except ObjectDoesNotExist:
        bitbucket_authed = False
    if bitbucket_authed or github_authed:
        return render_to_response('username_watched.html', {
            'github_username':
            github_username,
            'github_authed':
            github_authed,
            'bitbucket_username':
            bitbucket_username,
            'bitbucket_authed':
            bitbucket_authed,
            'repositories_by_language':
            sorted(dict(repositories_by_language).iteritems(),
                   key=lambda (k, v): len(v),
                   reverse=True),
            'owned':
            owned
        },
                                  context_instance=RequestContext(request))
    else:
        return HttpResponseRedirect(reverse('repowatcher.main.views.authed'))
Ejemplo n.º 15
0
def authed(request):
    user = request.user
    bitbucket_authed = True
    github_authed = True
    bitbucket_user_events = []
    github_user_events = []
    github_repository_user = None
    bitbucket_repository_user = None
    try:
        github_username = user.social_auth.get(
            provider='github').extra_data['username']
        github_provider = GithubProvider(user)
        update = False

        # Get user information
        try:
            github_repository_user = github_provider.retrieve_user_details(
                github_username)
        except ObjectDoesNotExist:
            update = True
            github_repository_user = RepositoryUser()
        if update or (datetime.now() - github_repository_user.last_modified
                      ) > timedelta(days=1):
            github_user_dict = github_provider.get_user_details(
                github_username)
            github_repository_user = github_provider.create_or_update_user_details(
                github_user_dict, github_repository_user)
            github_repository_user.save()
        github_user_events = github_provider.get_user_events(github_username)
        for github_user_event in github_user_events:
            github_user_event['host'] = 'github'
            github_user_event['created_on'] = dateutil.parser.parse(
                github_user_event['created_at'])

        # Get repository information
        repositories, _ = github_provider.retrieve_starred_repositories_list(
            github_username)

        if len(repositories) == 0:
            repo_link_type, _ = LinkType.objects.get_or_create(name="starred")
            if not github_repository_user:
                github_repository_user, _ = RepositoryUser.objects.get_or_create(
                    login=github_username, host='github')
            watched = github_provider.get_starred_repositories(github_username)
            for repo in watched:
                update = False
                try:
                    repository = Repository.objects.get(
                        host_slug='github/' + repo['owner'].lower() + '/' +
                        repo['name'].lower())
                except ObjectDoesNotExist:
                    update = True
                    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()
                    RepositoryCategory.objects.get_or_create(
                        name=repository.language)
                if not repository.private:
                    RepositoryUserRepositoryLink.objects.get_or_create(
                        user=github_repository_user,
                        repository=repository,
                        link_type=repo_link_type)

                repositories.append(repository)
        github_repository_user.starred = len(repositories)
        github_repository_user.save()

        # Get repository information
        repositories, _ = github_provider.retrieve_watched_repositories_list(
            github_username)

        if len(repositories) == 0:
            repo_link_type, _ = LinkType.objects.get_or_create(name="watched")
            if not github_repository_user:
                github_repository_user, _ = RepositoryUser.objects.get_or_create(
                    login=github_username, host='github')
            watched = github_provider.get_watched_repositories(github_username)
            for repo in watched:
                update = False
                try:
                    repository = Repository.objects.get(
                        host_slug='github/' + repo['owner'].lower() + '/' +
                        repo['name'].lower())
                except ObjectDoesNotExist:
                    update = True
                    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()
                    RepositoryCategory.objects.get_or_create(
                        name=repository.language)
                if not repository.private:
                    RepositoryUserRepositoryLink.objects.get_or_create(
                        user=github_repository_user,
                        repository=repository,
                        link_type=repo_link_type)

                repositories.append(repository)
        github_repository_user.watched = len(repositories)
        github_repository_user.save()
    except ObjectDoesNotExist:
        github_authed = False

    try:
        bitbucket_username = user.social_auth.get(
            provider='bitbucket').extra_data['username']
        bitbucket_provider = BitbucketProvider(user)
        # Get user information
        update = False
        try:
            bitbucket_repository_user = bitbucket_provider.retrieve_user_details(
                bitbucket_username)
        except ObjectDoesNotExist:
            update = True
            bitbucket_repository_user = RepositoryUser()
        if update or (datetime.now() - bitbucket_repository_user.last_modified
                      ) > timedelta(days=1):
            user_dict = bitbucket_provider.get_user_details(bitbucket_username)
            bitbucket_repository_user = bitbucket_provider.create_or_update_user_details(
                user_dict, bitbucket_repository_user)
            bitbucket_repository_user.save()
        bitbucket_user_events = bitbucket_provider.get_user_events(
            bitbucket_username)
        for bitbucket_user_event in bitbucket_user_events:
            bitbucket_user_event['host'] = 'bitbucket'
            bitbucket_user_event['created_on'] = dateutil.parser.parse(
                bitbucket_user_event['utc_created_on'])
        # Get repository information
        owned_repositories, _ = bitbucket_provider.retrieve_owned_repositories_list(
            bitbucket_username)
        watched_repositories, _ = bitbucket_provider.retrieve_watched_repositories_list(
            bitbucket_username)

        if len(owned_repositories) == 0:
            owned = bitbucket_provider.get_owned_repositories(
                bitbucket_username)
            for repo in owned:
                update = False
                try:
                    repository = bitbucket_provider.retrieve_repository_details(
                        repo['owner'], repo['name'])
                except ObjectDoesNotExist:
                    update = True
                    repository = Repository()
                if update or (datetime.now() -
                              repository.last_modified) > timedelta(days=1):
                    repository = bitbucket_provider.create_or_update_repository_details(
                        repo, repository)
                    if not repository.private:
                        repository.save()
                    RepositoryCategory.objects.get_or_create(
                        name=repository.language)
                if not repository.private:
                    RepositoryUserRepositoryLink.objects.get_or_create(
                        user=bitbucket_repository_user,
                        repository=repository,
                        owned=True)
                owned_repositories.append(repository)
            bitbucket_repository_user.public_repos = len(owned_repositories)
            bitbucket_repository_user.save()

        if len(watched_repositories) == 0:
            watched = bitbucket_provider.get_watched_repositories(
                bitbucket_username)
            for repo in watched:
                update = False
                try:
                    repository = bitbucket_provider.retrieve_repository_details(
                        repo['owner'], repo['name'])
                except ObjectDoesNotExist:
                    update = True
                    repository = Repository()
                if update or (datetime.now() -
                              repository.last_modified) > timedelta(days=1):
                    repository = bitbucket_provider.create_or_update_repository_details(
                        repo, repository)
                    if not repository.private:
                        repository.save()
                watched_repositories.append(repository)
            bitbucket_repository_user.starred = len(watched_repositories)
    except ObjectDoesNotExist:
        bitbucket_authed = False

    user_events = sorted(github_user_events + bitbucket_user_events,
                         key=itemgetter('created_on'),
                         reverse=True)[:30]

    return render_to_response('authed.html', {
        'user_events': user_events,
        'github_repository_user': github_repository_user,
        'bitbucket_repository_user': bitbucket_repository_user,
        'github_authed': github_authed,
        'bitbucket_authed': bitbucket_authed
    },
                              context_instance=RequestContext(request))