~stevanr/linaro-ci-dashboard/build_results_xml

« back to all changes in this revision

Viewing changes to dashboard/frontend/models/textfield_loop.py

  • Committer: Milo Casagrande
  • Date: 2012-09-04 14:53:44 UTC
  • mfrom: (34.2.7 textfield_complete)
  • Revision ID: milo@ubuntu.com-20120904145344-1iz8okpc3b5r0dke
Merged textfield loop branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from frontend.models.loop import Loop
20
20
 
21
21
 
 
22
# Default delimiter to separate values from keys in the text field.
 
23
DEFAULT_DELIMITER = '='
 
24
 
 
25
 
22
26
class TextFieldLoop(Loop):
 
27
    """
 
28
    This is the base class model for the loops that want to be a free-form loop
 
29
    with just a text field to insert values.
 
30
    A loop of this kind is not much useful, it needs to be chained with another
 
31
    one.
 
32
 
 
33
    This model in particular is an abstract model, it is necessary to inherits
 
34
    from it to be used.
 
35
    """
23
36
    class Meta:
24
37
        app_label = 'frontend'
 
38
        abstract = True
25
39
 
26
40
    values = models.TextField()
27
41
 
28
42
    def schedule_build(self, parameters=None):
29
43
        return None
30
44
 
31
 
    def parse_values(self):
32
 
        pass
 
45
    def values_to_dict(self):
 
46
        """
 
47
        Returns a dictionary representation of the values inserted. The
 
48
        key<>value pairs are split on DEFAULT_DELIMITER.
 
49
 
 
50
        :return A dictionary of the values inserted.
 
51
        """
 
52
        text_to_dict = {}
 
53
        valid, lines = self.valid_values(self.values)
 
54
 
 
55
        if valid:
 
56
            for line in lines:
 
57
                key, value = line.split(DEFAULT_DELIMITER)
 
58
                text_to_dict[key.lower()] = value
 
59
 
 
60
        return text_to_dict
 
61
 
 
62
    @staticmethod
 
63
    def valid_values(values):
 
64
        """
 
65
        Checks if the values inserted by the user are well formed, meaning that
 
66
        each key<>value pair is using the correct delimiter.
 
67
 
 
68
        :param values: the string with all the key<>value pairs, separated with
 
69
        a newline character.
 
70
        :type str
 
71
        :return a boolean for the validity, and the list of lines.
 
72
        """
 
73
        valid = True
 
74
        lines = values.splitlines()
 
75
        for line in lines:
 
76
            if DEFAULT_DELIMITER in line:
 
77
                continue
 
78
            else:
 
79
                valid = False
 
80
                break
 
81
        return valid, lines