Usage¶
⚠️ Because iControlServer is built side by side of iControlKit frameworks please check the iControlKit Swift or iControlKit C# documentation.
Work with Models¶
A model is the single, definitive source of information about your data
It contains the essential fields and behaviors of the data you’re storing.
# 'Sources/models.py'
from Digitalblend.iControl import models
class Level(models.NonSynchronizableModel):
name = models.django.CharField(max_length=150, verbose_name="Name")
videos = models.django.ManyToManyField('Video', blank=True)
class Video(models.SynchronizableModel):
file = models.django.FileField(upload_to=utils.File.DestinationFor('file'), validators=[utils.Validator.Video, ])
levels = models.django.ManyToManyField('Level', through=Level.videos.through, blank=True)
All models need to be at least a subclass of
models.Modelabstract class.For a back-end model use
models.NonSynchronizableModel.For a iControlKit model use
models.SynchronizableModel, needed fields for synchronisation are automatically created.An ‘id’ field - (
PrimaryKey, unique identifier)An ‘created’ field - (
Date, store the item creation date)An ‘updated’ field - (
Date, store the item date of last modification)A ‘name’ field - (
String, an unique name identifier)
Available model fields are in models.django : References
Work with Profiles¶
`iControlServer establish a standardised user model for clients. To enhance the given model you’ll use profiles.``
A Profile is a link between an user and a model storing additional data.
# 'Sources/admin.py'
from Digitalblend.iControl import profiles
from Sources import models
profiles.Profile.register(models.Level)
# you can now access the linked profile :
user.levelprofile
# and access the data stored in the Level model :
user.levelprofile.level
# or user the shortcut :
user.level
Work with Serializer¶
Serializer provides serialization and deserialization functionalities for Model
# 'Sources/serializers.py'
from Digitalblend.iControl import serializers
from Sources import models
class LevelSerializer(serializers.Serializer):
model = models.Level
fields = ('name', )
class VideoSerializer(serializers.Serializer):
model = models.Video
fields = ('file', )
All serializers need to be a sublcass of
Serializerclass.The fields property is a list of fields that will be serialized or deserialized by this class.
‘id’, ‘created’, ‘updated’ and ‘name’ model’s fields are automatically added
Work with the Admin¶
# 'Source/admin.py'
from Digitalblend.iControl import admins, profiles
from Sources import models
class LevelAdmin(admins.Admin):
list_display = ('name', )
filter_horizontal = ('videos', )
class VideoAdmin(admins.Admin):
list_display = ('name', 'file', )
filter_horizontal = ('levels', )
admins.Admin.register(models.Level, LevelAdmin)
admins.Admin.register(models.Video, VideoAdmin)
All admin need to be a subclass of
Adminclass.You can inner a given model into a subclass of the
Inlineclass and add it through theinlinesproperty of theAdminclass. ReferenceYou need to register your class with a model through
admins.Admin.registermethod.Use filter_horizontal for good display of
ManyToManyField
Work with the Api¶
# 'Source/api.py'
from Digitalblend.iControl import apis
from Sources import models, serializers
class LevelRoute(apis.Route):
permission_classes = (apis.Permission.Defaults.IsAuthenticated)
serializer_class = serializers.LevelSerializer
class VideoRoute(apis.Route):
class Permission(apis.Permission):
def has_object_permission(self, request, view, obj):
if request.user.levelprofile.level in obj.levels.all():
return True
else:
return False
permission_classes = (apis.Permission.Defaults.IsAuthenticated, Permission)
serializer_class = serializers.VideoSerializer
@apis.Route.endpoint
def file(self, request, pk=None):
return self.sendFile(self.get_object().file)
apis.Api.register(models.Level, LevelRoute)
apis.Api.register(models.Video, VideoRoute)
All api route need to be a subclass of
apis.Routeclass.You need to register your class with a model through
apis.Api.registermethod.
Serving file (from FileField or ImageField) need a route endpoint method in your class.
...
@apis.Route.endpoint
def file(self, request, pk=None):
return self.sendFile(self.get_object().file)
You can specify to a route a set of permissions. There is some default permission classes in apis.Permission.Defaults
AllowAny
IsAuthenticated
IsAdminUser
...
permission_classes = (apis.Permission.Defaults.IsAuthenticated, )
Or create a customized per object level permission: overriding apis.Permission class.
...
class CustomPermission(apis.Permission):
def has_object_permission(self, request, view, obj):
if request.user.levelprofile.level in obj.levels.all():
return True
else:
return False
permission_classes = (CustomPermission, )
Work with the provided Utils¶
utils.Util.File provides functions related to files.
DestinationForfunction automatically create a destination path for uploaded file through the admin site.
...
file = models.django.FileField(upload_to=utils.File.DestinationFor('file'),
utils.Util.Validator provides functions related to files validationself.
VideoImagePdfScenefunctions are used to validate file type while uploading file through the admin site.
...
file = models.django.FileField(upload_to=..., validators=[utils.Validator.Video, ])