Commit 10f622cc authored by Shrikrishna Singh's avatar Shrikrishna Singh

Add teams field to Contributor model

This adds a team field to Contributor models,
which will show in which teams a contributor
belongs.

Closes coala/landing#49
parent de13a20f
Pipeline #23280265 passed with stages
in 59 minutes and 18 seconds
No preview for this file type
......@@ -6,13 +6,15 @@ import requests
from IGitt.GitHub import GitHub
from coala_web.settings import GITHUB_API_KEY
from org.models import Contributor
from org.models import Contributor, Team
from org.git import get_org
request_headers = {'Authorization': 'token %s' % GITHUB_API_KEY}
def fetch_org():
data = defaultdict(lambda: defaultdict(int, {'name': '', 'bio': ''}))
data = defaultdict(lambda: defaultdict(int, {'name': '', 'bio': '',
'teams': []}))
repo_url = '/orgs/coala/repos'
response_repo = GitHub.get(GITHUB_API_KEY, repo_url)
for repo in response_repo:
......@@ -36,7 +38,19 @@ def fetch_org():
if user['name']:
data[contributor]['name'] = user['name']
org = get_org()
teams = org.get_teams()
for team in teams:
t, created = Team.objects.get_or_create(name=team.name)
if created:
t.save()
contributors = team.get_members()
for contributor in contributors:
contributor = contributor.login
data[contributor]['teams'].append(team.name)
# Saving dict in database
teams = Team.objects.all()
for contributor in data:
if Contributor.objects.filter(login=contributor).exists():
c = Contributor.objects.get(login=contributor)
......@@ -49,6 +63,10 @@ def fetch_org():
c.issues_opened = data[contributor]['issues']
c.reviews = data[contributor]['reviews']
c.save()
for team in teams:
if team.name in data[contributor]['teams']:
c.teams.add(team)
c.save()
def get_commits(repo, data):
......
from github import Github
from coala_web.settings import GITHUB_API_KEY
def get_org():
github_obj = Github(GITHUB_API_KEY)
org = github_obj.get_organization('coala')
return org
# -*- coding: utf-8 -*-
# Generated by Django 1.10.8 on 2018-06-04 14:15
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('org', '0002_contributor'),
]
operations = [
migrations.CreateModel(
name='Team',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(default=None, max_length=200)),
],
),
migrations.AddField(
model_name='contributor',
name='teams',
field=models.ManyToManyField(to='org.Team'),
),
]
from django.db import models
import json
# Create your models here.
class Repo(models.Model):
......@@ -21,6 +20,13 @@ class Repo(models.Model):
return json.loads(self.contributors_data)
class Team(models.Model):
name = models.CharField(max_length=200, default=None)
def __str__(self):
return self.name
class Contributor(models.Model):
login = models.TextField(default=None, primary_key=True)
name = models.TextField(default=None, null=True)
......@@ -28,3 +34,4 @@ class Contributor(models.Model):
num_commits = models.IntegerField(default=None, null=True)
reviews = models.IntegerField(default=None, null=True)
issues_opened = models.IntegerField(default=None, null=True)
teams = models.ManyToManyField(Team)
......@@ -18,6 +18,7 @@ def contrib(request):
'contributions': item.num_commits,
'bio': item.bio,
'login': item.login,
'reviews': item.reviews
'reviews': item.reviews,
'teams': [team.name for team in item.teams.all()],
} for item in response]
return HttpResponse(json.dumps(data), content_type='application/json')
......@@ -114,6 +114,7 @@
<li>Reviews</li>
<li>Bio</li>
<li>Issues Opened</li>
<li>Teams</li>
</ul>
</div>
<div class="col m6">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment