2012-07-07 11:14:04 +02:00
|
|
|
from django.db import models
|
|
|
|
|
2012-11-24 14:01:21 +01:00
|
|
|
|
2012-07-07 11:14:04 +02:00
|
|
|
class MinMaxIntegerField(models.IntegerField):
|
2013-09-25 12:53:44 +02:00
|
|
|
"""
|
|
|
|
IntegerField with options to set a min- and a max-value.
|
|
|
|
"""
|
|
|
|
|
2012-07-07 11:14:04 +02:00
|
|
|
def __init__(self, min_value=None, max_value=None, *args, **kwargs):
|
|
|
|
self.min_value, self.max_value = min_value, max_value
|
|
|
|
super(MinMaxIntegerField, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def formfield(self, **kwargs):
|
2012-11-24 14:01:21 +01:00
|
|
|
defaults = {'min_value': self.min_value, 'max_value': self.max_value}
|
2012-07-07 11:14:04 +02:00
|
|
|
defaults.update(kwargs)
|
|
|
|
return super(MinMaxIntegerField, self).formfield(**defaults)
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RESTModelMixin:
|
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
Mixin for Django models which are used in our REST API.
|
2015-06-29 12:08:15 +02:00
|
|
|
"""
|
|
|
|
|
2016-02-11 11:29:19 +01:00
|
|
|
access_permissions = None
|
|
|
|
|
2015-06-29 12:08:15 +02:00
|
|
|
def get_root_rest_element(self):
|
|
|
|
"""
|
|
|
|
Returns the root rest instance.
|
|
|
|
|
|
|
|
Uses self as default.
|
|
|
|
"""
|
|
|
|
return self
|
|
|
|
|
2016-09-17 22:26:23 +02:00
|
|
|
@classmethod
|
|
|
|
def get_access_permissions(cls):
|
2016-02-11 22:58:32 +01:00
|
|
|
"""
|
|
|
|
Returns a container to handle access permissions for this model and
|
|
|
|
its corresponding viewset.
|
|
|
|
"""
|
2016-09-17 22:26:23 +02:00
|
|
|
return cls.access_permissions
|
2016-02-11 22:58:32 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_collection_string(cls):
|
2015-06-29 12:08:15 +02:00
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
Returns the string representing the name of the collection. Returns
|
|
|
|
None if this is not a so called root rest instance.
|
2015-06-29 12:08:15 +02:00
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
# TODO Check if this is a root rest element class and return None if not.
|
|
|
|
return '/'.join((cls._meta.app_label.lower(), cls._meta.object_name.lower()))
|
2016-01-03 15:33:51 +01:00
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
def get_rest_pk(self):
|
2016-01-03 15:33:51 +01:00
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
Returns the primary key used in the REST API. By default this is
|
|
|
|
the database pk.
|
2016-01-03 15:33:51 +01:00
|
|
|
"""
|
2016-02-11 22:58:32 +01:00
|
|
|
return self.pk
|