예제 #1
0
 def get(self, request):
     repo = self.repo.repo
     path = self.path
     ref = request.get_form_var("ref", "HEAD")
     item = repo.get_path_by_ref("%s:%s" % (ref, path))
     if not item:
         raise api_errors.NotFoundError('blob %s', path)
     if item.type != "blob":
         raise api_errors.NotFoundError('blob %s', path)
     commit = repo.get_last_commit(ref, path=path)
     r = item.as_dict()
     r['last_commit'] = commit.as_dict()
     return r
예제 #2
0
 def commits_by_path(self, request):
     if not self.project:
         raise api_errors.NotFoundError("project")
     path = request.get_form_var('path', '')
     ref = request.get_form_var('ref') or 'HEAD'
     commits = {'r': 1}
     cs = {}
     try:
         tree = self.project.repo.get_tree(ref, path=path, with_commit=True)
         for v in tree:
             k = v['id']
             v['message_with_emoji'] = parse_emoji(
                 escape(v['commit']['message']))
             author = get_author_by_email(v['commit']['author']['email'])
             if author:
                 v['contributor'] = author
                 v['contributor_url'] = "%s/people/%s" % (DOMAIN, author)
             else:
                 v['contributor'] = v['commit']['author']['name']
             v['age'] = compute_relative_time(v['commit']['author']['time'])
             v['sha'] = v['commit']['sha']
             v['commit_id'] = v['commit']['sha']
             cs[k] = v
         commits['commits'] = cs
     except KeyError:
         commits = {'r': '0', 'err': 'Path not found'}
     except IOError:
         commits = {'r': '0', 'err': 'Path not found'}
     return json.dumps(commits)
예제 #3
0
 def src(self, request):
     if not self.project:
         raise api_errors.NotFoundError("project")
     ''' e.g. /api/testuser/testproj/src?path=test.md&ref=5c712b6712bec9c4ed1531b61fd7a4cbcbf3fe90 '''  # noqa
     path = request.get_form_var('path')
     ref = request.get_form_var('ref') or 'HEAD'
     t, src = '', ''
     _ref = ':'.join((ref, path or ''))
     try:
         # file_content = src.data
         src = self.project.repo.get_path_by_ref(_ref)
         if not src:
             raise ValueError
         t = src.type
         if t == 'blob':
             if src.binary:
                 if path.endswith('.pdf'):
                     src = '<a class="media" href="%s"></a>' % (
                         '/' + self.project.name + '/raw/' + ref + '/' +
                         path)
                 else:
                     src = '<div class="rawfile">The content of %s appear to be raw binary, please use raw view instead</div>' % path  # noqa
             elif path.endswith(('md', 'mkd', 'markdown')):
                 src = '<div class="markdown-body">{}</div>'.format(
                     format_md_or_rst(path, src.data, self.project.name))
             else:
                 src = highlight_code(path, src.data, div=True)
         elif t == 'tree':
             src = [dict(e) for e in src]
     except (KeyError, ValueError):
         t = 'blob'
         src = '<div class="error"><i class="icon-exclamation-sign"></i> File not found.</div>'  # noqa
     data = {'path': path, 'type': t, 'src': src}
     return json.dumps(data)
예제 #4
0
파일: gist.py 프로젝트: jackfrued/code-1
    def __init__(self, request, id):
        self.id = id
        self.gist = Gist.get(self.id)

        if not self.gist:
            raise api_errors.NotFoundError("gist")

        self.is_owner = False
        self.comments = GistCommentsUI(request, self.gist)
예제 #5
0
 def __init__(self, request, proj_name, issue_number):
     self.proj_name = proj_name
     self.issue_number = issue_number
     self.project = CodeDoubanProject.get_by_name(self.proj_name)
     self.project_issue = ProjectIssue.get(project_id=self.project.id,
                                           number=self.issue_number)
     if not self.project_issue:
         raise api_errors.NotFoundError('project issue')
     self.comments = IssueCommentsUI(request, self.project_issue)
     self.user = request.user
예제 #6
0
def _q_exception_handler(request, exception):
    # return Not Found instead of TraversalError in api
    if isinstance(exception, TraversalError):
        exception = api_errors.NotFoundError()

    if not isinstance(exception, (OAuthError, CodeAPIError)):
        raise exception

    if isinstance(exception, CodeAPIError):
        error_data = exception.to_dict()
    else:
        error_data = dict(code=exception.status_code,
                          msg=exception.message,
                          request='%s: %s' % (request.method, request.url))
    request.response.set_content_type('application/json; charset=utf-8')
    return json.dumps(error_data)
예제 #7
0
파일: gists.py 프로젝트: jackfrued/code-1
def _q_lookup(request, item):
    if item.isdigit():
        return GistUI(request, item)
    raise api_errors.NotFoundError("gist")
예제 #8
0
 def _q_lookup(self, request, part):
     name = "%s/%s" % (self.name, part)
     if not CodeDoubanProject.exists(name):
         raise api_errors.NotFoundError("repo")
     return RepositoryUI(name)
예제 #9
0
 def _q_index(self, request):
     if not self.repo:
         raise api_errors.NotFoundError("repo")
     return {}
예제 #10
0
 def _q_lookup(self, request, part):
     repo = self.repo.repo
     commit = repo.get_commit(part)
     if not commit:
         raise api_errors.NotFoundError('commit %s' % part)
     return CommitUI(self.repo, commit)
예제 #11
0
 def __init__(self, repo):
     if not repo:
         raise api_errors.NotFoundError("repo")
     self.repo = repo
예제 #12
0
 def _index(self, request):
     if not self.team:
         raise api_errors.NotFoundError('team')
     return self.team.as_dict()
예제 #13
0
파일: issues.py 프로젝트: jackfrued/code-1
 def _q_lookup(self, request, issue_number):
     repo = self.repo
     issue = ProjectIssue.get(project_id=repo.id, number=issue_number)
     if not issue:
         raise api_errors.NotFoundError('project issue')
     return IssueUI(request, repo, issue)
예제 #14
0
 def meta(self, request):
     if not self.project:
         raise api_errors.NotFoundError("project")
     data = self.project.get_info()
     request.response.set_content_type('application/json; charset=utf8')
     return json.dumps(data)
예제 #15
0
 def hooks(self, request):
     if not self.project:
         raise api_errors.NotFoundError("project")
     data = map((lambda x: x.__dict__), self.project.hooks)
     return json.dumps(data)
예제 #16
0
 def _q_index(self, request):
     if self.project:
         return self._project_json(request)
     else:
         raise api_errors.NotFoundError('repo')