forked from ietf-tools/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
57 lines (43 loc) · 2.1 KB
/
Copy pathmodels.py
File metadata and controls
57 lines (43 loc) · 2.1 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Copyright The IETF Trust 2015-2026, All Rights Reserved
import itertools
from django.db import models
class DirtyBits(models.Model):
"""A weak semaphore mechanism for coordination with celery beat tasks
Web workers will set the "dirty_time" value for a given dirtybit slug.
Celery workers will do work if "processed_time" < "dirty_time" and update
"processed_time".
"""
class Slugs(models.TextChoices):
RFCINDEX = "rfcindex", "RFC Index"
ERRATA = "errata", "Errata Tags"
# next line can become `...choices=Slugs)` when we get to Django 5.x
slug = models.CharField(max_length=40, blank=False, choices=Slugs.choices, unique=True)
dirty_time = models.DateTimeField(null=True, blank=True)
processed_time = models.DateTimeField(null=True, blank=True)
class Meta:
verbose_name_plural = "dirty bits"
class DumpInfo(models.Model):
date = models.DateTimeField()
host = models.CharField(max_length=128)
tz = models.CharField(max_length=32, default='UTC')
class ForeignKey(models.ForeignKey):
"A local ForeignKey proxy which provides the on_delete value required under Django 2.0."
def __init__(self, to, on_delete=models.CASCADE, **kwargs):
return super(ForeignKey, self).__init__(to, on_delete=on_delete, **kwargs)
class OneToOneField(models.OneToOneField):
"A local OneToOneField proxy which provides the on_delete value required under Django 2.0."
def __init__(self, to, on_delete=models.CASCADE, **kwargs):
return super(OneToOneField, self).__init__(to, on_delete=on_delete, **kwargs)
def object_to_dict(instance):
"""
Similar to django.forms.models.model_to_dict() but more comprehensive.
Taken from https://stackoverflow.com/questions/21925671/#answer-29088221
with a minor tweak: .id --> .pk
"""
opts = instance._meta
data = {}
for f in itertools.chain(opts.concrete_fields, opts.private_fields):
data[f.name] = f.value_from_object(instance)
for f in opts.many_to_many:
data[f.name] = [i.pk for i in f.value_from_object(instance)]
return data