~linaro-automation/linaro-ci-dashboard/trunk

« back to all changes in this revision

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

  • Committer: Milo Casagrande
  • Date: 2012-09-11 14:58:46 UTC
  • mfrom: (40.1.34 views_refactoring)
  • Revision ID: milo@ubuntu.com-20120911145846-pcqlubkj49mjvrze
Merged views refactoring.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
                               default=None,
32
32
                               null=True,
33
33
                               on_delete=models.SET_NULL)
34
 
    type = models.CharField(max_length=50, editable=False)
 
34
    type = models.CharField(max_length=50, editable=False, blank=True)
35
35
    is_restricted = models.BooleanField(default=False,
36
36
                                        verbose_name='Build is restricted')
37
37
    is_official = models.BooleanField(default=False,
41
41
                                     related_name='previous_loop',
42
42
                                     verbose_name='Next loop in chain')
43
43
 
44
 
    # List of fields to be excluded by default from the base64 encoded config.
45
 
    # If a subclass needs different fields, it is necessary to override this
46
 
    # variable.
47
 
    EXCLUDE_LIST = ['id', 'server', 'loop_ptr', 'next_loop', 'name', 'type']
48
 
 
49
44
    def __init__(self, *args, **kwargs):
50
45
        self.log = Logger.getClassLogger(self)
51
46
        super(Loop, self).__init__(*args, **kwargs)
125
120
        return []
126
121
 
127
122
    def __repr__(self):
128
 
        return "%s(id=%s, ...)" % (self.__class__.__name__, self.id)
 
123
        return "%s(id=%s)" % (self.__class__.__name__, self.id)
129
124
 
130
 
    def base64_config(self):
 
125
    def base64_config(self,
 
126
                      include=None,
 
127
                      exclude=None,
 
128
                      capitalize=False,
 
129
                      upper=False):
131
130
        """ Return loop configuration as base64 encoded string.
 
131
 
 
132
        :param include: the list of fields, as string, to get.
 
133
        :type include list
 
134
        :param exclude: the list of fields, as string, to exclude.
 
135
        :type exclude list
 
136
        :param capitalize: whether the field name should be capitalized (the
 
137
        first letter should be a capital one).
 
138
        :type capitalize bool
 
139
        :param upper: whether the field name should be all capital letter.
 
140
        :type upper bool
 
141
        :return A base64 encoded string composed of 'key=value'
132
142
        """
133
143
        text_config = u''
134
 
        for field in self._meta.fields:
135
 
            if not field.name in self.EXCLUDE_LIST:
136
 
                text_config += u'%s=%s\n' % (field.name.upper(),
137
 
                                             field.value_to_string(self))
 
144
        for field in self._get_wanted_fields(include=include,
 
145
                                             exclude=exclude,
 
146
                                             capitalize=capitalize,
 
147
                                             upper=upper):
 
148
            text_config += u'%s=%s\n' % (field[0], str(field[1]))
138
149
        return base64.b64encode(text_config)
139
150
 
140
151
    def get_child_object(self):
150
161
            result.extend(Loop.objects.filter(type=loop_type))
151
162
        return result
152
163
 
 
164
    def _get_wanted_fields(self,
 
165
                           include=None,
 
166
                           exclude=None,
 
167
                           verbose_name=False,
 
168
                           capitalize=False,
 
169
                           upper=False):
 
170
        """
 
171
        Gets the wanted fields, as a list of tuples with
 
172
        (field.name, field.value). By default retrieves all the fields
 
173
        associated with the loop instance.
 
174
 
 
175
        There is some control over the results: it is possible to retrieve only
 
176
        a subset of the fields, to retrieve the verbose name associated with
 
177
        the fields, and either to capitalize or make all capital letters
 
178
        the name.
 
179
 
 
180
        The parameters used to retrieve a subset of fields are 'include' and
 
181
        'exclude'. If both are used, 'exclude' has precedence, meaning that if
 
182
        a field is specified both in 'exclude' and in 'include', that result
 
183
        will not include that field.
 
184
 
 
185
        :param include: the list of fields, as string, to get.
 
186
        :type include list
 
187
        :param exclude: the list of fields, as string, to exclude.
 
188
        :type exclude list
 
189
        :param verbose_name: whether to retrieve the verbose name associated
 
190
        with the field. Default False.
 
191
        :type verbose_name bool
 
192
        :param capitalize: whether the field name should be capitalized (the
 
193
        first letter should be a capital one).
 
194
        :type capitalize bool
 
195
        :param upper: whether the field name should be all capital letter.
 
196
        :type upper bool
 
197
        :return a list of tuples, in which each tuple consists of
 
198
        (field.name, field.value).
 
199
        """
 
200
        wanted_fields = []
 
201
        for field in self._meta.fields:
 
202
            # First check if field is excluded, in case both 'include'
 
203
            # and 'exclude' are passed. Exclude wins.
 
204
            if exclude and field.name in exclude:
 
205
                continue
 
206
            if include and not field.name in include:
 
207
                continue
 
208
 
 
209
            # Consider OneToOneField, since we do not have the value stored,
 
210
            # we need to retrieve it.
 
211
            value = None
 
212
            if isinstance(field, models.related.OneToOneField):
 
213
                pk = field.value_from_object(self)
 
214
                if pk:
 
215
                    value = Loop.objects.get(pk=pk).name
 
216
            else:
 
217
                value = field._get_val_from_obj(self)
 
218
 
 
219
            if verbose_name:
 
220
                name = field.verbose_name
 
221
            else:
 
222
                name = field.name
 
223
            if capitalize:
 
224
                name = name.capitalize()
 
225
            if upper:
 
226
                name = name.upper()
 
227
 
 
228
            wanted_fields.append((name, value))
 
229
        return wanted_fields
 
230
 
 
231
    def get_details(self):
 
232
        """
 
233
        Retrieves the necessary fields for the detail view.
 
234
        This is mostly and probably only needed in the detail HTML template.
 
235
 
 
236
        The fields retrieved here are not all the fields in the model.
 
237
 
 
238
        :return A list of tuples.
 
239
        """
 
240
        excluded_fields = ['name', 'server', 'type', 'id', 'loop_ptr']
 
241
        return self._get_wanted_fields(exclude=excluded_fields,
 
242
                                       verbose_name=True,
 
243
                                       capitalize=True)
 
244
 
153
245
    def continue_down_the_chain(self, configuration=None):
154
246
        """Invokes the build of the next loop in chain.
155
247