예제 #1
0
 def create_or_update_repository_details(self, repository_dict, repository = None):
     if repository is None:
         repository = Repository()
     extra_data = {}
     for key, value in repository_dict.iteritems():
         if key == "_attrs":
             continue
         if key == 'owner' or key == "organization":
             try:
                 setattr(repository, key, value.login)
                 continue
             except Exception:
                 pass
         if key == 'source' or key == "parent":
             try:
                 setattr(repository, key, value.full_name)
                 continue
             except Exception:
                 pass
         if key in ['owner', 'name', 'html_url', 'homepage', 'language','description','watchers','created_at','pushed_at','private']:
             setattr(repository, key, value)
         else:
             if isinstance(value, datetime):
                 extra_data[key] = value.__str__()
             else:
                 extra_data[key] = value
     repository.extra_data = json.dumps(extra_data)
     if repository.language == "" or repository.language == None:
         repository.language = "other"
     repository.scm = 'git'
     repository.host =self.host
     repository.language = repository.language.lower()
     return repository
예제 #2
0
def bitbucket_repo(request, owner, repo):
    owner = urllib.unquote(owner)
    repo = urllib.unquote(repo)
    user = request.user
    slug = '/'.join((owner, repo))
    host_slug = '/'.join(('bitbucket', owner, repo))
    bitbucket_provider = BitbucketProvider(user)
    update = False
    try:
        repository = bitbucket_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 = bitbucket_provider.get_repository_details(
            owner, repo)
        repository = bitbucket_provider.create_or_update_repository_details(
            repository_dict, repository)
        if not repository.private:
            repository.save()
    repo_events = bitbucket_provider.get_repository_events(owner, repo)
    return render_to_response('bitbucket_repo.html', {
        'repository': repository,
        'repo_events': repo_events
    }, RequestContext(request))
예제 #3
0
def bitbucket_username_category_watched(request, username, category):
    """has all bitbucket repos and the latest 30 events for a username with a specific category"""
    owned = False
    username = urllib.unquote(username)
    category = urllib.unquote(category)
    category = category.lower()
    user = request.user
    try:
        bitbucket_username = user.social_auth.get(
            provider='bitbucket').extra_data['username']
        if username == bitbucket_username:
            bitbucket_provider = BitbucketProvider(user)
            watched_filtered, repository_user = bitbucket_provider.retrieve_watched_category_repositories(
                username, category)
            repository_user.save()
            if len(watched_filtered) == 0:
                watched = bitbucket_provider.get_repositories(
                    username, "starred")
                for repo in watched:
                    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):
                        bitbucket_provider.create_or_update_repository_details(
                            repo, repository)
                        if not repository.private:
                            repository.save()
                    if repository.language == category:
                        watched_filtered.append(repository)
                RepositoryCategory.objects.get_or_create(name=category)
                watched_filtered.sort(key=lambda x: x.watchers, reverse=True)
            repo_events = bitbucket_provider.get_repositories_events(
                watched_filtered)
            return render_to_response(
                'bitbucket_username_category_watched.html', {
                    'username': username,
                    'watched': watched_filtered,
                    'category': category,
                    'repo_events': repo_events,
                    'owned': owned
                },
                context_instance=RequestContext(request))
    except ObjectDoesNotExist:
        pass
    res = HttpResponse("Unauthorized")
    res.status_code = 401
    return res
예제 #4
0
def bitbucket_username_watched(request, username):
    user = request.user
    # need to fixed the problem with owned repos
    try:
        bitbucket_username = user.social_auth.get(
            provider='bitbucket').extra_data['username']
        if username == bitbucket_username:
            bitbucket_provider = BitbucketProvider(user)
            repositories_by_language, repository_user = bitbucket_provider.retrieve_watched_repositories_dict(
                username)
            repository_user.save()
            if len(repositories_by_language) == 0:

                repositories = bitbucket_provider.get_watched_repositories(
                    username)
                for repo in repositories:
                    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()
                    repositories_by_language[repository.language].append(
                        repository)
                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('bitbucket_username_watched.html', {
                'username':
                username,
                'repositories_by_language':
                sorted(dict(repositories_by_language).iteritems(),
                       key=lambda (k, v): len(v),
                       reverse=True),
                'owned':
                False
            },
                                      context_instance=RequestContext(request))
    except ObjectDoesNotExist:
        pass
    res = HttpResponse("Unauthorized")
    res.status_code = 401
    return res
