File: models.py

package info (click to toggle)
python-django-cache-machine 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 228 kB
  • sloc: python: 1,143; makefile: 74
file content (42 lines) | stat: -rw-r--r-- 1,120 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from unittest import mock

import django
from django.db import models

from caching.base import CachingManager, CachingMixin, cached_method

# This global call counter will be shared among all instances of an Addon.
call_counter = mock.Mock()


class User(CachingMixin, models.Model):
    name = models.CharField(max_length=30)

    objects = CachingManager()

    if django.VERSION[0] >= 2:

        class Meta:
            # Tell Django to use this manager when resolving foreign keys.
            # (Django >= 2.0)
            base_manager_name = "objects"


class Addon(CachingMixin, models.Model):
    val = models.IntegerField()
    author1 = models.ForeignKey(User, on_delete=models.CASCADE)
    author2 = models.ForeignKey(
        User, related_name="author2_set", on_delete=models.CASCADE
    )

    objects = CachingManager()

    class Meta:
        # without this, Postgres & SQLite return objects in different orders:
        ordering = ("pk",)

    @cached_method
    def calls(self, arg=1):
        """This is a docstring for calls()"""
        call_counter()
        return arg, call_counter.call_count