from django.conf import settings from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType from django.db import models from django.utils.translation import gettext_lazy as _ class Assignment(models.Model): user = models.ForeignKey("auth.User", on_delete=models.PROTECT) subject_ct = models.ForeignKey(ContentType, on_delete=models.PROTECT) subject_id = models.PositiveIntegerField() subject = GenericForeignKey('subject_ct', 'subject_id') class Meta: unique_together = [ ("subject_ct", "subject_id"), ] class NoteQuerySet(models.QuerySet): def for_user(self, user): notes = ( self.filter(author=user).annotate(owner_label=models.Value(_('własne'), output_field=models.CharField())) ) if user.has_perm('user.view_all_notes'): notes |= ( self.exclude(author=user).annotate(owner_label=models.F('author__username')) ) else: notes |= ( self.exclude(author=user).filter(author__groups__name=settings.SUPER_LEXICOGRAPHS_GROUP_NAME) .annotate(owner_label=models.Value(_('Super'), output_field=models.CharField())) ) return notes class Note(models.Model): author = models.ForeignKey("auth.User", on_delete=models.PROTECT) subject_ct = models.ForeignKey(ContentType, on_delete=models.PROTECT) subject_id = models.PositiveIntegerField() subject = GenericForeignKey('subject_ct', 'subject_id') note = models.TextField() created_at = models.DateTimeField(auto_now_add=True) objects = models.Manager.from_queryset(NoteQuerySet)()