예제 #5
0
def bitbucket_username_category_owned(request, username, category):
    """has all bitbucket 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
    username = urllib.unquote(username)
    category = urllib.unquote(category)
    user = request.user
    bitbucket_provider = BitbucketProvider(user)
    watched_filtered, repository_user = bitbucket_provider.retrieve_owned_category_repositories(
        username, category)
    repository_user.save()

    if len(watched_filtered) == 0:
        owned = bitbucket_provider.get_owned_repositories(username)
        category_lower = category.lower()
        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()
            if repository.language == category_lower:
                watched_filtered.append(repository)
            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)
        repository_user.public_repos = len(owned)
        repository_user.save()
        watched_filtered.sort(key=lambda x: x.watchers, reverse=True)
    # Get repository events
    repo_events = bitbucket_provider.get_repositories_events(watched_filtered)
    return render_to_response('bitbucket_username_category_watched.html', {
        'username': username,
        'watched': watched_filtered,
        'category': category,
        'repo_events': repo_events,
        'owned': owned
    },
                              context_instance=RequestContext(request))
예제 #6
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))
예제 #7
0
def bitbucket_username_owned(request, username):
    repo_link_type, _ = LinkType.objects.get_or_create(name="owned")
    user = request.user
    bitbucket_provider = BitbucketProvider(user)
    repositories_by_language, repository_user = bitbucket_provider.retrieve_owned_repositories_dict(
        username)
    repository_user.save()

    if len(repositories_by_language) == 0:
        watched = bitbucket_provider.get_owned_repositories(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()
            if not repository.private:
                RepositoryUserRepositoryLink.objects.get_or_create(
                    user=repository_user,
                    repository=repository,
                    link_type=repo_link_type)
            repositories_by_language[repository.language].append(repository)
        repository_user.public_repos = len(watched)
        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('bitbucket_username_watched.html', {
        'username':
        username,
        'repositories_by_language':
        sorted(dict(repositories_by_language).iteritems(),
               key=lambda (k, v): len(v),
               reverse=True),
        'owned':
        True
    },
                              context_instance=RequestContext(request))
예제 #8
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))
예제 #9
0
 def save_repositories(self, repositories_dict, link_type):
     profile = self.user.get_profile()
     for category_name, value in repositories_dict.iteritems():
         update = False
         if category_name == '':
             category_name = 'other'
         repository_category, _ = RepositoryCategory.objects.get_or_create(
             name=category_name)
         for order, item in enumerate(value, start=1):
             if item != '':
                 try:
                     repository = Repository.objects.get(
                         host_slug=self.host + '/' + item.lower())
                 except ObjectDoesNotExist:
                     repository = Repository()
                     update = True
                 if update or (datetime.now() - repository.last_modified
                               ) > timedelta(days=1):
                     owner, repository_name = item.split('/')
                     repository_dict = self.get_repository_details(
                         owner, repository_name)
                     repository = self.create_or_update_repository_details(
                         repository_dict, repository)
                     if not repository.private:
                         repository.save()
                 repo_link_type, _ = LinkType.objects.get_or_create(
                     name=link_type)
                 try:
                     user_repository_link = UserRepositoryLink.objects.get(
                         user=profile,
                         repository=repository,
                         link_type=repo_link_type)
                 except ObjectDoesNotExist:
                     user_repository_link = UserRepositoryLink(
                         user=profile,
                         repository=repository,
                         link_type=repo_link_type)
                 user_repository_link.repository_category = repository_category
                 user_repository_link.order = order
                 user_repository_link.save()
예제 #10
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))
예제 #11
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))
예제 #12
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'))
예제 #13
0
def bitbucket_username(request, username):
    username = urllib.unquote(username)
    user = request.user
    repository_user = None
    update = False
    response = None

    bitbucket_provider = BitbucketProvider(user)
    # Get user information
    try:
        repository_user = bitbucket_provider.retrieve_user_details(username)
    except ObjectDoesNotExist:
        update = True
        repository_user = RepositoryUser()
    if update or (datetime.now() -
                  repository_user.last_modified) > timedelta(days=1):
        user_dict = bitbucket_provider.get_user_details(username)
        repository_user = bitbucket_provider.create_or_update_user_details(
            user_dict, repository_user)
        repository_user.save()

    user_events = bitbucket_provider.get_user_events(username)
    # Get repository information
    owned_repositories, repository_user = bitbucket_provider.retrieve_owned_repositories_list(
        username)
    watched_repositories, repository_user = bitbucket_provider.retrieve_watched_repositories_list(
        username)

    if len(owned_repositories) == 0:
        repo_link_type, _ = LinkType.objects.get_or_create(name="owned")
        owned = bitbucket_provider.get_owned_repositories(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=repository_user,
                    repository=repository,
                    link_type=repo_link_type)
            owned_repositories.append(repository)
        repository_user.public_repos = len(owned_repositories)
        repository_user.save()

    if len(watched_repositories) == 0:
        watched = bitbucket_provider.get_watched_repositories(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)
        repository_user.starred = len(watched_repositories)
    return render_to_response('bitbucket_username.html', {
        'user_events': user_events,
        'repository_user': repository_user
    },
                              context_instance=RequestContext(request))
예제 #14
0
    def update_category_repositories(self, username, category, link_type):
        repo_link_type, _ = LinkType.objects.get_or_create(name=link_type)
        profile = self.user.get_profile()
        try:
            repository_user = RepositoryUser.objects.get(slug=self.host + '/' +
                                                         username.lower())
            repository_links = repository_user.repositoryuserrepositorylink_set.filter(
                link_type=repo_link_type).filter(
                    repository__host=self.host).select_related('repository')
            repository_links.delete()
        except ObjectDoesNotExist:
            user_dict = self.get_user_details(username)
            repository_user = self.create_or_update_user_details(user_dict)
        category_object, _ = RepositoryCategory.objects.get_or_create(
            name=category.lower())
        watched = list(self.get_repositories(username, link_type))
        category_lower = category.lower()

        # Removes repisotory links that are no longer watched from the api
        saved = Repository.objects.filter(
            userrepositorylink__user=profile
        ).filter(userrepositorylink__link_type=repo_link_type).filter(
            userrepositorylink__repository_category=category_object).filter(
                host=self.host)
        saved_slugs = (e.slug for e in saved)
        saved_slugs_set = set(saved_slugs)
        watched_slugs = (e['owner'] + '/' + e['name'] for e in watched)
        watched_slugs_set = set(watched_slugs)
        removed_slugs = saved_slugs_set - watched_slugs_set
        for slug in removed_slugs:
            repository = Repository.objects.get(host_slug=self.host + '/' +
                                                slug)
            UserRepositoryLink.objects.get(user=profile,
                                           repository=repository,
                                           link_type=repo_link_type).delete()

        # finds the highest order value for the category in user repository links
        order = UserRepositoryLink.objects.filter(user=profile).filter(
            userrepositorylink__link_type=repo_link_type).filter(
                repository_category=category_object).aggregate(
                    max_order=Max('order'))['max_order']

        # saves the repositories and adds any new ones at an order value above the max for the category
        watched_filtered = []
        for repo in watched:
            try:
                repository = Repository.objects.get(host_slug=self.host + '/' +
                                                    repo['owner'].lower() +
                                                    '/' + repo['name'].lower())
            except ObjectDoesNotExist:
                repository = Repository()
            repository = self.create_or_update_repository_details(
                repo, repository)
            if not repository.private:
                repository.save()
            try:
                user_repository_link = UserRepositoryLink.objects.get(
                    user=profile,
                    repository=repository,
                    link_type=repo_link_type)
            except ObjectDoesNotExist:
                if repository.language == category_lower:
                    order = order + 1
                    watched_filtered.append(repository)
                    user_repository_link = UserRepositoryLink(
                        user=profile,
                        repository=repository,
                        order=order,
                        link_type=repo_link_type)
                    user_repository_link.repository_category, _ = RepositoryCategory.objects.get_or_create(
                        name=repository.language)
                    user_repository_link.save()
#            if not repository.private:
#                RepositoryUserRepositoryLink.objects.get_or_create(user = repository_user, repository = repository, owned = owned, starred = starred)
        if repo_link_type.name == "owned":
            repository_user.public_repos = len(watched)
        elif repo_link_type.name == "starred":
            repository_user.starred = len(watched)
        elif repo_link_type.name == "watched":
            repository_user.watched = len(watched)
        return repository_user
예제 #15
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'))
예제 #16
0
def about(request):
    used = [
        Repository(owner='django', name='django', host='github'),
        Repository(owner='omab', name='django-social-auth', host='github'),
        Repository(owner='copitux', name='python-github3', host='github'),
        Repository(owner='kennethreitz', name='requests', host='github'),
        Repository(owner='maraujop', name='requests-oauth', host='github'),
        Repository(owner='jquery', name='jquery', host='github'),
        Repository(owner='jquery', name='jquery-ui', host='github'),
        Repository(owner='twitter', name='bootstrap', host='github'),
        Repository(owner='desandro', name='masonry', host='github'),
        Repository(owner='rmm5t', name='jquery-timeago', host='github'),
        Repository(owner='ask', name='celery', host='github'),
        Repository(owner='ask', name='django-celery', host='github'),
        Repository(owner='andymccurdy', name='redis-py', host='github'),
        Repository(owner='sebleier', name='django-redis-cache', host='github'),
        Repository(owner='jackmoore', name='colorbox', host='github')
    ]
    return render_to_response('about.html', {'used': used},
                              context_instance=RequestContext(request))
예제 #17
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))
예제 #18
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))