Sorry about that. The code was just a test using the Python shell. I've 
updated the app code 
(https://gitlab.com/mayan-edms/document_renaming/commit/b09bf4a8dadf381cd8b54cb5f452119db8173a4f)
 
and now sequences can be created automatically with a rename template like 
this:

{% set username = document.target_actions.first().actor.username %}{{ username 
}}-{{ 
"%05d"|format(Sequence.objects.get_or_create(slug=username,label=username)[0].next_value())
 }}



The first part extracts the document event log, gets the first entry and 
stores the actor's username string in a variable called username.
The second part renders the username wih a dash.
The final part tries to the sequence with the username and if it is not 
available creates it. Since get_or_created returns two values (a tuple), 
the actual sequence instance and a boolean if it was newly created, only 
the first value is retained using [0]. get_next() if called to get the 
value and advance the sequence. And everything is send as an argument to 
the 'format' template tag which behaves like Python's .format() and padded 
to 5 zeros.

Here are the tests for the new functionality: 
https://gitlab.com/mayan-edms/document_renaming/blob/master/document_renaming/tests/test_models.py#L104
If no new bugs are discovered release 2 can be done in the following days. 
Hope this helps.



On Tuesday, January 12, 2016 at 2:59:26 PM UTC-4, Rachael Sewell wrote:
>
> Hi Roberto,
>
> I’m still a little confused. Do I add the following to the 
> document_renaming model or the document model?
>
> from jinja2 import Template
> from document_renaming.models import Sequence
>
> document=Document.objects.first()
>
>
> Did you add the following code as the renaming template via the web 
> interface:
>
> Template('{% set user = document.target_actions.first().actor 
> %}{{user}}-{{ Sequence.objects.get(slug=user).next_value() 
> }}').render(document=document, Sequence=Sequence)
>
>
>
> Kind regards,
> Rachael
>
> On Jan 12, 2016, at 1:37 AM, Roberto Rosario <
> [email protected]> wrote:
>
> Did a quick test and with Jinja2 it is possible:
>
> from jinja2 import Template
> from document_renaming.models import Sequence
>
> document=Document.objects.first()
>
> Template('{% set user = document.target_actions.first().actor 
> %}{{user}}-{{ Sequence.objects.get(slug=user).next_value() 
> }}').render(document=document, Sequence=Sequence)
>
>
> outputs:
>
> admin-1
>
> it is even possible to create the user sequences from the template (using 
> get_or_create):
>
> Template('{% set user = document.target_actions.first().actor 
> %}{{user}}-{{ Sequence.objects.get_or_create(slug=user)[0].next_value() 
> }}').render(document=document, Sequence=Sequence)
>
> On Tuesday, January 12, 2016 at 5:30:02 AM UTC-4, Roberto Rosario wrote:
>>
>> The UI, document upload and document renamer execute in different 
>> contexts, it is not possible for them to share the web session. The 
>> alternative would be to access the event log of the document and extract 
>> the first entry which would indicate the user who created the document and 
>> expose this to the context of the renaming template here: 
>> https://gitlab.com/mayan-edms/document_renaming/blob/master/document_renaming/models.py#L80
>>
>> The even log app uses Django activity stream (
>> https://github.com/justquick/django-activity-stream/blob/master/actstream/models.py)
>>  
>> and they provide a reverse reference. Something like 
>>
>> document.target_actions.first().actor 
>>
>> should give your the user instance.
>>
>> The second problem would be to get the user's sequence dynamically. 
>> Sequences are passed to the rename template's context as 'sequence_XXX' 
>> variables because Django's template engine doesn't allow parameter method 
>> calling.
>>
>> In my mind the easiest path would be to ditch Django's template engine 
>> and use something like Jinja2 (http://jinja.pocoo.org/) which allows 
>> method calls with parameters and the renaming template itself can contain 
>> all the required logic:
>>
>> {% set username = document.target_actions.first.actor.username %}  {{ 
>> username }}-{{ Sequence.objects.get(slug=%username%).next_value() }}
>>
>> Provided you have already the sequences created for each use. This is all 
>> from the top of my head without any actual test of course :)
>>
>>
>> On Friday, January 8, 2016 at 4:52:07 PM UTC-4, Rachael Sewell wrote:
>>>
>>> Roberto,
>>>
>>> I'm working with this app currently. I have two document types:
>>>
>>>    1. Specs 
>>>    2. Memos
>>>    
>>> Memos will be renamed according to the logged in user. I would like to 
>>> create a number like this <username>-<incremental number>. For example: 
>>> "rachael.sewell-00"
>>>
>>> I've tried a few things, but it seems the user is not accessible in the 
>>> view. Any pointers to where I should be updating code would be helpful to 
>>> add this. Thanks for the continued help.
>>>
>>> Rachael
>>>
>>> On Friday, December 18, 2015 at 10:34:13 AM UTC-8, Rachael Sewell wrote:
>>>>
>>>> Hello,
>>>>
>>>> I'd like to know if there is already a way to add an automatically 
>>>> created document number to a document. For example I would like to start 
>>>> with a initial document number of AN00001 and increment (AN00002, AN00003) 
>>>> for each additional doc. Does this functionality already exist? If not 
>>>> could it be added to the core or would it need to be a custom application? 
>>>> Any pointers on how to get started on this would be helpful as well. 
>>>>
>>>> Thanks,
>>>> Rachael
>>>>
>>>
> -- 
>
> --- 
> You received this message because you are subscribed to a topic in the 
> Google Groups "Mayan EDMS" group.
> To unsubscribe from this topic, visit 
> https://groups.google.com/d/topic/mayan-edms/HRy_us-jp10/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to 
> [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
>
>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"Mayan EDMS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to