diff options
| author | 2018-11-02 18:39:48 +0000 | |
|---|---|---|
| committer | 2018-11-02 18:39:48 +0000 | |
| commit | 6c4666d36d2853a4ee8b9fd61086a1e4f1dd7776 (patch) | |
| tree | 3ae797514bd5d13582075165936aaa503e38daef /api | |
| parent | Merge branch 'snake_idioms' into 'django' (diff) | |
| parent | Added help text (diff) | |
Merge branch 'special_snakes' into 'django'
Django - Special Snakes API
See merge request python-discord/projects/site!45
Diffstat (limited to 'api')
| -rw-r--r-- | api/admin.py | 4 | ||||
| -rw-r--r-- | api/migrations/0012_specialsnake.py | 22 | ||||
| -rw-r--r-- | api/migrations/0013_specialsnake_image.py | 21 | ||||
| -rw-r--r-- | api/migrations/0014_auto_20181025_1959.py | 23 | ||||
| -rw-r--r-- | api/migrations/0015_auto_20181027_1617.py | 19 | ||||
| -rw-r--r-- | api/migrations/0016_auto_20181027_1619.py | 18 | ||||
| -rw-r--r-- | api/migrations/0017_auto_20181029_1921.py | 19 | ||||
| -rw-r--r-- | api/models.py | 20 | ||||
| -rw-r--r-- | api/serializers.py | 15 | ||||
| -rw-r--r-- | api/tests/base.py | 2 | ||||
| -rw-r--r-- | api/tests/test_models.py | 7 | ||||
| -rw-r--r-- | api/urls.py | 13 | ||||
| -rw-r--r-- | api/viewsets.py | 42 | 
13 files changed, 208 insertions, 17 deletions
| diff --git a/api/admin.py b/api/admin.py index 829d74ed..b06cc939 100644 --- a/api/admin.py +++ b/api/admin.py @@ -4,7 +4,8 @@ from .models import (      DocumentationLink, Member,      OffTopicChannelName, Role,      SnakeFact, SnakeIdiom, -    SnakeName, Tag +    SnakeName, SpecialSnake, +    Tag  ) @@ -15,4 +16,5 @@ admin.site.register(Role)  admin.site.register(SnakeFact)  admin.site.register(SnakeIdiom)  admin.site.register(SnakeName) +admin.site.register(SpecialSnake)  admin.site.register(Tag) diff --git a/api/migrations/0012_specialsnake.py b/api/migrations/0012_specialsnake.py new file mode 100644 index 00000000..9c6e8305 --- /dev/null +++ b/api/migrations/0012_specialsnake.py @@ -0,0 +1,22 @@ +# Generated by Django 2.1.2 on 2018-10-22 09:53 + +import api.models +from django.db import migrations, models + + +class Migration(migrations.Migration): + +    dependencies = [ +        ('api', '0011_auto_20181020_1904'), +    ] + +    operations = [ +        migrations.CreateModel( +            name='SpecialSnake', +            fields=[ +                ('name', models.CharField(max_length=140, primary_key=True, serialize=False)), +                ('info', models.TextField()), +            ], +            bases=(api.models.ModelReprMixin, models.Model), +        ), +    ] diff --git a/api/migrations/0013_specialsnake_image.py b/api/migrations/0013_specialsnake_image.py new file mode 100644 index 00000000..a0d0d318 --- /dev/null +++ b/api/migrations/0013_specialsnake_image.py @@ -0,0 +1,21 @@ +# Generated by Django 2.1.2 on 2018-10-23 11:51 + +import datetime +from django.db import migrations, models +from django.utils.timezone import utc + + +class Migration(migrations.Migration): + +    dependencies = [ +        ('api', '0012_specialsnake'), +    ] + +    operations = [ +        migrations.AddField( +            model_name='specialsnake', +            name='image', +            field=models.URLField(default=datetime.datetime(2018, 10, 23, 11, 51, 23, 703868, tzinfo=utc)), +            preserve_default=False, +        ), +    ] diff --git a/api/migrations/0014_auto_20181025_1959.py b/api/migrations/0014_auto_20181025_1959.py new file mode 100644 index 00000000..3599d2cd --- /dev/null +++ b/api/migrations/0014_auto_20181025_1959.py @@ -0,0 +1,23 @@ +# Generated by Django 2.1.2 on 2018-10-25 19:59 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + +    dependencies = [ +        ('api', '0013_specialsnake_image'), +    ] + +    operations = [ +        migrations.AlterField( +            model_name='specialsnake', +            name='info', +            field=models.TextField(help_text='Info about a special snake.'), +        ), +        migrations.AlterField( +            model_name='specialsnake', +            name='name', +            field=models.CharField(help_text='A special snake name.', max_length=140, primary_key=True, serialize=False), +        ), +    ] diff --git a/api/migrations/0015_auto_20181027_1617.py b/api/migrations/0015_auto_20181027_1617.py new file mode 100644 index 00000000..8973ff6d --- /dev/null +++ b/api/migrations/0015_auto_20181027_1617.py @@ -0,0 +1,19 @@ +# Generated by Django 2.1.2 on 2018-10-27 16:17 + +import django.contrib.postgres.fields +from django.db import migrations, models + + +class Migration(migrations.Migration): + +    dependencies = [ +        ('api', '0014_auto_20181025_1959'), +    ] + +    operations = [ +        migrations.AlterField( +            model_name='specialsnake', +            name='image', +            field=django.contrib.postgres.fields.ArrayField(base_field=models.URLField(), size=None), +        ), +    ] diff --git a/api/migrations/0016_auto_20181027_1619.py b/api/migrations/0016_auto_20181027_1619.py new file mode 100644 index 00000000..b8bdfb16 --- /dev/null +++ b/api/migrations/0016_auto_20181027_1619.py @@ -0,0 +1,18 @@ +# Generated by Django 2.1.2 on 2018-10-27 16:19 + +from django.db import migrations + + +class Migration(migrations.Migration): + +    dependencies = [ +        ('api', '0015_auto_20181027_1617'), +    ] + +    operations = [ +        migrations.RenameField( +            model_name='specialsnake', +            old_name='image', +            new_name='images', +        ), +    ] diff --git a/api/migrations/0017_auto_20181029_1921.py b/api/migrations/0017_auto_20181029_1921.py new file mode 100644 index 00000000..012bda61 --- /dev/null +++ b/api/migrations/0017_auto_20181029_1921.py @@ -0,0 +1,19 @@ +# Generated by Django 2.1.2 on 2018-10-29 19:21 + +import django.contrib.postgres.fields +from django.db import migrations, models + + +class Migration(migrations.Migration): + +    dependencies = [ +        ('api', '0016_auto_20181027_1619'), +    ] + +    operations = [ +        migrations.AlterField( +            model_name='specialsnake', +            name='images', +            field=django.contrib.postgres.fields.ArrayField(base_field=models.URLField(), help_text='Images displaying this special snake.', size=None), +        ), +    ] diff --git a/api/models.py b/api/models.py index bb520f8c..9990e266 100644 --- a/api/models.py +++ b/api/models.py @@ -103,6 +103,26 @@ class SnakeName(ModelReprMixin, models.Model):          return f"{self.name} ({self.scientific})" +class SpecialSnake(ModelReprMixin, models.Model): +    """A special snake's name, info and image from our database used by the bot's snake cog.""" + +    name = models.CharField( +        max_length=140, +        primary_key=True, +        help_text='A special snake name.' +    ) +    info = models.TextField( +        help_text='Info about a special snake.' +    ) +    images = pgfields.ArrayField( +        models.URLField(), +        help_text='Images displaying this special snake.' +    ) + +    def __str__(self): +        return self.name + +  class Role(ModelReprMixin, models.Model):      """A role on our Discord server.""" diff --git a/api/serializers.py b/api/serializers.py index 51d89425..f8d15bbf 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -2,10 +2,11 @@ from rest_framework.serializers import ModelSerializer, PrimaryKeyRelatedField  from rest_framework_bulk import BulkSerializerMixin  from .models import ( -    DocumentationLink, Member, -    OffTopicChannelName, Role, -    SnakeFact, SnakeIdiom, -    SnakeName, Tag +    DocumentationLink, +    Member, OffTopicChannelName, +    Role, SnakeFact, +    SnakeIdiom, SnakeName, +    SpecialSnake, Tag  ) @@ -42,6 +43,12 @@ class SnakeNameSerializer(ModelSerializer):          fields = ('name', 'scientific') +class SpecialSnakeSerializer(ModelSerializer): +    class Meta: +        model = SpecialSnake +        fields = ('name', 'images', 'info') + +  class RoleSerializer(ModelSerializer):      class Meta:          model = Role diff --git a/api/tests/base.py b/api/tests/base.py index 395dc55c..5d8ccf8c 100644 --- a/api/tests/base.py +++ b/api/tests/base.py @@ -5,7 +5,7 @@ from rest_framework.test import APITestCase  test_user, _created = User.objects.get_or_create(      username='test',      email='[email protected]', -    password='testpass',  # flake8: noqa - S106 +    password='testpass',  # noqa: S106      is_superuser=True,      is_staff=True  ) diff --git a/api/tests/test_models.py b/api/tests/test_models.py index af2ef5d5..91db2def 100644 --- a/api/tests/test_models.py +++ b/api/tests/test_models.py @@ -4,7 +4,8 @@ from ..models import (      DocumentationLink, Member, ModelReprMixin,      OffTopicChannelName, Role,      SnakeFact, SnakeIdiom, -    SnakeName, Tag +    SnakeName, SpecialSnake, +    Tag  ) @@ -32,6 +33,10 @@ class StringDunderMethodTests(SimpleTestCase):              SnakeFact(fact='snakes are cute'),              SnakeIdiom(idiom='snake snacks'),              SnakeName(name='python', scientific='3'), +            SpecialSnake( +                name='Pythagoras Pythonista', +                info='The only python snake that is born a triangle' +            ),              Role(                  id=5, name='test role',                  colour=0x5, permissions=0 diff --git a/api/urls.py b/api/urls.py index 4f1b2455..8229b08c 100644 --- a/api/urls.py +++ b/api/urls.py @@ -4,8 +4,9 @@ from rest_framework.routers import DefaultRouter  from .views import HealthcheckView  from .viewsets import (      DocumentationLinkViewSet, MemberViewSet, -    OffTopicChannelNameViewSet, SnakeFactViewSet, -    SnakeIdiomViewSet, SnakeNameViewSet, +    OffTopicChannelNameViewSet, +    SnakeFactViewSet, SnakeIdiomViewSet, +    SnakeNameViewSet, SpecialSnakeViewSet,      TagViewSet  ) @@ -27,7 +28,7 @@ bot_router.register(  )  bot_router.register(      'snake-facts', -    SnakeFactViewSet, +    SnakeFactViewSet  )  bot_router.register(      'snake-idioms', @@ -39,8 +40,12 @@ bot_router.register(      base_name='snakename'  )  bot_router.register( +    'special-snakes', +    SpecialSnakeViewSet +) +bot_router.register(      'tags', -    TagViewSet, +    TagViewSet  )  app_name = 'api' diff --git a/api/viewsets.py b/api/viewsets.py index 24c2aef9..08660810 100644 --- a/api/viewsets.py +++ b/api/viewsets.py @@ -11,14 +11,16 @@ from rest_framework_bulk import BulkCreateModelMixin  from .models import (      DocumentationLink, Member, -    OffTopicChannelName, SnakeFact, -    SnakeIdiom, SnakeName, +    OffTopicChannelName, +    SnakeFact, SnakeIdiom, +    SnakeName, SpecialSnake,      Tag  )  from .serializers import ( -    DocumentationLinkSerializer, MemberSerializer, -    OffTopicChannelNameSerializer, SnakeFactSerializer, -    SnakeIdiomSerializer, SnakeNameSerializer, +    DocumentationLinkSerializer, +    MemberSerializer, OffTopicChannelNameSerializer, +    SnakeFactSerializer, SnakeIdiomSerializer, +    SnakeNameSerializer, SpecialSnakeSerializer,      TagSerializer  ) @@ -203,7 +205,7 @@ class SnakeFactViewSet(ListModelMixin, GenericViewSet):  class SnakeIdiomViewSet(ListModelMixin, GenericViewSet):      """ -    View providing  for the snake cog. +    View providing snake idioms for the snake cog.      ## Routes      ### GET /bot/snake-idioms @@ -282,6 +284,34 @@ class SnakeNameViewSet(ViewSet):          return Response({}) +class SpecialSnakeViewSet(ListModelMixin, GenericViewSet): +    """ +    View providing special snake names for our bot's snake cog. + +    ## Routes +    ### GET /bot/special-snakes +    Returns a list of special snake names. + +    #### Response Format +    >>> [ +    ...   { +    ...     'name': 'Snakky sneakatus', +    ...     'info': 'Scary snek', +    ...     'image': 'https://discordapp.com/assets/53ef346458017da2062aca5c7955946b.svg' +    ...   } +    ... ] + +    #### Status codes +    - 200: returned on success + +    ## Authentication +    Requires an API token. +    """ + +    serializer_class = SpecialSnakeSerializer +    queryset = SpecialSnake.objects.all() + +  class TagViewSet(ModelViewSet):      """      View providing CRUD operations on tags shown by our bot. | 
