I just ran into this today, and thought someone else might be able to use 
it.

I have need of an 'auto-increment' metadata column - When adding a new 
document, if the field is blank, it should grab the next available number 
and use that.

I implemented this using a Parser:

1. Edit apps/metadata/models.py and edit class MetadataType. Replace:

        if self.parser:
>             parser = import_string(self.parser)()
>             value = parser.parse(value)
>

with:

>         if self.parser:
>             parser = import_string(self.parser)()
>             parser.metadata_field = self #Rob - pass parent to parser
>             value = parser.parse(value) 
>

Now, the metadata instance is available within the parser class.

2. Edit apps/metadata/parsers.py and add metadata_field = None after the 
class header:

> class MetadataParser(object):
>     metadata_field = None 
>

3. Add your parser code to the same file, after your other parsers:

class AutoIncrementID(MetadataParser):
>     def execute(self, input_data):
>         if input_data != '':
>             return str(int(input_data))
>         
>         from .models import DocumentMetadata
>         from django.db.models import Max
>         return 
> str(int(DocumentMetadata.objects.filter(metadata_type=self.metadata_field).aggregate(Max('value'))['value__max'])+1)
>  
>
>

4. Register the new parser(bottom of the file, with the other similar 
lines):

> MetadataParser.register(AutoIncrementID)
>

Now, you can create a metadata type, assign the new AutoIncrementID 
parser(I suggest creating an Integer Validator for checking this too).

When you add/edit a Docunment with this metadata type, if you manually 
enter a value it will accept it. If you leave the field blank, it will 
force the next available number on it.

Hope this helps someone!

-Rob

-- 

--- 
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