Commit 688a677c authored by alexandre's avatar alexandre

Featured scores backend

parent 6685faf4
......@@ -26,6 +26,8 @@ INSTALLED_APPS = [
'django.contrib.messages',
'django.contrib.staticfiles',
'adminsortable2',
'taggit',
'taggit_serializer',
......
from django.contrib import admin
from .models import Attachment, Score
from adminsortable2.admin import SortableAdminMixin, SortableInlineAdminMixin
from .models import Attachment, Score, FeaturedScore
class FeaturedScoreAdmin(SortableAdminMixin, admin.ModelAdmin):
# list_display = ('__unicode__', 'web_publish_date', 'visible')
pass
class AttachmentAdmin(admin.ModelAdmin):
......@@ -10,5 +16,6 @@ class ScoreAdmin(admin.ModelAdmin):
pass
admin.site.register(FeaturedScore, FeaturedScoreAdmin)
admin.site.register(Attachment, AttachmentAdmin)
admin.site.register(Score, ScoreAdmin)
# -*- coding: utf-8 -*-
# Generated by Django 1.10.8 on 2018-05-04 12:09
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('playground', '0017_auto_20180410_1243'),
]
operations = [
migrations.CreateModel(
name='FeaturedScore',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('order', models.PositiveSmallIntegerField(default=0, verbose_name='ordre')),
('score', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='playground.Score')),
],
options={
'ordering': ('order',),
},
),
]
......@@ -42,3 +42,14 @@ class Score(models.Model):
class Meta:
ordering = ['title']
class FeaturedScore(models.Model):
score = models.ForeignKey(Score)
order = models.PositiveSmallIntegerField("ordre", default=0, blank=False, null=False)
class Meta:
ordering = ("order",)
def __str__(self):
return self.score.title
......@@ -3,9 +3,10 @@ from rest_framework import viewsets
from rest_framework.pagination import PageNumberPagination
from rest_framework.parsers import FormParser, MultiPartParser
from rest_framework.filters import OrderingFilter, SearchFilter
from django_filters.rest_framework import DjangoFilterBackend
from django_filters import BooleanFilter
from django_filters.rest_framework import DjangoFilterBackend, FilterSet
# from django_filters.rest_framework import DjangoFilterBackend
from .models import Attachment, Score
from .models import Attachment, Score, FeaturedScore
from .serializers import AttachmentSerializer, ScoreSerializer, ScoreLightSerializer
......@@ -25,6 +26,14 @@ class ScoreViewSetPagination(PageNumberPagination):
# max_page_size = 10000
class ScoreFilter(FilterSet):
is_featured = BooleanFilter(method='my_custom_filter')
class Meta:
model = Score
fields = ('title', 'score_type', 'is_featured')
class ScoreViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
......@@ -33,7 +42,20 @@ class ScoreViewSet(viewsets.ModelViewSet):
pagination_class = ScoreViewSetPagination
filter_backends = (DjangoFilterBackend, OrderingFilter, SearchFilter)
search_fields = ('title',)
filter_fields = ('title', 'score_type')
# filter_fields = ('title', 'score_type')
filter_class = ScoreFilter
def get_queryset(self):
"""
Optionally restricts the returned purchases to a given user,
by filtering against a `username` query parameter in the URL.
"""
queryset = self.queryset
is_featured = self.request.query_params.get('is_featured', None)
if is_featured is not None:
ids = FeaturedScore.objects.all().values_list("score__id", flat=True)
queryset = queryset.filter(id__in=ids)
return queryset
def get_serializer_class(self, *args, **kwargs):
"""
......
......@@ -7,3 +7,4 @@ django-filter
django-compressor
django-taggit
django-taggit-serializer
django-admin-sortable2
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