Commit c3390ac1 authored by Shrikrishna Singh's avatar Shrikrishna Singh

org/: Make an api for active newcomers

This makes an api to show the newcomers
who are active in last three months at
/newcomers/active

Closes coala/landing#66
parent 67840d37
Pipeline #27765774 passed with stages
in 19 minutes and 19 seconds
......@@ -18,7 +18,12 @@ from django.contrib import admin
from django.views.generic import TemplateView
from bears.views import home, search, analytics
from org.views import contrib, issues, merge_requests
from org.views import (
contrib,
issues,
merge_requests,
newcomers_active,
)
from try_online.views import editor
......@@ -33,5 +38,6 @@ urlpatterns = [
url(r'^issues/gitlab/all', issues, {'hoster': 'gitlab'}),
url(r'^mrs/github/all', merge_requests, {'hoster': 'github'}),
url(r'^mrs/gitlab/all', merge_requests, {'hoster': 'gitlab'}),
url(r'^newcomers/active', newcomers_active),
url(r'^', TemplateView.as_view(template_name='home.html'))
]
No preview for this file type
from functools import lru_cache
import datetime
from org.models import Contributor
from igitt_django.models import IGittIssue, IGittMergeRequest
NEWCOMERS_TEAM_NAME = 'coala newcomers'
REMOVE_NEWCOMERS = ['co-robo']
LAST_ACTIVE = (
datetime.datetime.today() - datetime.timedelta(
3*365/12)).isoformat()
@lru_cache(maxsize=32)
def all_newcomers():
"""
Get newcomers all the newcomers.
:return: the list of newcomer usernames
"""
newcomers_list = []
contributors = Contributor.objects.all()
for contributor in contributors:
# Get newcomers who belongs to only one team
# and that is newcomers team.
contributor_teams = contributor.teams.all()
if contributor_teams.count() == 1:
if contributor_teams[0].name == NEWCOMERS_TEAM_NAME:
newcomers_list.append(contributor.login)
for newcomer in REMOVE_NEWCOMERS:
if newcomer in newcomers_list:
newcomers_list.remove(newcomer)
return newcomers_list
def active_newcomers():
"""
Get the newcomers who are active in past three months.
"""
active_newcomers_list = []
issues = IGittIssue.objects.all()
mrs = IGittMergeRequest.objects.all()
all_newcomers_list = all_newcomers()
# Get the datetime three months ago from today.
for issue in issues:
i_updated_at = issue.data['updated_at']
if i_updated_at <= LAST_ACTIVE:
continue
author = issue.data['author']
if author in all_newcomers_list:
if author not in active_newcomers_list:
active_newcomers_list.append(author)
for mr in mrs:
m_updated_at = mr.data['updated_at']
if m_updated_at <= LAST_ACTIVE:
continue
author = issue.data['author']
if author in all_newcomers_list:
if author not in active_newcomers_list:
active_newcomers_list.append(author)
return active_newcomers_list
......@@ -4,6 +4,7 @@ from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods
from django_common.http import HttpResponse
from org.models import Contributor
from org.newcomers import active_newcomers
from igitt_django.models import IGittIssue, IGittMergeRequest
from brake.decorators import ratelimit
......@@ -48,3 +49,16 @@ def merge_requests(request, hoster):
return HttpResponse(
json.dumps(mrs, indent=4, default=str),
content_type='application/json',)
@ratelimit(block=True, rate='1/m')
@csrf_exempt
@require_http_methods(['GET'])
def newcomers_active(request):
newcomers_list = active_newcomers()
active_newcomers_list = []
for newcomer in newcomers_list:
active_newcomers_list.append({'username': newcomer})
return HttpResponse(
json.dumps(active_newcomers_list),
content_type='application/json')
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