Commit e328cbcd authored by Papa Tamsir Kane's avatar Papa Tamsir Kane

erp5_web_bookmark: Auto generate reference

parent 1f80db2b
reference = context.getReference()
if reference:
return reference
def encode(n, alphabet, mapping, mask, min_length):
n_and_mask = n & mask
result = 0
for i, b in enumerate(mapping):
if n_and_mask & (1 << i):
result |= (1 << b)
x = (n & ~mask) | result
# convert x to adequate base
x_base = to_base(x, alphabet)
padding = alphabet[0] * (min_length - len(x_base))
return '{}{}'.format(padding, x_base)
def to_base(number, alphabet):
base = len(alphabet)
number_base = ''
while number > base:
quotient, remainder = divmod(number, base)
number_base = alphabet[remainder] + number_base
number = quotient
number_base = alphabet[number] + number_base
return number_base
'''
class MyException(Exception):
pass
'''
url_id = int(context.getId())
config = context.Url_getReferenceGeneratorConfig()
reference = encode(url_id, config['ALPHABET'], config['MAPPING'],
config['MASK'], config['MIN_LENGTH'])
context.setReference(reference)
return reference
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>Script_magic</string> </key>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>**kw</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>URL_getReference</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
def from_base(representation, alphabet):
base = len(alphabet)
weights = enumerate(reversed(representation))
return sum((alphabet.index(c) * (base ** i) for i, c in weights))
def decode(representation, alphabet, mapping, mask):
number = from_base(representation, alphabet)
result = 0
n_and_mask = number & mask
for i, b in enumerate(mapping):
if n_and_mask & (1 << b):
result |= (1 << i)
x = (number & ~mask) | result
return x
config = context.Url_getReferenceGeneratorConfig()
decoded = decode(representation, config['ALPHABET'],
config['MAPPING'], config['MASK'])
return decoded
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>Script_magic</string> </key>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>representation, **kw</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Url_decode</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
config = {}
config['ALPHABET'] = 'P1DjUqRi2YVLr5Ke8gZwNTctlodkMSJOmzusQaxCnE43WyG7Bp9bFH06AhIfvX'
BLOCK_SIZE = 24
config['BLOCK_SIZE'] = BLOCK_SIZE
config['MASK'] = (1 << BLOCK_SIZE) - 1
config['MAPPING'] = range(BLOCK_SIZE - 1, -1, -1)
config['MIN_LENGTH'] = 5
return config
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>Script_magic</string> </key>
<value> <int>3</int> </value>
</item>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
</klass>
<tuple/>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>**kw</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Url_getReferenceGeneratorConfig</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
curation_request_lines_list = context.getAggregateRelatedValueList() curation_request_lines_list = context.getAggregateRelatedValueList(portal_type='Curation Request Line')
return curation_request_lines_list return curation_request_lines_list
...@@ -88,13 +88,13 @@ ...@@ -88,13 +88,13 @@
<key> <string>left</string> </key> <key> <string>left</string> </key>
<value> <value>
<list> <list>
<string>my_reference</string>
<string>my_title</string> <string>my_title</string>
<string>my_coordinate_text</string> <string>my_coordinate_text</string>
<string>your_modification_date</string> <string>your_modification_date</string>
<string>my_translated_validation_state_title</string> <string>my_translated_validation_state_title</string>
<string>my_effective_date</string> <string>my_effective_date</string>
<string>my_language</string> <string>my_language</string>
<string>my_reference</string>
</list> </list>
</value> </value>
</item> </item>
......
...@@ -10,7 +10,8 @@ ...@@ -10,7 +10,8 @@
<key> <string>delegated_list</string> </key> <key> <string>delegated_list</string> </key>
<value> <value>
<list> <list>
<string>title</string> <string>default</string>
<string>editable</string>
</list> </list>
</value> </value>
</item> </item>
...@@ -48,6 +49,16 @@ ...@@ -48,6 +49,16 @@
<key> <string>tales</string> </key> <key> <string>tales</string> </key>
<value> <value>
<dictionary> <dictionary>
<item>
<key> <string>default</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item>
<key> <string>editable</string> </key>
<value> <string></string> </value>
</item>
<item> <item>
<key> <string>field_id</string> </key> <key> <string>field_id</string> </key>
<value> <string></string> </value> <value> <string></string> </value>
...@@ -63,22 +74,39 @@ ...@@ -63,22 +74,39 @@
<key> <string>values</string> </key> <key> <string>values</string> </key>
<value> <value>
<dictionary> <dictionary>
<item>
<key> <string>default</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>editable</string> </key>
<value> <int>0</int> </value>
</item>
<item> <item>
<key> <string>field_id</string> </key> <key> <string>field_id</string> </key>
<value> <string>my_string_field</string> </value> <value> <string>my_view_mode_reference</string> </value>
</item> </item>
<item> <item>
<key> <string>form_id</string> </key> <key> <string>form_id</string> </key>
<value> <string>Base_viewFieldLibrary</string> </value> <value> <string>Base_viewFieldLibrary</string> </value>
</item> </item>
<item>
<key> <string>title</string> </key>
<value> <string>Reference</string> </value>
</item>
</dictionary> </dictionary>
</value> </value>
</item> </item>
</dictionary> </dictionary>
</pickle> </pickle>
</record> </record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="TALESMethod" module="Products.Formulator.TALESField"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_text</string> </key>
<value> <string>python: context.URL_getReference()</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData> </ZopeData>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment