Skip to content

Commit c2f53ff

Browse files
authored
Merge pull request #23 from biojs/develop
Develop
2 parents 301c3e3 + 913ce00 commit c2f53ff

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.13 on 2018-06-16 16:59
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
import django.db.models.deletion
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
dependencies = [
12+
('main', '0009_auto_20180609_0855'),
13+
]
14+
15+
operations = [
16+
migrations.AlterField(
17+
model_name='contribution',
18+
name='component',
19+
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='contributions', to='main.Component'),
20+
),
21+
]

main/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,5 @@ def __unicode__(self):
6666

6767
class Contribution(models.Model):
6868
contributor = models.ForeignKey(Contributor, on_delete=models.SET_NULL, null=True)
69-
component = models.ForeignKey(Component, on_delete=models.SET_NULL, null=True)
69+
component = models.ForeignKey(Component, on_delete=models.SET_NULL, null=True, related_name='contributions')
7070
contributions = models.IntegerField(default=0)

main/views.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
55
from django.http import HttpResponse
66
import urllib, json
77
from django.core.management import call_command
8+
from django.core.serializers.json import DjangoJSONEncoder
89

910
def index(request):
10-
top_dl_components = Component.objects.all().order_by('-downloads')[:3]
11-
top_starred_components = Component.objects.all().order_by('-stars')[:3]
12-
recent_components = Component.objects.all().order_by('-modified_time')[:3]
13-
dl = DownloadSerializer(top_dl_components, many=True) # serialized data containing number of downloads
11+
top_downloaded_components = Component.objects.all().only('name', 'id', 'downloads', 'url_name').order_by('-downloads')[:3]
12+
top_starred_components = Component.objects.all().only('name', 'id', 'stars', 'url_name').order_by('-stars')[:3]
13+
recent_components = Component.objects.all().only('name', 'id', 'modified_time', 'url_name').order_by('-modified_time')[:3]
14+
downloaded = DownloadSerializer(top_downloaded_components, many=True) # serialized data containing number of downloads
1415
starred = StarSerializer(top_starred_components, many=True) # serialized data containing number of stars
15-
recent = RecentComponentSerializer(recent_components, many=True) # serialized data according to upload time
16+
recent = RecentComponentSerializer(recent_components, many=True)
1617
return JsonResponse({
17-
'top_dl_components':dl.data,
18+
'top_dl_components':downloaded.data,
1819
'top_starred_components':starred.data,
1920
'most_recent_components':recent.data,
2021
})
2122

2223
def all_components(request): # requested on_load() for querying
23-
all_components = BaseComponentSerializer(Component.objects.all(), many=True)
24+
all_components = BaseComponentSerializer(Component.objects.all().only('name', 'id', 'url_name'), many=True)
2425
return JsonResponse({
2526
'all_components':all_components.data,
2627
})
@@ -35,7 +36,7 @@ def top_components(request):
3536
def component_details(request, url_name):
3637
component = Component.objects.get(url_name=url_name)
3738
details = DetailComponentSerializer(component, context={'request':request})
38-
contributions = ContributionSerializer(Contribution.objects.filter(component=component), many=True)
39+
contributions = ContributionSerializer(component.contributions, many=True)
3940
return JsonResponse({
4041
'details' : details.data,
4142
'contributors' : contributions.data,

0 commit comments

Comments
 (0)