Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / db / fields.py @ 435bb7fb

History | View | Annotate | Download (2.8 kB)

1 54975d37 Christos Stavrakakis
# Copyright 2013 GRNET S.A. All rights reserved.
2 54975d37 Christos Stavrakakis
#
3 54975d37 Christos Stavrakakis
# Redistribution and use in source and binary forms, with or without
4 54975d37 Christos Stavrakakis
# modification, are permitted provided that the following conditions
5 54975d37 Christos Stavrakakis
# are met:
6 54975d37 Christos Stavrakakis
#
7 54975d37 Christos Stavrakakis
#   1. Redistributions of source code must retain the above copyright
8 54975d37 Christos Stavrakakis
#      notice, this list of conditions and the following disclaimer.
9 54975d37 Christos Stavrakakis
#
10 54975d37 Christos Stavrakakis
#  2. Redistributions in binary form must reproduce the above copyright
11 54975d37 Christos Stavrakakis
#     notice, this list of conditions and the following disclaimer in the
12 54975d37 Christos Stavrakakis
#     documentation and/or other materials provided with the distribution.
13 54975d37 Christos Stavrakakis
#
14 54975d37 Christos Stavrakakis
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 54975d37 Christos Stavrakakis
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 54975d37 Christos Stavrakakis
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 54975d37 Christos Stavrakakis
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 54975d37 Christos Stavrakakis
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 54975d37 Christos Stavrakakis
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 54975d37 Christos Stavrakakis
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 54975d37 Christos Stavrakakis
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 54975d37 Christos Stavrakakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 54975d37 Christos Stavrakakis
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 54975d37 Christos Stavrakakis
# SUCH DAMAGE.
25 54975d37 Christos Stavrakakis
#
26 54975d37 Christos Stavrakakis
# The views and conclusions contained in the software and documentation are
27 54975d37 Christos Stavrakakis
# those of the authors and should not be interpreted as representing official
28 54975d37 Christos Stavrakakis
# policies, either expressed or implied, of GRNET S.A.
29 54975d37 Christos Stavrakakis
30 54975d37 Christos Stavrakakis
from django.db import models
31 54975d37 Christos Stavrakakis
from south.modelsinspector import add_introspection_rules
32 54975d37 Christos Stavrakakis
33 54975d37 Christos Stavrakakis
34 54975d37 Christos Stavrakakis
class SeparatedValuesField(models.TextField):
35 54975d37 Christos Stavrakakis
    description = ("Stores list of values as a TextField,"
36 54975d37 Christos Stavrakakis
                   " separated by a delimiter.")
37 54975d37 Christos Stavrakakis
    __metaclass__ = models.SubfieldBase
38 54975d37 Christos Stavrakakis
39 54975d37 Christos Stavrakakis
    def __init__(self, *args, **kwargs):
40 54975d37 Christos Stavrakakis
        self.delimiter = kwargs.pop('delimiter', ',')
41 54975d37 Christos Stavrakakis
        super(SeparatedValuesField, self).__init__(*args, **kwargs)
42 54975d37 Christos Stavrakakis
43 54975d37 Christos Stavrakakis
    def to_python(self, value):
44 54975d37 Christos Stavrakakis
        if not value:
45 0e02a709 Christos Stavrakakis
            return []
46 54975d37 Christos Stavrakakis
        if isinstance(value, list):
47 54975d37 Christos Stavrakakis
            return value
48 54975d37 Christos Stavrakakis
        return value.split(self.delimiter)
49 54975d37 Christos Stavrakakis
50 c3362463 Christos Stavrakakis
    def get_prep_value(self, value):
51 54975d37 Christos Stavrakakis
        if not value:
52 54975d37 Christos Stavrakakis
            return
53 54975d37 Christos Stavrakakis
        assert(isinstance(value, list) or isinstance(value, tuple))
54 54975d37 Christos Stavrakakis
        return self.delimiter.join([unicode(s) for s in value])
55 54975d37 Christos Stavrakakis
56 54975d37 Christos Stavrakakis
    def value_to_string(self, obj):
57 54975d37 Christos Stavrakakis
        value = self._get_val_from_obj(obj)
58 c3362463 Christos Stavrakakis
        return self.get_prep_value(value)
59 54975d37 Christos Stavrakakis
60 54975d37 Christos Stavrakakis
61 54975d37 Christos Stavrakakis
add_introspection_rules([
62 54975d37 Christos Stavrakakis
    (
63 54975d37 Christos Stavrakakis
        [SeparatedValuesField],  # Class(es) these apply to
64 54975d37 Christos Stavrakakis
        [],         # Positional arguments (not used)
65 54975d37 Christos Stavrakakis
        {           # Keyword argument
66 54975d37 Christos Stavrakakis
            "delimiter": ["delimiter", {"default": ","}],
67 54975d37 Christos Stavrakakis
        },
68 54975d37 Christos Stavrakakis
    ),
69 54975d37 Christos Stavrakakis
], ["^synnefo\.db\.fields\.SeparatedValuesField"])