def post(self): """Create a project from the api. Example:: { "url": "http://github.com/defunkt/github", "name": "github", "description": "You're lookin' at it.", "watchers": 5, "forks": 2, "private": 1, "email": "*****@*****.**", "account": "twitter_name", }, """ form = self.parse_form() if not form.is_valid(): return self.respond_json(form.errors, status_code=400) # Lookup the user by email or account email = form.cleaned_data.pop('email', None) account = form.cleaned_data.pop('account', None) user = None if email: user = User.get_by_auth_id('email:%s' % email) elif account: user = User.get_by_auth_id('twitter:%s' % account) created = False project_url = form.cleaned_data['url'] project_key = Project.make_key(project_url) project = project_key.get() if project is None: created = True project = Project(key=project_key, **form.cleaned_data) project.put() @ndb.transactional def txn(): count = getattr(user, 'total', 0) projects = set(getattr(user, 'projects', [])) user.total = count + 10 user.projects = list(projects.add(project_url)) user.put() return user if created and user: txn() self.respond_json({'project': self.serialize(project)}, status_code=201 if created else 200)
def post(self, request): payload = self.parse_payload(request) logging.info(payload) if not payload: raise http.HttpResponseBadRequest try: data = json.loads(payload) except: logging.exception("Unable to serialize POST") raise http.HttpResponseBadRequest commit_data = data.get('commits', []) repo = self._parse_repo(data) project, _ = Project.create(**repo) commit_dict = self.parse_commits(commit_data, project) total_commits = [] for email, commits in commit_dict.iteritems(): # TODO: run this in a task queue? cmts = Commit.create_by_email(email, commits, project=project) total_commits += cmts status = 201 if len(total_commits) else 200 self._publish_commits(total_commits) return self.respond_json({'commits': [c.hash for c in total_commits]}, status=status)
def test_post_adds_points_to_project(self): user = self.make_user('chris') user.add_auth_id('email:[email protected]') self.app.post('/api/v1/github', self.POST) p_key = Project.make_key('http://github.com/defunkt/github') p = p_key.get() self.assertEqual(p.total, 12)
def test_post_adds_points_to_project(self): user = self.make_user('marcus') user.add_auth_id('email:[email protected]') self.app.post('/api/v1/bitbucket', self.POST) p_key = Project.make_key('https://bitbucket.org/marcus/project-x/') p = p_key.get() self.assertEqual(p.total, 11)
def test_testing_mode_off(self): settings.TESTING = False user = self.make_user('chris') user.add_auth_id('email:[email protected]') self.app.post('/api/v1/github', self.POST) p_key = Project.make_key('http://github.com/defunkt/github') # project should not be created self.assertEqual(p_key.get(), None)
def test_testing_mode_off(self): settings.TESTING = False user = self.make_user('marcus') user.add_auth_id('email:[email protected]') self.app.post('/api/v1/bitbucket', self.POST) u = User.get_by_auth_id('email:[email protected]') p_key = Project.make_key('https://bitbucket.org/marcus/project-x/') # project should not be created self.assertEqual(p_key.get(), None)
def test_disabled_project(self): kwargs = { 'name': 'foo', 'url': 'http://example.com/user/repo', 'repo_id': 1, 'service': 'github', } Project.objects.create(active=False, **kwargs) project = Project.create(**kwargs) self.assertEqual(project, None)
def projects(request, template_name='projects/index.html'): limit = 100 cursor = request.GET.get('cursor') if cursor: cursor = Cursor(urlsafe=cursor) query = Project.query().order(-Project.total) models, next_cursor, more = query.fetch_page(limit, start_cursor=cursor) return render_to_response(template_name, {'projects': models, 'next': next_cursor, 'more': more}, context_instance=RequestContext(request))
def people_projects(request, username): user = User.get_by_auth_id('own:%s' % username) if user == None: raise Http404("User not found") if getattr(user, 'projects', None) == None: projects = [] else: projects = user.projects projects = ndb.get_multi([Project.make_key(project) for project in projects]) return render_to_response('people/people_projects.html', {"projects":projects, 'profile':user}, context_instance=RequestContext(request))
def people_projects(request, username): user = User.get_by_auth_id('twitter:%s' % username) if user == None: raise Http404("User not found") if getattr(user, 'projects', None) == None: projects = [] else: projects = user.projects projects = [Project.get_or_create(url=project)[1] for project in projects] return render_to_response('people/people_projects.html', {"projects":projects, 'profile':user}, context_instance=RequestContext(request))
def post(self): payload = self.parse_payload() logging.info(payload) if not payload: abort(400) try: data = json.loads(payload) except: logging.exception("Unable to serialize POST") abort(400) repository = data.get('repository', {}) commit_data = data.get('commits', []) repo = self._parse_repo(repository) commit_dict = self.parse_commits(commit_data) total_commits = [] for email, commits in commit_dict.iteritems(): # TODO: run this in a task queue? cmts = Commit.create_by_email(email, commits, project=repo.get('url', '')) total_commits += cmts status = 200 # If we found commits try to save the project too. if len(total_commits): status = 201 _, project = Project.get_or_create(**repo) project_key = project.key @ndb.transactional def txn(): # TODO: run this in a task queue? total = len(total_commits) project = project_key.get() logging.info("adding %s points to %s", total, project) project.total += total project.put() txn() return self.respond_json( {'commits': [c.urlsafe() for c in total_commits]}, status_code=status)
def post(self): payload = self.parse_payload() logging.info(payload) if not payload: abort(400) try: data = json.loads(payload) except: logging.exception("Unable to serialize POST") abort(400) repository = data.get('repository', {}) commit_data = data.get('commits', []) repo = self._parse_repo(repository) commit_dict = self.parse_commits(commit_data) total_commits = [] for email, commits in commit_dict.iteritems(): # TODO: run this in a task queue? cmts = Commit.create_by_email(email, commits, project=repo.get('url', '')) total_commits += cmts status = 200 # If we found commits try to save the project too. if len(total_commits): status = 201 _, project = Project.get_or_create(**repo) project_key = project.key @ndb.transactional def txn(): # TODO: run this in a task queue? total = len(total_commits) project = project_key.get() logging.info("adding %s points to %s", total, project) project.total += total project.put() txn() return self.respond_json({'commits': [c.urlsafe() for c in total_commits]}, status_code=status)
def handle(self, *args, **options): if len(args) != 1: raise CommandError('Must supply a JSON file of commits.') commit_path = args[0] if os.path.isdir(commit_path): files = [os.path.join(commit_path, f) for f in os.listdir(commit_path) if f.startswith('commits')] else: files = [commit_path] for commits_json in files: logging.info("Parsing File: %s", commits_json) with open(commits_json, 'r') as commit_file: commits = json.loads(commit_file.read()) for commit in commits['models']: try: project, _ = Project.create(url=commit['project']) c = to_commit(commit) Commit.create_by_email(c['email'], c, project) except Exception: logging.exception("Error: %s" % commit)
def post(self, request): payload = self.parse_payload(request) if not payload: return http.HttpResponseBadRequest() elif isinstance(payload, http.HttpResponse): return payload try: data = json.loads(payload) except: logging.exception("Unable to serialize POST") return http.HttpResponseBadRequest() commit_data = data.get('commits', []) repo = self._parse_repo(data) logging.info(repo) project = Project.create(**repo) if project is None: logging.error("Project Disabled") # TODO: discover what response codes are helpful to github # and bitbucket return self.respond_json({'error': 'abuse'}, status=202) commit_dict = self.parse_commits(commit_data, project) total_commits = [] for email, commits in commit_dict.iteritems(): # TODO: run this in a task queue? cmts = Commit.create_by_email(email, commits, project=project) total_commits += cmts status = 201 if len(total_commits) else 200 self._publish_commits(total_commits) return self.respond_json( {'commits': [c.hash for c in total_commits]}, status=status)
def index(request): """Render the home page""" # For now we are just using hard coded sections #sections = cache.get('front_page') #if sections is None: # sections = Section.all().order('order').fetch(10) # cache.set('front_page', sections, 120) stats = [] total = 0 people = [] locations = [] projects = [] teams = [] messages = [] token = '' # this is only shown on authenticated page loads # to save on the overhead. if True: stats = Accumulator.get_histogram('global') total = sum(stats) location_future = Location.query().order(-Location.total).fetch_async(15) people_future = User.query().order(-ndb.GenericProperty('total')).fetch_async(10) project_future = Project.query().order(-Project.total).fetch_async(10) team_future = Team.query().order(-Team.total).fetch_async(15) message_future = Message.query().order(-Message.timestamp).fetch_async(30) # Julython live stuffs #token_key = 'live_token:%s' % request.user.username #token = memcache.get(token_key) #if token is None: #token = channel.create_channel(request.user.username) #memcache.set(token_key, token, time=7000) locations = location_future.get_result() people = people_future.get_result() projects = project_future.get_result() teams = team_future.get_result() message_models = message_future.get_result() m_list = [to_dict(m) for m in message_models] m_list.reverse() messages = json.dumps(m_list) ctx = Context({ 'sections': [], 'people': people, 'projects': projects, 'locations': locations, 'teams': teams, 'stats': json.dumps(stats), 'total': total, 'token': token, 'messages': messages, 'user': request.user, 'MEDIA_URL': settings.MEDIA_URL, 'STATIC_URL': settings.STATIC_URL}) return render_to_response('index.html', context_instance=ctx)
def make_project(self, name, save=True, **kwargs): key = ndb.Key('Project', name) project = Project(key=key, **kwargs) if save: project.put() return project
def index(request): """Render the home page""" # For now we are just using hard coded sections #sections = cache.get('front_page') #if sections is None: # sections = Section.all().order('order').fetch(10) # cache.set('front_page', sections, 120) stats = [] total = 0 people = [] locations = [] projects = [] teams = [] messages = [] token = '' # this is only shown on authenticated page loads # to save on the overhead. if True: stats = Accumulator.get_histogram('global') total = sum(stats) location_future = Location.query().order(-Location.total).fetch_async( 15) people_future = User.query().order( -ndb.GenericProperty('total')).fetch_async(10) project_future = Project.query().order(-Project.total).fetch_async(10) team_future = Team.query().order(-Team.total).fetch_async(15) message_future = Message.query().order(-Message.timestamp).fetch_async( 30) # Julython live stuffs #token_key = 'live_token:%s' % request.user.username #token = memcache.get(token_key) #if token is None: #token = channel.create_channel(request.user.username) #memcache.set(token_key, token, time=7000) locations = location_future.get_result() people = people_future.get_result() projects = project_future.get_result() teams = team_future.get_result() message_models = message_future.get_result() m_list = [to_dict(m) for m in message_models] m_list.reverse() messages = json.dumps(m_list) ctx = Context({ 'sections': [], 'people': people, 'projects': projects, 'locations': locations, 'teams': teams, 'stats': json.dumps(stats), 'total': total, 'token': token, 'messages': messages, 'user': request.user, 'MEDIA_URL': settings.MEDIA_URL, 'STATIC_URL': settings.STATIC_URL }) return render_to_response('index.html', context_instance=ctx)
def test_parse_project_url(self): url = 'http://example.com/user/repo.git' self.assertEqual(Project.parse_project_name(url), 'example-com-user-repo_git')
def test_parse_none(self): self.assertEqual(None, Project.parse_project_name(None))
def make_project(self, url="http://github.com/project", name="test"): return Project.create(url=url, name=name)
def make_project(self, url='http://github.com/project', name='test'): return Project.create(url=url, name=name)
def test_parse_project_url(self): url = 'http://example.com/user/repo.git' self.assertEqual( Project.parse_project_name(url), 'example-com-user-repo_git')