Commit c2603381 authored by Michal Čihař's avatar Michal Čihař

Add JSON view for credits

Issue #798
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 029fec1e
......@@ -37,6 +37,7 @@ from weblate.trans.specialchars import get_special_chars
from weblate.trans.validators import validate_check_flags
from weblate.accounts.forms import sort_choices
from urllib import urlencode
from datetime import date
from weblate.logger import LOGGER
from weblate import get_doc_url
......@@ -809,3 +810,29 @@ class UserManageForm(forms.Form):
raise ValidationError(_('No matching user found!'))
except User.MultipleObjectsReturned:
raise ValidationError(_('More users matched!'))
class ReportDownloadForm(forms.Form):
style = forms.ChoiceField(
label=_('Report format'),
help_text=_('Choose file format for the report'),
choices=(
('rst', _('reStructuredText')),
('json', _('JSON'))
),
)
class CreditsForm(ReportDownloadForm):
start_date = forms.DateField(
label=_('Starting date'),
initial=date(2000, 1, 1),
widget=forms.DateInput(
attrs={
'type': 'date',
'data-provide': 'datepicker',
'data-date-format': 'yyyy-mm-dd',
},
format='%Y-%m-%d'
)
)
......@@ -21,8 +21,10 @@
from weblate.trans.tests.test_views import ViewTestCase
from weblate.trans.views.reports import generate_credits
from django.core.urlresolvers import reverse
from django.utils import timezone
from datetime import timedelta
import json
class ReportsTest(ViewTestCase):
......@@ -59,3 +61,15 @@ class ReportsTest(ViewTestCase):
'Nazdar svete2!\n'
)
self.test_credits_one()
def test_credits_view_json(self):
self.add_change()
response = self.client.post(
reverse('credits', kwargs=self.kw_subproject),
{'style': 'json', 'start_date': '2000-01-01'},
)
credits = json.loads(response.content)
self.assertEqual(
credits,
[{'Czech': [['noreply@weblate.org', 'Weblate Test']]}]
)
......@@ -36,7 +36,7 @@ from weblate.lang.models import Language
from weblate.trans.forms import (
get_upload_form, SearchForm,
AutoForm, ReviewForm, NewLanguageForm,
UserManageForm,
UserManageForm, CreditsForm,
)
from weblate.trans.permissions import can_automatic_translation
from weblate.accounts.models import Profile, notify_new_language
......@@ -314,6 +314,7 @@ def show_translation(request, project, subproject, lang):
'search_form': search_form,
'review_form': review_form,
'last_changes': last_changes,
'credits_form': CreditsForm(),
'last_changes_url': urlencode(obj.get_kwargs()),
'show_only_component': True,
'other_translations': Translation.objects.filter(
......
......@@ -19,6 +19,12 @@
#
from weblate.trans.models.changes import Change
from weblate.trans.forms import CreditsForm
from weblate.trans.views.helper import get_subproject
from django.http import HttpResponse
from django.views.decorators.http import require_POST
from django.shortcuts import redirect
import json
def generate_credits(component, start_date):
......@@ -37,3 +43,21 @@ def generate_credits(component, start_date):
result.append({translation.language.name: sorted(set(authors))})
return result
@require_POST
def get_credits(request, project, subproject):
"""View for credits"""
obj = get_subproject(request, project, subproject)
form = CreditsForm(request.POST)
if not form.is_valid():
return redirect(obj)
credits = generate_credits(obj, form.cleaned_data['start_date'])
if form.cleaned_data['style'] == 'json':
return HttpResponse(
json.dumps(credits),
content_type='application/json'
)
......@@ -185,6 +185,11 @@ urlpatterns = patterns(
'weblate.trans.views.edit.auto_translation',
name='auto_translation',
),
url(
r'^credits/' + SUBPROJECT + '$',
'weblate.trans.views.reports.get_credits',
name='credits',
),
url(
r'^new-lang/' + SUBPROJECT + '$',
'weblate.trans.views.basic.new_language',
......
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