class RootModel(LabelModel):
    slug = models.SlugField()
    description = models.TextField()

    wq_label_template = "{{slug}}" class UserManagedModel(models.Model):
    user = models.ForeignKey("auth.User", models.CASCADE) class Parent(LabelModel):
    name = models.CharField(max_length=10) class Child(LabelModel):
    name = models.CharField(max_length=10)
    parent = models.ForeignKey(Parent, models.CASCADE, related_name="children") class ItemType(LabelModel):
    name = models.CharField(max_length=10)
    active = models.NullBooleanField(default=True) class Item(LabelModel):
    name = models.CharField(max_length=10)
    type = models.ForeignKey(ItemType, models.CASCADE)

    class Meta:
        ordering = ('type', 'name') class FileModel(LabelModel):
    name = models.CharField(max_length=255)
    file = models.FileField(upload_to='files') class ImageModel(LabelModel):
    name = models.CharField(max_length=255)
    image = models.ImageField(upload_to='files') class SlugModel(LabelModel):
    code = models.SlugField()
    name = models.CharField(max_length=255) class SlugRefParent(LabelModel):
    ref = models.ForeignKey(SlugModel, models.CASCADE)
    name = models.CharField(max_length=255)

    wq_label_template = "{{name}}{{#ref_id}} ({{ref}}){{/ref_id}}" class SlugRefChild(models.Model):
    parent = models.ForeignKey(SlugRefParent, models.CASCADE)
    name = models.CharField(max_length=255) class DateModel(models.Model):
    name = models.CharField(max_length=10)
    date = models.DateTimeField()
    empty_date = models.DateTimeField(null=True)

    def __str__(self):
        return "%s on %s" % (self.name, self.date.date()) class ChoiceModel(LabelModel):
    CHOICE_CHOICES = [
        ('one', 'Choice One'),
        ('two', 'Choice Two'),
        ('three', 'Choice Three'),
    ]
    name = models.CharField(
        max_length=10,
        help_text='Enter Name',
    )
    choice = models.CharField(
        max_length=10,
        help_text='Pick One',
        choices=CHOICE_CHOICES,
    )

    wq_label_template = "{{name}}: {{choice}}" class TranslatedModel(models.Model):
    name = models.CharField(
        verbose_name=_('translated model name'),
        max_length=255,
    ) class CharFieldModel(models.Model):
    required_field = models.CharField(max_length=20)
    nullable_field = models.CharField(null=True, max_length=20)
    blankable_field = models.CharField(blank=True, max_length=20)
    nullableblankable_field = models.CharField(
        null=True, blank=True, max_length=20
    ) class ExpensiveModel(LabelModel):
    name = models.CharField(max_length=20)
    expensive = ExpensiveField(cost=2, max_length=255)
    more_expensive = ExpensiveField(
        cost=5,
        max_length=255,
        null=True,
        blank=True
    ) class FieldsetModel(LabelModel):
    # General
    name = models.CharField(max_length=50)
    title = models.CharField(max_length=20)

    # Address
    address = models.CharField(max_length=255)
    city = models.CharField(max_length=255) class RootModelSerializer(ModelSerializer):
    extramodels = ModelSerializer.for_model(
        ExtraModel, include_fields="__all__"
    )(many=True)
    onetoonemodel = ModelSerializer.for_model(
        OneToOneModel, include_fields="__all__"
    )() class ParentSerializer(ModelSerializer):
    children = ChildSerializer(many=True) class ItemSerializer(ModelSerializer):
    class Meta:
        wq_field_config = {
            'type': {
                 'filter': {
                     'active': [
                         '1',

                         # Allow inactive types when editing existing items
                         '{{#id}}0{{/id}}{{^id}}1{{/id}}',
                     ]
                 }
            }
        } class SlugRefChildSerializer(ModelSerializer):
    class Meta:
        wq_field_config = {
            'parent': {
                 'filter': {
                     'ref_id': 'test'
                 }
            }
        } class ExpensiveSerializer(ModelSerializer):
    class Meta:
        fields = "__all__"
        list_exclude = ('expensive', 'more_expensive')
        config_exclude = ('more_expensive',)
        model = ExpensiveModel class FieldsetSerializer(ModelSerializer):
    status = serializers.SerializerMethodField(
        style={'wq_config': {'disabled': True}}
    )
    is_complete = serializers.SerializerMethodField()

    def get_status(self, instance):
        return 'OK'

    def get_is_complete(self, instance):
        return True

    class Meta:
        model = FieldsetModel
        fields = "__all__"
        wq_fieldsets = {
            'general': {
                'label': 'General',
                'fields': ['name', 'title', 'status']
            },
            'contact': {
                'label': 'Contact Information',
                'control': {'appearance': 'contact-fieldset'},
                'fields': ['address', 'city']
            }
        }
from wq.db import rest
from .models import (
    RootModel, UserManagedModel, Parent, Child, ItemType, Item,
    FileModel, ImageModel,
    SlugModel, SlugRefParent, SlugRefChild,
    DateModel, ChoiceModel, TranslatedModel,
    CharFieldModel, ExpensiveModel, FieldsetModel
)
from .serializers import (
    RootModelSerializer, ParentSerializer, ItemSerializer,
    SlugRefChildSerializer, ExpensiveSerializer, FieldsetSerializer,
)


def cache_users_own_data(qs, req):
    if req.user.is_authenticated:
        return qs.filter(user=req.user)
    else:
        return qs.none()


rest.router.register_model(
    RootModel,
    url="",
    lookup="slug",
    serializer=RootModelSerializer,
    fields="__all__",
)
rest.router.register_model(
    UserManagedModel,
    fields="__all__",
    cache_filter=cache_users_own_data,
)
rest.router.register_model(
    Parent,
    serializer=ParentSerializer,
    fields="__all__",
)
rest.router.register_model(
    Child,
    per_page=100,
    url="children",
    fields="__all__",
)
rest.router.register_model(

    ItemType,