from django.db import models from oauth2_provider.models import AbstractApplication from oauth2_provider import models as oauth2_models from django.utils.translation import gettext_lazy as _ # Create your models here. class ApplicationQuerySet(models.QuerySet): """Application queryset""" def get_by_natural_key(self, client_id): return self.get(client_id=client_id) def by_source(self, source: int): """Filter by source parameter""" return self.filter(source=source) class ApplicationManager(oauth2_models.ApplicationManager): """Application manager""" class Application(AbstractApplication): """Custom oauth2 application model""" MOBILE = 0 WEB = 1 SOURCES = ( (MOBILE, _('Mobile')), (WEB, _('Web')), ) source = models.PositiveSmallIntegerField(choices=SOURCES, default=MOBILE, verbose_name=_('Source')) objects = ApplicationManager.from_queryset(ApplicationQuerySet)() class Meta(AbstractApplication.Meta): swappable = "OAUTH2_PROVIDER_APPLICATION_MODEL" def natural_key(self): return (self.client_id,)