signature.py
125 lines
| 3.6 KiB
| text/x-python
|
PythonLexer
|
r827 | import xml.etree.ElementTree as et | ||
|
r1834 | |||
|
r820 | from django.db import models | ||
|
r1834 | |||
|
r1560 | from boards.models import KeyPair | ||
|
r820 | |||
|
r841 | TAG_MODEL = 'model' | ||
TAG_REQUEST = 'request' | ||||
TAG_ID = 'id' | ||||
TYPE_GET = 'get' | ||||
|
r1566 | TYPE_LIST = 'list' | ||
|
r841 | |||
ATTR_VERSION = 'version' | ||||
ATTR_TYPE = 'type' | ||||
ATTR_NAME = 'name' | ||||
|
r827 | ATTR_KEY = 'key' | ||
ATTR_KEY_TYPE = 'type' | ||||
ATTR_LOCAL_ID = 'local-id' | ||||
|
r841 | class GlobalIdManager(models.Manager): | ||
|
r1232 | def global_id_exists(self, global_id): | ||
""" | ||||
Checks if the same global id already exists in the system. | ||||
""" | ||||
return self.filter(key=global_id.key, | ||||
key_type=global_id.key_type, | ||||
local_id=global_id.local_id).exists() | ||||
|
r841 | |||
|
r820 | class GlobalId(models.Model): | ||
|
r1520 | """ | ||
Global model ID and cache. | ||||
Key, key type and local ID make a single global identificator of the model. | ||||
Content is an XML cache of the model that can be passed along between nodes | ||||
without manual serialization each time. | ||||
""" | ||||
|
r820 | class Meta: | ||
app_label = 'boards' | ||||
|
r841 | objects = GlobalIdManager() | ||
|
r820 | def __init__(self, *args, **kwargs): | ||
models.Model.__init__(self, *args, **kwargs) | ||||
if 'key' in kwargs and 'key_type' in kwargs and 'local_id' in kwargs: | ||||
self.key = kwargs['key'] | ||||
self.key_type = kwargs['key_type'] | ||||
self.local_id = kwargs['local_id'] | ||||
key = models.TextField() | ||||
key_type = models.TextField() | ||||
local_id = models.IntegerField() | ||||
|
r1519 | content = models.TextField(blank=True, null=True) | ||
|
r820 | |||
def __str__(self): | ||||
|
r841 | return '%s::%s::%d' % (self.key_type, self.key, self.local_id) | ||
|
r820 | |||
|
r836 | def to_xml_element(self, element: et.Element): | ||
|
r827 | """ | ||
Exports global id to an XML element. | ||||
""" | ||||
element.set(ATTR_KEY, self.key) | ||||
element.set(ATTR_KEY_TYPE, self.key_type) | ||||
element.set(ATTR_LOCAL_ID, str(self.local_id)) | ||||
|
r836 | @staticmethod | ||
|
r1233 | def from_xml_element(element: et.Element): | ||
|
r836 | """ | ||
Parses XML id tag and gets global id from it. | ||||
Arguments: | ||||
element -- the XML 'id' element | ||||
|
r1233 | |||
Returns: | ||||
global_id -- id itself | ||||
exists -- True if the global id was taken from database, False if it | ||||
did not exist and was created. | ||||
|
r836 | """ | ||
|
r1233 | try: | ||
|
r836 | return GlobalId.objects.get(key=element.get(ATTR_KEY), | ||
key_type=element.get(ATTR_KEY_TYPE), | ||||
local_id=int(element.get( | ||||
|
r1233 | ATTR_LOCAL_ID))), True | ||
except GlobalId.DoesNotExist: | ||||
|
r836 | return GlobalId(key=element.get(ATTR_KEY), | ||
key_type=element.get(ATTR_KEY_TYPE), | ||||
|
r1233 | local_id=int(element.get(ATTR_LOCAL_ID))), False | ||
|
r836 | |||
|
r1560 | def is_local(self): | ||
"""Checks fo the ID is local model's""" | ||||
return KeyPair.objects.filter( | ||||
key_type=self.key_type, public_key=self.key).exists() | ||||
|
r1586 | def clear_cache(self): | ||
""" | ||||
Removes content cache and signatures. | ||||
""" | ||||
self.content = None | ||||
self.save() | ||||
self.signature_set.all().delete() | ||||
|
r820 | |||
class Signature(models.Model): | ||||
class Meta: | ||||
app_label = 'boards' | ||||
|
r837 | def __init__(self, *args, **kwargs): | ||
models.Model.__init__(self, *args, **kwargs) | ||||
if 'key' in kwargs and 'key_type' in kwargs and 'signature' in kwargs: | ||||
self.key_type = kwargs['key_type'] | ||||
self.key = kwargs['key'] | ||||
self.signature = kwargs['signature'] | ||||
|
r820 | key_type = models.TextField() | ||
key = models.TextField() | ||||
signature = models.TextField() | ||||
|
r1242 | |||
|
r1986 | global_id = models.ForeignKey('GlobalId', on_delete=models.CASCADE) | ||