def get(self, *args, **kwargs): if self.check_perm(): return self.check_perm() self.initialize_vars() try: ci_id = self.get_ci_id() except: # editing/viewing Ci which doesn's exists. return HttpResponseRedirect('/cmdb/ci/jira_ci_unknown') if ci_id: self.ci = get_object_or_404(db.CI, id=ci_id) # preview only for devices if self.ci.content_object and self.ci.content_type.name == 'device': self.show_in_ralph = True self.ralph_ci_link = "/ui/search/info/%d" % self.ci.content_object.id self.service_name = self.get_first_parent_venture_name(ci_id) self.problems = db.CIProblem.objects.filter( ci=self.ci).order_by('-time').all() self.incidents = db.CIIncident.objects.filter( ci=self.ci).order_by('-time').all() self.git_changes = [ x.content_object for x in db.CIChange.objects.filter( ci=self.ci, type=db.CI_CHANGE_TYPES.CONF_GIT.id) ] self.device_attributes_changes = [ x.content_object for x in db.CIChange.objects.filter( ci=self.ci, type=db.CI_CHANGE_TYPES.DEVICE.id) ] reps = db.CIChangePuppet.objects.filter(ci=self.ci).all() for report in reps: puppet_logs = db.PuppetLog.objects.filter( cichange=report).all() self.puppet_reports.append( dict(report=report, logs=puppet_logs)) self.zabbix_triggers = db.CIChangeZabbixTrigger.objects.filter( ci=self.ci).order_by('-lastchange') self.so_events = db.CIChange.objects.filter( type=db.CI_CHANGE_TYPES.STATUSOFFICE.id, ci=self.ci).all() self.calculate_relations(ci_id) self.form_options['instance'] = self.ci self.form_options['initial'] = self.form_initial(self.ci) self.form_attributes_options['initial'] = self.custom_form_initial( self.ci) self.form_attributes = EditAttributeFormFactory( ci=self.ci).factory(**self.form_attributes_options) self.form = self.Form(**self.form_options) return super(Edit, self).get(*args, **kwargs)
def get(self, *args, **kwargs): if self.check_perm(): return self.check_perm() self.initialize_vars() try: ci_id = self.get_ci_id() except: # editing/viewing Ci which doesn's exists. return HttpResponseRedirect('/cmdb/ci/jira_ci_unknown') if ci_id: self.ci = get_object_or_404(db.CI, id=ci_id) # preview only for devices if (self.ci.content_object and self.ci.content_type.name == 'device'): self.show_in_ralph = True self.ralph_ci_link = ("/ui/search/info/%d" % self.ci.content_object.id) self.service_name = self.get_first_parent_venture_name(ci_id) self.problems = db.CIProblem.objects.filter( ci=self.ci).order_by('-time').all() self.incidents = db.CIIncident.objects.filter( ci=self.ci).order_by('-time').all() self.git_changes = [ x.content_object for x in db.CIChange.objects.filter( ci=self.ci, type=db.CI_CHANGE_TYPES.CONF_GIT.id)] self.device_attributes_changes = [ x.content_object for x in db.CIChange.objects.filter( ci=self.ci, type=db.CI_CHANGE_TYPES.DEVICE.id)] self.ci_attributes_changes = [ x.content_object for x in db.CIChange.objects.filter( ci=self.ci, type=db.CI_CHANGE_TYPES.CI.id).order_by('time') ] reps = db.CIChangePuppet.objects.filter(ci=self.ci).all() for report in reps: puppet_logs = db.PuppetLog.objects.filter( cichange=report).all() self.puppet_reports.append( dict(report=report, logs=puppet_logs) ) self.zabbix_triggers = db.CIChangeZabbixTrigger.objects.filter( ci=self.ci).order_by('-lastchange') self.so_events = db.CIChange.objects.filter( type=db.CI_CHANGE_TYPES.STATUSOFFICE.id, ci=self.ci).all() self.calculate_relations(ci_id) self.form_options['instance'] = self.ci self.form_options['initial'] = self.form_initial(self.ci) self.form_attributes_options['initial'] = self.custom_form_initial( self.ci) self.form_attributes = EditAttributeFormFactory( ci=self.ci).factory( **self.form_attributes_options) self.form = self.Form(**self.form_options) return super(Edit, self).get(*args, **kwargs)
def post(self, *args, **kwargs): self.initialize_vars() ci_id = self.kwargs.get('ci_id') if ci_id: self.ci = get_object_or_404(db.CI, id=ci_id) if self.Form: self.form = self.Form(self.request.POST, **self.form_options) self.form_attributes = EditAttributeFormFactory( ci=self.ci).factory(self.request.POST, **self.form_attributes_options) if self.form.is_valid() and self.form_attributes.is_valid(): self.form.data['base-id'] = self.ci.id model = self.form.save(commit=False) model.uid = self.ci.uid model.save(user=self.request.user) self.form_attributes.ci = model model_attributes = self.form_attributes.save() messages.success(self.request, "Changes saved.") return HttpResponseRedirect(self.request.path) else: messages.error(self.request, "Correct the errors.") return super(Edit, self).get(*args, **kwargs)
def post(self, *args, **kwargs): self.initialize_vars() ci_id = self.kwargs.get('ci_id') if ci_id: self.ci = get_object_or_404(db.CI, id=ci_id) self.form_options['instance'] = self.ci self.form = self.Form( self.request.POST, **self.form_options ) self.form_attributes = EditAttributeFormFactory( ci=self.ci).factory( self.request.POST, **self.form_attributes_options ) if self.form.is_valid() and self.form_attributes.is_valid(): model = self.form.save(commit=False) model.id = self.ci.id model.owners.clear() model.layers.clear() layers = self.form_attributes.data.getlist('base-layers') for layer in layers: model.layers.add(CILayer.objects.get(pk=int(layer))) owners_t = self.form_attributes.data.getlist( 'base-technical_owners') for owner in owners_t: own = CIOwnership( ci=model, owner=CIOwner.objects.get(pk=owner), type=1,) own.save() owners_b = self.form_attributes.data.getlist( 'base-business_owners') for owner in owners_b: own = CIOwnership( ci=model, owner=CIOwner.objects.get(pk=owner), type=2,) own.save() model.save(user=self.request.user) self.form_attributes.ci = model self.form_attributes.save() messages.success(self.request, "Changes saved.") return HttpResponseRedirect(self.request.path) else: messages.error(self.request, "Correct the errors.") return super(Edit, self).get(*args, **kwargs)
def post(self, *args, **kwargs): self.initialize_vars() ci_id = self.kwargs.get("ci_id") if ci_id: self.ci = get_object_or_404(db.CI, id=ci_id) if self.Form: self.form = self.Form(self.request.POST, **self.form_options) self.form_attributes = EditAttributeFormFactory(ci=self.ci).factory( self.request.POST, **self.form_attributes_options ) if self.form.is_valid() and self.form_attributes.is_valid(): self.form.data["base-id"] = self.ci.id model = self.form.save(commit=False) model.uid = self.ci.uid model.save(user=self.request.user) self.form_attributes.ci = model model_attributes = self.form_attributes.save() messages.success(self.request, "Changes saved.") return HttpResponseRedirect(self.request.path) else: messages.error(self.request, "Correct the errors.") return super(Edit, self).get(*args, **kwargs)
class Edit(BaseCMDBView): template_name = 'cmdb/edit_ci.html' Form = CIEditForm form_attributes_options = dict( label_suffix='', prefix='attr', ) form_options = dict( label_suffix='', prefix='base', ) def get_first_parent_venture_name(self, ci_id): cis = db.CI.objects.filter( relations__parent__child=ci_id, relations__parent__parent__type=db.CI_TYPES.VENTUREROLE.id).all() if cis: return cis[0].name def generate_breadcrumb(self): if getattr(self, 'ci'): ci = self.ci parent = ci.id else: return [] list = [] counter = 0 while parent and counter < 100: ci = db.CI.objects.filter(id=parent).all()[0] list.insert(0, ci) try: parent = db.CI.objects.filter(parent__child=parent).all()[0].id except: parent = None if parent == ci.id: parent = None counter += 1 return list def get_messages(self): days = datetime.timedelta(days=7) last_week_puppet_errors = db.CIChangePuppet.objects.filter( ci=self.ci, time__range=(datetime.datetime.now(), datetime.datetime.now() - days)).count() incidents = db.CIIncident.objects.filter(ci=self.ci, ).count() problems = db.CIProblem.objects.filter(ci=self.ci, ).count() messages = [] if last_week_puppet_errors: messages.append( dict( message="Puppet reported %d errors since last week." % (last_week_puppet_errors), title='Warning', type='warning', )) if incidents: messages.append( dict( message="This CI has %d incidents." % (incidents), title='Be carefull.', type='error', )) if problems: messages.append( dict( message="This CI has %d problems." % (problems), title='Be carefull.', type='error', )) return messages def get_context_data(self, **kwargs): ret = super(Edit, self).get_context_data(**kwargs) ret.update({ 'form': self.form, 'form_attributes': self.form_attributes, 'ci': self.ci, 'ci_id': self.ci.id, 'uid': self.ci.uid, 'label': 'Edit CI - ' + self.ci.uid, 'relations_contains': self.relations_contains, 'relations_requires': self.relations_requires, 'relations_isrequired': self.relations_isrequired, 'relations_parts': self.relations_parts, 'relations_hasrole': self.relations_hasrole, 'relations_isrole': self.relations_isrole, 'puppet_reports': self.puppet_reports, 'git_changes': self.git_changes, 'device_attributes_changes': self.device_attributes_changes, 'problems': self.problems, 'incidents': self.incidents, 'zabbix_triggers': self.zabbix_triggers, 'service_name': self.service_name, 'so_events': self.so_events, 'cmdb_messages': self.get_messages(), 'show_in_ralph': self.show_in_ralph, 'ralph_ci_link': self.ralph_ci_link, }) return ret def custom_form_initial(self, ci): data = dict() objs = db.CIAttributeValue.objects.filter(ci=ci) for obj in objs: field_type = obj.attribute.attribute_type if field_type == db.CI_ATTRIBUTE_TYPES.INTEGER.id: field_type = 'integer' value = obj.value_integer.value elif field_type == db.CI_ATTRIBUTE_TYPES.STRING.id: field_type = 'string' value = obj.value_string.value elif field_type == db.CI_ATTRIBUTE_TYPES.FLOAT.id: field_type = 'float' value = obj.value_float.value elif field_type == db.CI_ATTRIBUTE_TYPES.DATE.id: field_type = 'date' value = obj.value_date.value elif field_type == db.CI_ATTRIBUTE_TYPES.CHOICE.id: field_type = 'choice' value = obj.value_choice.value data['attribute_%s_%s' % (field_type, obj.attribute_id)] = value return data def form_initial(self, ci): data = dict( technical_owner=', '.join(ci.get_technical_owners()), ci=self.ci, ) return data def check_perm(self): if not self.get_permissions_dict().get( 'edit_configuration_item_info_generic_perm', False): return HttpResponseForbidden() def calculate_relations(self, ci_id): self.relations_contains = [ (x, x.child, get_icon_for(x.child)) for x in db.CIRelation.objects.filter( parent=ci_id, type=db.CI_RELATION_TYPES.CONTAINS.id) ] self.relations_parts = [ (x, x.parent, get_icon_for(x.parent)) for x in db.CIRelation.objects.filter( child=ci_id, type=db.CI_RELATION_TYPES.CONTAINS.id) ] self.relations_requires = [ (x, x.child, get_icon_for(x.parent)) for x in db.CIRelation.objects.filter( parent=ci_id, type=db.CI_RELATION_TYPES.REQUIRES.id) ] self.relations_isrequired = [ (x, x.parent, get_icon_for(x.parent)) for x in db.CIRelation.objects.filter( child=ci_id, type=db.CI_RELATION_TYPES.REQUIRES.id) ] self.relations_hasrole = [ (x, x.child, get_icon_for(x.parent)) for x in db.CIRelation.objects.filter( parent=ci_id, type=db.CI_RELATION_TYPES.HASROLE.id) ] self.relations_isrole = [ (x, x.parent, get_icon_for(x.parent)) for x in db.CIRelation.objects.filter( child=ci_id, type=db.CI_RELATION_TYPES.HASROLE.id) ] def get_ci_id(self): """ 2 types of id can land here. """ ci_id = self.kwargs.get('ci_id') if ci_id.find('-') >= 0: ci = db.CI.objects.get(uid=ci_id) return ci.id else: return self.kwargs.get('ci_id', None) def get(self, *args, **kwargs): if self.check_perm(): return self.check_perm() self.initialize_vars() try: ci_id = self.get_ci_id() except: # editing/viewing Ci which doesn's exists. return HttpResponseRedirect('/cmdb/ci/jira_ci_unknown') if ci_id: self.ci = get_object_or_404(db.CI, id=ci_id) # preview only for devices if self.ci.content_object and self.ci.content_type.name == 'device': self.show_in_ralph = True self.ralph_ci_link = "/ui/search/info/%d" % self.ci.content_object.id self.service_name = self.get_first_parent_venture_name(ci_id) self.problems = db.CIProblem.objects.filter( ci=self.ci).order_by('-time').all() self.incidents = db.CIIncident.objects.filter( ci=self.ci).order_by('-time').all() self.git_changes = [ x.content_object for x in db.CIChange.objects.filter( ci=self.ci, type=db.CI_CHANGE_TYPES.CONF_GIT.id) ] self.device_attributes_changes = [ x.content_object for x in db.CIChange.objects.filter( ci=self.ci, type=db.CI_CHANGE_TYPES.DEVICE.id) ] reps = db.CIChangePuppet.objects.filter(ci=self.ci).all() for report in reps: puppet_logs = db.PuppetLog.objects.filter( cichange=report).all() self.puppet_reports.append( dict(report=report, logs=puppet_logs)) self.zabbix_triggers = db.CIChangeZabbixTrigger.objects.filter( ci=self.ci).order_by('-lastchange') self.so_events = db.CIChange.objects.filter( type=db.CI_CHANGE_TYPES.STATUSOFFICE.id, ci=self.ci).all() self.calculate_relations(ci_id) self.form_options['instance'] = self.ci self.form_options['initial'] = self.form_initial(self.ci) self.form_attributes_options['initial'] = self.custom_form_initial( self.ci) self.form_attributes = EditAttributeFormFactory( ci=self.ci).factory(**self.form_attributes_options) self.form = self.Form(**self.form_options) return super(Edit, self).get(*args, **kwargs) def initialize_vars(self): self.form_attributes = {} self.service_name = '' self.relations_contains = [] self.relations_requires = [] self.relations_parts = [] self.relations_hasrole = [] self.relations_isrole = [] self.relations_isrequired = [] self.puppet_reports = [] self.git_changes = [] self.zabbix_triggers = [] self.device_attributes_changes = [] self.form = None self.ci = None self.relations_contains = [] self.relations_requires = [] self.relations_parts = [] self.relations_hasrole = [] self.relations_isrole = [] self.relations_isrequired = [] self.puppet_reports = [] self.git_changes = [] self.device_attributes_changes = [] self.zabbix_triggers = [] self.so_events = [] self.problems = [] self.incidents = [] self.show_in_ralph = False self.ralph_ci_link = "" @nested_commit_on_success def post(self, *args, **kwargs): self.initialize_vars() ci_id = self.kwargs.get('ci_id') if ci_id: self.ci = get_object_or_404(db.CI, id=ci_id) if self.Form: self.form = self.Form(self.request.POST, **self.form_options) self.form_attributes = EditAttributeFormFactory( ci=self.ci).factory(self.request.POST, **self.form_attributes_options) if self.form.is_valid() and self.form_attributes.is_valid(): self.form.data['base-id'] = self.ci.id model = self.form.save(commit=False) model.uid = self.ci.uid model.save(user=self.request.user) self.form_attributes.ci = model model_attributes = self.form_attributes.save() messages.success(self.request, "Changes saved.") return HttpResponseRedirect(self.request.path) else: messages.error(self.request, "Correct the errors.") return super(Edit, self).get(*args, **kwargs)
class Edit(BaseCMDBView): template_name = "cmdb/edit_ci.html" Form = CIEditForm form_attributes_options = dict(label_suffix="", prefix="attr") form_options = dict(label_suffix="", prefix="base") def get_first_parent_venture_name(self, ci_id): cis = db.CI.objects.filter( relations__parent__child=ci_id, relations__parent__parent__type=db.CI_TYPES.VENTUREROLE.id ).all() if cis: return cis[0].name def generate_breadcrumb(self): if getattr(self, "ci"): ci = self.ci parent = ci.id else: return [] list = [] counter = 0 while parent and counter < 100: ci = db.CI.objects.filter(id=parent).all()[0] list.insert(0, ci) try: parent = db.CI.objects.filter(parent__child=parent).all()[0].id except: parent = None if parent == ci.id: parent = None counter += 1 return list def get_messages(self): days = datetime.timedelta(days=7) last_week_puppet_errors = db.CIChangePuppet.objects.filter( ci=self.ci, time__range=(datetime.datetime.now(), datetime.datetime.now() - days) ).count() incidents = db.CIIncident.objects.filter(ci=self.ci).count() problems = db.CIProblem.objects.filter(ci=self.ci).count() messages = [] if last_week_puppet_errors: messages.append( dict( message="Puppet reported %d errors since last week." % (last_week_puppet_errors), title="Warning", type="warning", ) ) if incidents: messages.append(dict(message="This CI has %d incidents." % (incidents), title="Be carefull.", type="error")) if problems: messages.append(dict(message="This CI has %d problems." % (problems), title="Be carefull.", type="error")) return messages def get_context_data(self, **kwargs): ret = super(Edit, self).get_context_data(**kwargs) ret.update( { "form": self.form, "form_attributes": self.form_attributes, "ci": self.ci, "ci_id": self.ci.id, "uid": self.ci.uid, "label": "Edit CI - " + self.ci.uid, "relations_contains": self.relations_contains, "relations_requires": self.relations_requires, "relations_isrequired": self.relations_isrequired, "relations_parts": self.relations_parts, "relations_hasrole": self.relations_hasrole, "relations_isrole": self.relations_isrole, "puppet_reports": self.puppet_reports, "git_changes": self.git_changes, "device_attributes_changes": self.device_attributes_changes, "problems": self.problems, "incidents": self.incidents, "zabbix_triggers": self.zabbix_triggers, "service_name": self.service_name, "so_events": self.so_events, "cmdb_messages": self.get_messages(), "show_in_ralph": self.show_in_ralph, "ralph_ci_link": self.ralph_ci_link, } ) return ret def custom_form_initial(self, ci): data = dict() objs = db.CIAttributeValue.objects.filter(ci=ci) for obj in objs: field_type = obj.attribute.attribute_type if field_type == db.CI_ATTRIBUTE_TYPES.INTEGER.id: field_type = "integer" value = obj.value_integer.value elif field_type == db.CI_ATTRIBUTE_TYPES.STRING.id: field_type = "string" value = obj.value_string.value elif field_type == db.CI_ATTRIBUTE_TYPES.FLOAT.id: field_type = "float" value = obj.value_float.value elif field_type == db.CI_ATTRIBUTE_TYPES.DATE.id: field_type = "date" value = obj.value_date.value elif field_type == db.CI_ATTRIBUTE_TYPES.CHOICE.id: field_type = "choice" value = obj.value_choice.value data["attribute_%s_%s" % (field_type, obj.attribute_id)] = value return data def form_initial(self, ci): data = dict(technical_owner=", ".join(ci.get_technical_owners()), ci=self.ci) return data def check_perm(self): if not self.get_permissions_dict().get("edit_configuration_item_info_generic_perm", False): return HttpResponseForbidden() def calculate_relations(self, ci_id): self.relations_contains = [ (x, x.child, get_icon_for(x.child)) for x in db.CIRelation.objects.filter(parent=ci_id, type=db.CI_RELATION_TYPES.CONTAINS.id) ] self.relations_parts = [ (x, x.parent, get_icon_for(x.parent)) for x in db.CIRelation.objects.filter(child=ci_id, type=db.CI_RELATION_TYPES.CONTAINS.id) ] self.relations_requires = [ (x, x.child, get_icon_for(x.parent)) for x in db.CIRelation.objects.filter(parent=ci_id, type=db.CI_RELATION_TYPES.REQUIRES.id) ] self.relations_isrequired = [ (x, x.parent, get_icon_for(x.parent)) for x in db.CIRelation.objects.filter(child=ci_id, type=db.CI_RELATION_TYPES.REQUIRES.id) ] self.relations_hasrole = [ (x, x.child, get_icon_for(x.parent)) for x in db.CIRelation.objects.filter(parent=ci_id, type=db.CI_RELATION_TYPES.HASROLE.id) ] self.relations_isrole = [ (x, x.parent, get_icon_for(x.parent)) for x in db.CIRelation.objects.filter(child=ci_id, type=db.CI_RELATION_TYPES.HASROLE.id) ] def get_ci_id(self): """ 2 types of id can land here. """ ci_id = self.kwargs.get("ci_id") if ci_id.find("-") >= 0: ci = db.CI.objects.get(uid=ci_id) return ci.id else: return self.kwargs.get("ci_id", None) def get(self, *args, **kwargs): if self.check_perm(): return self.check_perm() self.initialize_vars() try: ci_id = self.get_ci_id() except: # editing/viewing Ci which doesn's exists. return HttpResponseRedirect("/cmdb/ci/jira_ci_unknown") if ci_id: self.ci = get_object_or_404(db.CI, id=ci_id) # preview only for devices if self.ci.content_object and self.ci.content_type.name == "device": self.show_in_ralph = True self.ralph_ci_link = "/ui/search/info/%d" % self.ci.content_object.id self.service_name = self.get_first_parent_venture_name(ci_id) self.problems = db.CIProblem.objects.filter(ci=self.ci).order_by("-time").all() self.incidents = db.CIIncident.objects.filter(ci=self.ci).order_by("-time").all() self.git_changes = [ x.content_object for x in db.CIChange.objects.filter(ci=self.ci, type=db.CI_CHANGE_TYPES.CONF_GIT.id) ] self.device_attributes_changes = [ x.content_object for x in db.CIChange.objects.filter(ci=self.ci, type=db.CI_CHANGE_TYPES.DEVICE.id) ] reps = db.CIChangePuppet.objects.filter(ci=self.ci).all() for report in reps: puppet_logs = db.PuppetLog.objects.filter(cichange=report).all() self.puppet_reports.append(dict(report=report, logs=puppet_logs)) self.zabbix_triggers = db.CIChangeZabbixTrigger.objects.filter(ci=self.ci).order_by("-lastchange") self.so_events = db.CIChange.objects.filter(type=db.CI_CHANGE_TYPES.STATUSOFFICE.id, ci=self.ci).all() self.calculate_relations(ci_id) self.form_options["instance"] = self.ci self.form_options["initial"] = self.form_initial(self.ci) self.form_attributes_options["initial"] = self.custom_form_initial(self.ci) self.form_attributes = EditAttributeFormFactory(ci=self.ci).factory(**self.form_attributes_options) self.form = self.Form(**self.form_options) return super(Edit, self).get(*args, **kwargs) def initialize_vars(self): self.form_attributes = {} self.service_name = "" self.relations_contains = [] self.relations_requires = [] self.relations_parts = [] self.relations_hasrole = [] self.relations_isrole = [] self.relations_isrequired = [] self.puppet_reports = [] self.git_changes = [] self.zabbix_triggers = [] self.device_attributes_changes = [] self.form = None self.ci = None self.relations_contains = [] self.relations_requires = [] self.relations_parts = [] self.relations_hasrole = [] self.relations_isrole = [] self.relations_isrequired = [] self.puppet_reports = [] self.git_changes = [] self.device_attributes_changes = [] self.zabbix_triggers = [] self.so_events = [] self.problems = [] self.incidents = [] self.show_in_ralph = False self.ralph_ci_link = "" @nested_commit_on_success def post(self, *args, **kwargs): self.initialize_vars() ci_id = self.kwargs.get("ci_id") if ci_id: self.ci = get_object_or_404(db.CI, id=ci_id) if self.Form: self.form = self.Form(self.request.POST, **self.form_options) self.form_attributes = EditAttributeFormFactory(ci=self.ci).factory( self.request.POST, **self.form_attributes_options ) if self.form.is_valid() and self.form_attributes.is_valid(): self.form.data["base-id"] = self.ci.id model = self.form.save(commit=False) model.uid = self.ci.uid model.save(user=self.request.user) self.form_attributes.ci = model model_attributes = self.form_attributes.save() messages.success(self.request, "Changes saved.") return HttpResponseRedirect(self.request.path) else: messages.error(self.request, "Correct the errors.") return super(Edit, self).get(*args, **kwargs)
class Edit(BaseCMDBView): template_name = 'cmdb/edit_ci.html' Form = CIEditForm form_attributes_options = dict(label_suffix='', prefix='attr') form_options = dict(label_suffix='', prefix='base') def get_first_parent_venture_name(self, ci_id): cis = db.CI.objects.filter( relations__parent__child=ci_id, relations__parent__parent__type=db.CI_TYPES.VENTUREROLE.id).all() if cis: return cis[0].name def generate_breadcrumb(self): if getattr(self, 'ci'): parent = self.ci.id else: return [] list = [] counter = 0 while parent and counter < 100: ci = db.CI.objects.filter(id=parent).all()[0] list.insert(0, ci) try: parent = db.CI.objects.filter(parent__child=parent).all()[0].id except: parent = None if parent == ci.id: parent = None counter += 1 return list def get_messages(self): days = datetime.timedelta(days=7) last_week_puppet_errors = db.CIChangePuppet.objects.filter( ci=self.ci, time__range=( datetime.datetime.now(), datetime.datetime.now() - days) ).count() incidents = db.CIIncident.objects.filter( ci=self.ci, ).count() problems = db.CIProblem.objects.filter( ci=self.ci, ).count() messages = [] if last_week_puppet_errors: messages.append(dict( message="Puppet reported %d errors since last week." % ( last_week_puppet_errors), title='Warning', type='warning', )) if incidents: messages.append(dict( message="This CI has %d incidents." % (incidents), title='Be carefull.', type='error', )) if problems: messages.append(dict( message="This CI has %d problems." % (problems), title='Be carefull.', type='error', )) return messages def get_context_data(self, **kwargs): ret = super(Edit, self).get_context_data(**kwargs) ret.update({ 'form': self.form, 'form_attributes': self.form_attributes, 'ci': self.ci, 'ci_id': self.ci.id, 'uid': self.ci.uid, 'label': 'Edit CI: {} (uid: {})'.format(self.ci.name, self.ci.uid), 'relations_contains': self.relations_contains, 'relations_requires': self.relations_requires, 'relations_isrequired': self.relations_isrequired, 'relations_parts': self.relations_parts, 'relations_hasrole': self.relations_hasrole, 'relations_isrole': self.relations_isrole, 'puppet_reports': self.puppet_reports, 'git_changes': self.git_changes, 'device_attributes_changes': self.device_attributes_changes, 'ci_attributes_changes': self.ci_attributes_changes, 'problems': self.problems, 'incidents': self.incidents, 'zabbix_triggers': self.zabbix_triggers, 'service_name': self.service_name, 'so_events': self.so_events, 'cmdb_messages': self.get_messages(), 'show_in_ralph': self.show_in_ralph, 'ralph_ci_link': self.ralph_ci_link, 'subsection': 'Edit - %s' % self.ci.name, }) return ret def custom_form_initial(self, ci): data = dict() objs = db.CIAttributeValue.objects.filter(ci=ci) for obj in objs: field_type = obj.attribute.attribute_type if field_type == db.CI_ATTRIBUTE_TYPES.INTEGER.id: field_type = 'integer' value = obj.value_integer.value elif field_type == db.CI_ATTRIBUTE_TYPES.STRING.id: field_type = 'string' value = obj.value_string.value elif field_type == db.CI_ATTRIBUTE_TYPES.FLOAT.id: field_type = 'float' value = obj.value_float.value elif field_type == db.CI_ATTRIBUTE_TYPES.DATE.id: field_type = 'date' value = obj.value_date.value elif field_type == db.CI_ATTRIBUTE_TYPES.CHOICE.id: field_type = 'choice' value = obj.value_choice.value data['attribute_%s_%s' % (field_type, obj.attribute_id)] = value return data def form_initial(self, ci): data = dict( technical_owner=', '.join(ci.get_technical_owners()), ci=self.ci, ) return data def check_perm(self): if not self.get_permissions_dict().get( 'edit_configuration_item_info_generic_perm', False): return HttpResponseForbidden() def calculate_relations(self, ci_id): self.relations_contains = [ (x, x.child, get_icon_for(x.child)) for x in db.CIRelation.objects.filter( parent=ci_id, type=db.CI_RELATION_TYPES.CONTAINS.id) ] self.relations_parts = [ (x, x.parent, get_icon_for(x.parent)) for x in db.CIRelation.objects.filter( child=ci_id, type=db.CI_RELATION_TYPES.CONTAINS.id) ] self.relations_requires = [ (x, x.child, get_icon_for(x.parent)) for x in db.CIRelation.objects.filter( parent=ci_id, type=db.CI_RELATION_TYPES.REQUIRES.id) ] self.relations_isrequired = [ (x, x.parent, get_icon_for(x.parent)) for x in db.CIRelation.objects.filter( child=ci_id, type=db.CI_RELATION_TYPES.REQUIRES.id) ] self.relations_hasrole = [ (x, x.child, get_icon_for(x.parent)) for x in db.CIRelation.objects.filter( parent=ci_id, type=db.CI_RELATION_TYPES.HASROLE.id) ] self.relations_isrole = [ (x, x.parent, get_icon_for(x.parent)) for x in db.CIRelation.objects.filter( child=ci_id, type=db.CI_RELATION_TYPES.HASROLE.id) ] def get_ci_id(self): """ 2 types of id can land here. """ ci_id = self.kwargs.get('ci_id') if ci_id.find('-') >= 0: ci = db.CI.objects.get(uid=ci_id) return ci.id else: return self.kwargs.get('ci_id', None) def get(self, *args, **kwargs): if self.check_perm(): return self.check_perm() self.initialize_vars() try: ci_id = self.get_ci_id() except: # editing/viewing Ci which doesn's exists. return HttpResponseRedirect('/cmdb/ci/jira_ci_unknown') if ci_id: self.ci = get_object_or_404(db.CI, id=ci_id) # preview only for devices if (self.ci.content_object and self.ci.content_type.name == 'device'): self.show_in_ralph = True self.ralph_ci_link = ("/ui/search/info/%d" % self.ci.content_object.id) self.service_name = self.get_first_parent_venture_name(ci_id) self.problems = db.CIProblem.objects.filter( ci=self.ci).order_by('-time').all() self.incidents = db.CIIncident.objects.filter( ci=self.ci).order_by('-time').all() self.git_changes = [ x.content_object for x in db.CIChange.objects.filter( ci=self.ci, type=db.CI_CHANGE_TYPES.CONF_GIT.id)] self.device_attributes_changes = [ x.content_object for x in db.CIChange.objects.filter( ci=self.ci, type=db.CI_CHANGE_TYPES.DEVICE.id)] self.ci_attributes_changes = [ x.content_object for x in db.CIChange.objects.filter( ci=self.ci, type=db.CI_CHANGE_TYPES.CI.id).order_by('time') ] reps = db.CIChangePuppet.objects.filter(ci=self.ci).all() for report in reps: puppet_logs = db.PuppetLog.objects.filter( cichange=report).all() self.puppet_reports.append( dict(report=report, logs=puppet_logs) ) self.zabbix_triggers = db.CIChangeZabbixTrigger.objects.filter( ci=self.ci).order_by('-lastchange') self.so_events = db.CIChange.objects.filter( type=db.CI_CHANGE_TYPES.STATUSOFFICE.id, ci=self.ci).all() self.calculate_relations(ci_id) self.form_options['instance'] = self.ci self.form_options['initial'] = self.form_initial(self.ci) self.form_attributes_options['initial'] = self.custom_form_initial( self.ci) self.form_attributes = EditAttributeFormFactory( ci=self.ci).factory( **self.form_attributes_options) self.form = self.Form(**self.form_options) return super(Edit, self).get(*args, **kwargs) def initialize_vars(self): self.form_attributes = {} self.service_name = '' self.relations_contains = [] self.relations_requires = [] self.relations_parts = [] self.relations_hasrole = [] self.relations_isrole = [] self.relations_isrequired = [] self.puppet_reports = [] self.git_changes = [] self.zabbix_triggers = [] self.ci_attributes_changes = [] self.device_attributes_changes = [] self.form = None self.ci = None self.relations_contains = [] self.relations_requires = [] self.relations_parts = [] self.relations_hasrole = [] self.relations_isrole = [] self.relations_isrequired = [] self.puppet_reports = [] self.git_changes = [] self.device_attributes_changes = [] self.zabbix_triggers = [] self.so_events = [] self.problems = [] self.incidents = [] self.show_in_ralph = False self.ralph_ci_link = "" @nested_commit_on_success def post(self, *args, **kwargs): self.initialize_vars() ci_id = self.kwargs.get('ci_id') if ci_id: self.ci = get_object_or_404(db.CI, id=ci_id) self.form_options['instance'] = self.ci self.form = self.Form( self.request.POST, **self.form_options ) self.form_attributes = EditAttributeFormFactory( ci=self.ci).factory( self.request.POST, **self.form_attributes_options ) if self.form.is_valid() and self.form_attributes.is_valid(): model = self.form.save(commit=False) model.id = self.ci.id model.owners.clear() model.layers.clear() layers = self.form_attributes.data.getlist('base-layers') for layer in layers: model.layers.add(CILayer.objects.get(pk=int(layer))) owners_t = self.form_attributes.data.getlist( 'base-technical_owners') for owner in owners_t: own = CIOwnership( ci=model, owner=CIOwner.objects.get(pk=owner), type=1,) own.save() owners_b = self.form_attributes.data.getlist( 'base-business_owners') for owner in owners_b: own = CIOwnership( ci=model, owner=CIOwner.objects.get(pk=owner), type=2,) own.save() model.save(user=self.request.user) self.form_attributes.ci = model self.form_attributes.save() messages.success(self.request, "Changes saved.") return HttpResponseRedirect(self.request.path) else: messages.error(self.request, "Correct the errors.") return super(Edit, self).get(*args, **kwargs)