Ejemplo n.º 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
Ejemplo n.º 2
0
    def create_or_update_repository_details(self, repository_dict, repository = None):
        extra_data = {}
        if repository is None:
            repository = Repository()
        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', 'followers_count':'watchers'}
        keys = key_map.keys()
        for key,value in repository_dict.iteritems():
            try:
                value = value.replace("/","")
            except AttributeError:
                pass
            if key in keys:
                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.language = repository.language.lower()
        repository.host =self.host
        return repository
Ejemplo n.º 3
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))