from devilry.simplified import (SimplifiedModelApi, simplified_modelapi,
                                PermissionDenied, FieldSpec,
                                FilterSpecs, FilterSpec,
                                boolConverter)
from devilry.apps.core import models


@simplified_modelapi
class SimplifiedRelatedStudentKeyValue(SimplifiedModelApi):
    """ Simplified wrapper for :class:`devilry.apps.core.models.RelatedStudentKeyValue`. """
    class Meta:
        model = models.RelatedStudentKeyValue
        methods = ['create', 'read', 'update', 'delete', 'search']
        resultfields = FieldSpec('id', 'application', 'key', 'value', 'relatedstudent', 'student_can_read')
        searchfields = FieldSpec('application', 'key', 'value', 'relatedstudent__user__username')
        editablefields = ('application', 'key', 'value', 'relatedstudent', 'student_can_read')
        filters = FilterSpecs(FilterSpec('id', supported_comp=('exact',)),
                              FilterSpec('student_can_read', supported_comp=('exact',), type_converter=boolConverter),
                              FilterSpec('application', supported_comp=('exact',)),
                              FilterSpec('relatedstudent__period', supported_comp=('exact',)),
                              FilterSpec('relatedstudent__user', supported_comp=('exact',)))

    @classmethod
    def create_searchqryset(cls, user):
        """ Returns all related users of this type where given ``user`` is admin or superadmin.

        :param user: A django user object.
        :rtype: a django queryset
        """
        return cls._meta.model.where_is_admin_or_superadmin(user)

    @classmethod
    def write_authorize(cls, user, obj):
        """ Check if the given ``user`` can save changes to the given
        ``obj``, and raise ``PermissionDenied`` if not.

        :param user: A django user object.
        :param obj: A object of the type this method is used in.
        :throws PermissionDenied:
        """
        if not obj.relatedstudent.period.can_save(user):
            raise PermissionDenied()

    @classmethod
    def is_empty(cls, obj):
        return True

@@ -268,16 +268,110 @@
  models%0A
+from devilry.coreutils.simplified.metabases import SimplifiedAbstractApplicationKeyValueMixin%0A
 %0A%0A@simpl
@@ -552,16 +552,60 @@
 ass Meta
+(SimplifiedAbstractApplicationKeyValueMixin)
 :%0A      
@@ -749,81 +749,102 @@
 ec('
-id', 'application', 'key', 'value', 'relatedstudent', 'student_can_read')
+relatedstudent', 'student_can_read') + SimplifiedAbstractApplicationKeyValueMixin.resultfields
 %0A   
@@ -877,39 +877,8 @@
 pec(
-'application', 'key', 'value', 
 'rel
@@ -910,209 +910,221 @@
 me')
-%0A        editablefields = ('application', 'key', 'value', 'relatedstudent', 'student_can_read')%0A        filters = FilterSpecs(FilterSpec('id', supported_comp=('exact',)),%0A                              
+ + SimplifiedAbstractApplicationKeyValueMixin.searchfields%0A        editablefields = ('relatedstudent', 'student_can_read') + SimplifiedAbstractApplicationKeyValueMixin.editablefields%0A        filters = FilterSpecs(
 Filt
@@ -1212,92 +1212,8 @@
 r),%0A
-                              FilterSpec('application', supported_comp=('exact',)),%0A
     
@@ -1395,16 +1395,69 @@
 act',)))
+ + SimplifiedAbstractApplicationKeyValueMixin.filters
 %0A%0A    @c

Use meta superclass.