##// END OF EJS Templates
Small progress in getting a sync data from server
Small progress in getting a sync data from server

File last commit:

r841:c295c39c decentral
r1138:7246d9ec decentral
Show More
signature.py
108 lines | 3.1 KiB | text/x-python | PythonLexer
import xml.etree.ElementTree as et
from django.db import models
TAG_MODEL = 'model'
TAG_REQUEST = 'request'
TAG_ID = 'id'
TYPE_GET = 'get'
ATTR_VERSION = 'version'
ATTR_TYPE = 'type'
ATTR_NAME = 'name'
ATTR_KEY = 'key'
ATTR_KEY_TYPE = 'type'
ATTR_LOCAL_ID = 'local-id'
class GlobalIdManager(models.Manager):
def generate_request_get(self, global_id_list: list):
"""
Form a get request from a list of ModelId objects.
"""
request = et.Element(TAG_REQUEST)
request.set(ATTR_TYPE, TYPE_GET)
request.set(ATTR_VERSION, '1.0')
model = et.SubElement(request, TAG_MODEL)
model.set(ATTR_VERSION, '1.0')
model.set(ATTR_NAME, 'post')
for global_id in global_id_list:
tag_id = et.SubElement(model, TAG_ID)
global_id.to_xml_element(tag_id)
return et.tostring(request, 'unicode')
class GlobalId(models.Model):
class Meta:
app_label = 'boards'
objects = GlobalIdManager()
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()
def __str__(self):
return '%s::%s::%d' % (self.key_type, self.key, self.local_id)
def to_xml_element(self, element: et.Element):
"""
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))
@staticmethod
def from_xml_element(element: et.Element, existing=False):
"""
Parses XML id tag and gets global id from it.
Arguments:
element -- the XML 'id' element
existing -- if this is False, a new instance of GlobalId will be
created. Otherwise, we will search for an existing GlobalId instance
and throw DoesNotExist if there isn't one.
"""
if existing:
return GlobalId.objects.get(key=element.get(ATTR_KEY),
key_type=element.get(ATTR_KEY_TYPE),
local_id=int(element.get(
ATTR_LOCAL_ID)))
else:
return GlobalId(key=element.get(ATTR_KEY),
key_type=element.get(ATTR_KEY_TYPE),
local_id=int(element.get(ATTR_LOCAL_ID)))
class Signature(models.Model):
class Meta:
app_label = 'boards'
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']
key_type = models.TextField()
key = models.TextField()
signature = models.TextField()