Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release app category update #167

Merged
merged 2 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion v1/app_store/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib import admin

from .models.app import App, AppImage
from .models.app import App, AppImage, Category

admin.site.register(App)
admin.site.register(AppImage)
admin.site.register(Category)
10 changes: 9 additions & 1 deletion v1/app_store/factories/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import factory
from factory.django import DjangoModelFactory, ImageField

from ..models.app import App, AppImage
from ..models.app import App, AppImage, Category


class CategoryFactory(DjangoModelFactory):
name = factory.Faker('pystr', max_chars=255)

class Meta:
model = Category


class AppFactory(DjangoModelFactory):
Expand All @@ -10,6 +17,7 @@ class AppFactory(DjangoModelFactory):
logo = ImageField(width=1024, height=768)
website = factory.Faker('url')
tagline = factory.Faker('pystr', max_chars=255)
category = factory.SubFactory(CategoryFactory)

class Meta:
model = App
Expand Down
33 changes: 33 additions & 0 deletions v1/app_store/migrations/0003_auto_20211004_0505.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 3.1.1 on 2021-10-04 05:05

from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):

dependencies = [
('app_store', '0002_app_tagline'),
]

operations = [
migrations.CreateModel(
name='Category',
fields=[
('created_date', models.DateTimeField(auto_now_add=True, db_index=True, null=True)),
('modified_date', models.DateTimeField(auto_now=True, db_index=True)),
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('name', models.CharField(max_length=255)),
],
options={
'verbose_name_plural': 'categories',
'ordering': ('created_date',),
},
),
migrations.AddField(
model_name='app',
name='category',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='app_store.category'),
),
]
17 changes: 17 additions & 0 deletions v1/app_store/models/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class App(CreatedModified):
website = models.URLField()
images = models.ManyToManyField('AppImage', blank=True, related_name='app_images')
tagline = models.CharField(max_length=255, blank=True, null=True)
category = models.ForeignKey('Category', null=True, blank=True, on_delete=models.SET_NULL)

def __str__(self):
return f'#{self.pk}: {self.name}'
Expand All @@ -33,3 +34,19 @@ def __str__(self):
def save(self, *args, **kwargs):
cache.delete_pattern('views.decorators.cache.cache*')
return super(AppImage, self).save(*args, **kwargs)


class Category(CreatedModified):
uuid = models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True)
name = models.CharField(max_length=255)

class Meta:
verbose_name_plural = 'categories'
ordering = ('created_date',)

def __str__(self):
return f'#{self.pk}: {self.name}'

def save(self, *args, **kwargs):
cache.delete_pattern('views.decorators.cache.cache*')
return super(Category, self).save(*args, **kwargs)
14 changes: 11 additions & 3 deletions v1/app_store/serializers/app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from rest_framework.serializers import ModelSerializer

from ..models.app import App, AppImage
from ..models.app import App, AppImage, Category


class AppSerializer(ModelSerializer):

class Meta:
fields = ('pk', 'name', 'description', 'logo', 'website', 'images', 'tagline',
fields = ('pk', 'name', 'description', 'logo', 'website', 'images', 'tagline', 'category',
'created_date', 'modified_date')
model = App
read_only_fields = ('created_date', 'modified_date',)
Expand All @@ -16,7 +16,7 @@ class Meta:
class AppSerializerCreate(ModelSerializer):

class Meta:
fields = ('pk', 'name', 'description', 'logo', 'website', 'images', 'tagline',
fields = ('pk', 'name', 'description', 'logo', 'website', 'images', 'tagline', 'category',
'created_date', 'modified_date')
model = App
read_only_fields = ('created_date', 'modified_date',)
Expand All @@ -28,3 +28,11 @@ class Meta:
fields = ('pk', 'app', 'image', 'created_date', 'modified_date')
model = AppImage
read_only_fields = ('created_date', 'modified_date',)


class CategorySerializer(ModelSerializer):

class Meta:
fields = ('pk', 'name', 'created_date', 'modified_date')
model = Category
read_only_fields = ('created_date', 'modified_date',)
3 changes: 2 additions & 1 deletion v1/app_store/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from rest_framework.routers import SimpleRouter

from .views.app import AppImageViewSet, AppViewSet
from .views.app import AppImageViewSet, AppViewSet, CategoryViewSet

router = SimpleRouter(trailing_slash=False)
router.register('app_store/apps', AppViewSet)
router.register('app_store/images', AppImageViewSet)
router.register('app/categories', CategoryViewSet)
10 changes: 8 additions & 2 deletions v1/app_store/views/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from config.helpers.cache import CachedModelViewSet
from v1.third_party.rest_framework.permissions import IsStaffOrReadOnly
from ..models.app import App, AppImage
from ..serializers.app import AppImageSerializer, AppSerializer, AppSerializerCreate
from ..models.app import App, AppImage, Category
from ..serializers.app import AppImageSerializer, AppSerializer, AppSerializerCreate, CategorySerializer


class AppViewSet(CachedModelViewSet):
Expand All @@ -25,3 +25,9 @@ class AppImageViewSet(CachedModelViewSet):
queryset = AppImage.objects.all()
serializer_class = AppImageSerializer
permission_classes = [IsStaffOrReadOnly]


class CategoryViewSet(CachedModelViewSet):
queryset = Category.objects.all()
serializer_class = CategorySerializer
permission_classes = [IsStaffOrReadOnly]