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')
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
47
EXCLUDE_LIST = ['id', 'server', 'loop_ptr', 'next_loop', 'name', 'type']
49
44
def __init__(self, *args, **kwargs):
50
45
self.log = Logger.getClassLogger(self)
51
46
super(Loop, self).__init__(*args, **kwargs)
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)
130
def base64_config(self):
125
def base64_config(self,
131
130
""" Return loop configuration as base64 encoded string.
132
:param include: the list of fields, as string, to get.
134
:param exclude: the list of fields, as string, to exclude.
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.
141
:return A base64 encoded string composed of 'key=value'
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,
146
capitalize=capitalize,
148
text_config += u'%s=%s\n' % (field[0], str(field[1]))
138
149
return base64.b64encode(text_config)
140
151
def get_child_object(self):
150
161
result.extend(Loop.objects.filter(type=loop_type))
164
def _get_wanted_fields(self,
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.
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
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.
185
:param include: the list of fields, as string, to get.
187
:param exclude: the list of fields, as string, to exclude.
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.
197
:return a list of tuples, in which each tuple consists of
198
(field.name, field.value).
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:
206
if include and not field.name in include:
209
# Consider OneToOneField, since we do not have the value stored,
210
# we need to retrieve it.
212
if isinstance(field, models.related.OneToOneField):
213
pk = field.value_from_object(self)
215
value = Loop.objects.get(pk=pk).name
217
value = field._get_val_from_obj(self)
220
name = field.verbose_name
224
name = name.capitalize()
228
wanted_fields.append((name, value))
231
def get_details(self):
233
Retrieves the necessary fields for the detail view.
234
This is mostly and probably only needed in the detail HTML template.
236
The fields retrieved here are not all the fields in the model.
238
:return A list of tuples.
240
excluded_fields = ['name', 'server', 'type', 'id', 'loop_ptr']
241
return self._get_wanted_fields(exclude=excluded_fields,
153
245
def continue_down_the_chain(self, configuration=None):
154
246
"""Invokes the build of the next loop in chain.