Skip to content

Commit

Permalink
FAQ management app and API (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
joahim authored Nov 24, 2021
1 parent 0e435f7 commit 87b9eb3
Show file tree
Hide file tree
Showing 15 changed files with 231 additions and 2 deletions.
Empty file added sledilnik/faq/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions sledilnik/faq/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from adminsortable2.admin import SortableInlineAdminMixin
from modeltranslation.admin import TranslationStackedInline

from django.contrib import admin

from sledilnik.utils import TranslationAdmin

from . import models


class FaqInline(SortableInlineAdminMixin, TranslationStackedInline):
model = models.Faq
extra = 0


@admin.register(models.Project)
class ProjectAdmin(TranslationAdmin):
inlines = [FaqInline]
35 changes: 35 additions & 0 deletions sledilnik/faq/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from tastypie import fields
from tastypie.resources import ModelResource
from tastypie.cache import SimpleCache

from django.conf import settings
from django.utils import translation

from .models import Project, Faq


class FaqResource(ModelResource):
class Meta:
queryset = Faq.objects.all()
fields = ["position", "question", "answer"]
cache = SimpleCache(timeout=60, public=True)


class FaqProjectResource(ModelResource):
faq = fields.ToManyField(FaqResource, "faqs", full=True)

class Meta:
resource_name = "faq"
queryset = Project.objects.all()
fields = ["id", "name"]
allowed_methods = ["get"]
cache = SimpleCache(timeout=60, public=True)

def dehydrate(self, bundle):
lang = bundle.request.GET.get("lang")

if lang not in [lng[0] for lng in settings.LANGUAGES]:
lang = settings.LANGUAGE_CODE
translation.activate(lang)

return bundle
5 changes: 5 additions & 0 deletions sledilnik/faq/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class FaqConfig(AppConfig):
name = 'faq'
45 changes: 45 additions & 0 deletions sledilnik/faq/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Generated by Django 3.1.7 on 2021-11-24 13:01

from django.db import migrations, models
import django.db.models.deletion
import sledilnik.easymde.models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Project',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100, unique=True, verbose_name='Project name')),
('name_sl', models.CharField(max_length=100, null=True, unique=True, verbose_name='Project name')),
('name_en', models.CharField(max_length=100, null=True, unique=True, verbose_name='Project name')),
],
options={
'ordering': ['name'],
},
),
migrations.CreateModel(
name='Faq',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('position', models.PositiveIntegerField(default=0, verbose_name='Position')),
('question', models.CharField(max_length=500, verbose_name='Question')),
('question_sl', models.CharField(max_length=500, null=True, verbose_name='Question')),
('question_en', models.CharField(max_length=500, null=True, verbose_name='Question')),
('answer', sledilnik.easymde.models.MarkdownField(verbose_name='Answer')),
('answer_sl', sledilnik.easymde.models.MarkdownField(null=True, verbose_name='Answer')),
('answer_en', sledilnik.easymde.models.MarkdownField(null=True, verbose_name='Answer')),
('project', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='faq.project', verbose_name='Project')),
],
options={
'ordering': ['position'],
},
),
]
34 changes: 34 additions & 0 deletions sledilnik/faq/migrations/0002_auto_20211124_1414.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 3.1.7 on 2021-11-24 13:14

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('faq', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='project',
name='slug',
field=models.SlugField(default='', help_text='Used to query the API', max_length=100, unique=True, verbose_name='Slug'),
preserve_default=False,
),
migrations.AlterField(
model_name='project',
name='name',
field=models.CharField(max_length=100, unique=True, verbose_name='Name'),
),
migrations.AlterField(
model_name='project',
name='name_en',
field=models.CharField(max_length=100, null=True, unique=True, verbose_name='Name'),
),
migrations.AlterField(
model_name='project',
name='name_sl',
field=models.CharField(max_length=100, null=True, unique=True, verbose_name='Name'),
),
]
21 changes: 21 additions & 0 deletions sledilnik/faq/migrations/0003_auto_20211124_1424.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.1.7 on 2021-11-24 13:24

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('faq', '0002_auto_20211124_1414'),
]

operations = [
migrations.RemoveField(
model_name='project',
name='name_en',
),
migrations.RemoveField(
model_name='project',
name='name_sl',
),
]
23 changes: 23 additions & 0 deletions sledilnik/faq/migrations/0004_auto_20211124_1515.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.1.7 on 2021-11-24 14:15

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


class Migration(migrations.Migration):

dependencies = [
('faq', '0003_auto_20211124_1424'),
]

operations = [
migrations.RemoveField(
model_name='project',
name='slug',
),
migrations.AlterField(
model_name='faq',
name='project',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='faqs', to='faq.project', verbose_name='Project'),
),
]
Empty file.
27 changes: 27 additions & 0 deletions sledilnik/faq/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.db import models
from django.utils.translation import gettext_lazy as _
from sledilnik.easymde.models import MarkdownField


class Project(models.Model):
name = models.CharField(_("Name"), max_length=100, unique=True)

class Meta:
ordering = ["name"]

def __str__(self):
return self.name


class Faq(models.Model):
project = models.ForeignKey(Project, verbose_name=_("Project"), on_delete=models.CASCADE, related_name="faqs")
position = models.PositiveIntegerField(_("Position"), default=0, null=False, blank=False)

question = models.CharField(_("Question"), max_length=500)
answer = MarkdownField(_("Answer"))

class Meta:
ordering = ["position"]

def __str__(self):
return self.question
3 changes: 3 additions & 0 deletions sledilnik/faq/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
12 changes: 12 additions & 0 deletions sledilnik/faq/translation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from modeltranslation.translator import register, TranslationOptions
from . import models


@register(models.Project)
class ProjectTranslationOptions(TranslationOptions):
fields = []


@register(models.Faq)
class FaqTranslationOptions(TranslationOptions):
fields = ["question", "answer"]
3 changes: 3 additions & 0 deletions sledilnik/faq/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
1 change: 1 addition & 0 deletions sledilnik/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

INSTALLED_APPS = [
'sledilnik',
'sledilnik.faq',
'sledilnik.posts',
'sledilnik.modeling',
'sledilnik.restrictions',
Expand Down
6 changes: 4 additions & 2 deletions sledilnik/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
from django.conf.urls.static import static
from django.utils.translation import gettext_lazy as _

import sledilnik.modeling.urls

from sledilnik.posts.api import PostResource
from sledilnik.restrictions.api import RestrictionResource
import sledilnik.modeling.urls
from sledilnik.faq.api import FaqProjectResource

admin.site.enable_nav_sidebar = False
admin.site.site_header = _("Sledilnik Administration")
Expand All @@ -18,7 +20,7 @@
api = Api(api_name='v1')
api.register(PostResource())
api.register(RestrictionResource())

api.register(FaqProjectResource())

urlpatterns = [
path('admin/', admin.site.urls),
Expand Down

0 comments on commit 87b9eb3

Please sign in to comment.