Exemple #1
0
 def get_property_form(self):
     props = {}
     if not self.object.venture_role:
         return None
     for p in self.object.venture_role.roleproperty_set.all():
         try:
             value = p.rolepropertyvalue_set.filter(
                 device=self.object)[0].value
         except IndexError:
             value = ''
         props[p.symbol] = value
     properties = list(self.object.venture_role.roleproperty_set.all())
     if not properties:
         return None
     return PropertyForm(properties, initial=props)
Exemple #2
0
 def post(self, *args, **kwargs):
     self.object = self.get_object()
     has_perm = self.request.user.get_profile().has_perm
     if not has_perm(Perm.edit_device_info_generic, self.object.venture):
         return HttpResponseForbidden(
             "You don't have permission to edit this.")
     self.property_form = self.get_property_form()
     if 'propertiessave' in self.request.POST:
         properties = list(self.object.venture_role.roleproperty_set.all())
         self.property_form = PropertyForm(properties, self.request.POST)
         if self.property_form.is_valid():
             messages.success(self.request, "Properties updated.")
             self.save_properties(self.object,
                                  self.property_form.cleaned_data)
             return HttpResponseRedirect(self.request.path)
     elif 'save-tags' in self.request.POST:
         tags = self.request.POST.get('tags', '')
         self.object.untag_all()
         self.object.tag(tags, Language.en, self.request.user)
         messages.success(self.request, "Tags updated.")
         return HttpResponseRedirect(self.request.path)
     else:
         return super(Info, self).post(*args, **kwargs)
     return super(Info, self).get(*args, **kwargs)
Exemple #3
0
 def post(self, *args, **kwargs):
     self.object = self.get_object()
     has_perm = self.request.user.get_profile().has_perm
     if not has_perm(Perm.edit_device_info_generic, self.object.venture):
         return HttpResponseForbidden("You don't have permission to edit this.")
     self.property_form = self.get_property_form()
     if 'propertiessave' in self.request.POST:
         properties = list(self.object.venture_role.roleproperty_set.all())
         self.property_form = PropertyForm(properties, self.request.POST)
         if self.property_form.is_valid():
             messages.success(self.request, "Properties updated.")
             self.save_properties(self.object, self.property_form.cleaned_data)
             return HttpResponseRedirect(self.request.path)
     elif 'save-tags' in self.request.POST:
         tags = self.request.POST.get('tags', '')
         self.object.untag_all()
         self.object.tag(tags, Language.en, self.request.user)
         messages.success(self.request, "Tags updated.")
         return HttpResponseRedirect(self.request.path)
     else:
         return super(Info, self).post(*args, **kwargs)
     return super(Info, self).get(*args, **kwargs)
Exemple #4
0
class Info(DeviceUpdateView):
    template_name = 'ui/device_info.html'
    read_perm = Perm.read_device_info_generic
    edit_perm = Perm.edit_device_info_generic

    def get_form_class(self):
        if self.object.verified:
            return DeviceInfoVerifiedForm
        return DeviceInfoForm

    def get_initial(self):
        return {
            'model_name': self.object.get_model_name(),
            'rack_name': self.object.rack,
            'dc_name': self.object.dc,
        }

    def get_context_data(self, **kwargs):
        ret = super(Info, self).get_context_data(**kwargs)
        if self.object:
            tags = self.object.get_tags(official=False,
                                      author=self.request.user)
        else:
            tags = []
        tags = ['"%s"' % t.name if ',' in t.name else t.name for t in tags]
        ret.update({
            'property_form': self.property_form,
            'tags': ', '.join(tags),
            'dt': DeviceType,
        })
        return ret

    def get(self, *args, **kwargs):
        self.object = self.get_object()
        self.property_form = self.get_property_form()
        return super(Info, self).get(*args, **kwargs)

    def save_properties(self, device, properties):
        for symbol, value in properties.iteritems():
            p = device.venture_role.roleproperty_set.get(symbol=symbol)
            pv, created = RolePropertyValue.concurrent_get_or_create(property=p, device=device)
            pv.value = value
            pv.save()

    def get_property_form(self):
        props = {}
        if not self.object.venture_role:
            return None
        for p in self.object.venture_role.roleproperty_set.all():
            try:
                value = p.rolepropertyvalue_set.filter(device=self.object)[0].value
            except IndexError:
                value = ''
            props[p.symbol] = value
        properties = list(self.object.venture_role.roleproperty_set.all())
        if not properties:
            return None
        return PropertyForm(properties, initial=props)

    def post(self, *args, **kwargs):
        self.object = self.get_object()
        has_perm = self.request.user.get_profile().has_perm
        if not has_perm(Perm.edit_device_info_generic, self.object.venture):
            return HttpResponseForbidden("You don't have permission to edit this.")
        self.property_form = self.get_property_form()
        if 'propertiessave' in self.request.POST:
            properties = list(self.object.venture_role.roleproperty_set.all())
            self.property_form = PropertyForm(properties, self.request.POST)
            if self.property_form.is_valid():
                messages.success(self.request, "Properties updated.")
                self.save_properties(self.object, self.property_form.cleaned_data)
                return HttpResponseRedirect(self.request.path)
        elif 'save-tags' in self.request.POST:
            tags = self.request.POST.get('tags', '')
            self.object.untag_all()
            self.object.tag(tags, Language.en, self.request.user)
            messages.success(self.request, "Tags updated.")
            return HttpResponseRedirect(self.request.path)
        else:
            return super(Info, self).post(*args, **kwargs)
        return super(Info, self).get(*args, **kwargs)
Exemple #5
0
class Info(DeviceUpdateView):
    template_name = 'ui/device_info.html'
    read_perm = Perm.read_device_info_generic
    edit_perm = Perm.edit_device_info_generic

    def get_form_class(self):
        if self.object.verified:
            return DeviceInfoVerifiedForm
        return DeviceInfoForm

    def get_initial(self):
        return {
            'model_name': self.object.get_model_name(),
            'rack_name': self.object.rack,
            'dc_name': self.object.dc,
        }

    def get_context_data(self, **kwargs):
        ret = super(Info, self).get_context_data(**kwargs)
        if self.object:
            tags = self.object.get_tags(official=False,
                                        author=self.request.user)
        else:
            tags = []
        tags = ['"%s"' % t.name if ',' in t.name else t.name for t in tags]
        ret.update({
            'property_form': self.property_form,
            'tags': ', '.join(tags),
            'dt': DeviceType,
        })
        return ret

    def get(self, *args, **kwargs):
        self.object = self.get_object()
        self.property_form = self.get_property_form()
        return super(Info, self).get(*args, **kwargs)

    def save_properties(self, device, properties):
        for symbol, value in properties.iteritems():
            p = device.venture_role.roleproperty_set.get(symbol=symbol)
            pv, created = RolePropertyValue.concurrent_get_or_create(
                property=p, device=device)
            pv.value = value
            pv.save()

    def get_property_form(self):
        props = {}
        if not self.object.venture_role:
            return None
        for p in self.object.venture_role.roleproperty_set.all():
            try:
                value = p.rolepropertyvalue_set.filter(
                    device=self.object)[0].value
            except IndexError:
                value = ''
            props[p.symbol] = value
        properties = list(self.object.venture_role.roleproperty_set.all())
        if not properties:
            return None
        return PropertyForm(properties, initial=props)

    def post(self, *args, **kwargs):
        self.object = self.get_object()
        has_perm = self.request.user.get_profile().has_perm
        if not has_perm(Perm.edit_device_info_generic, self.object.venture):
            return HttpResponseForbidden(
                "You don't have permission to edit this.")
        self.property_form = self.get_property_form()
        if 'propertiessave' in self.request.POST:
            properties = list(self.object.venture_role.roleproperty_set.all())
            self.property_form = PropertyForm(properties, self.request.POST)
            if self.property_form.is_valid():
                messages.success(self.request, "Properties updated.")
                self.save_properties(self.object,
                                     self.property_form.cleaned_data)
                return HttpResponseRedirect(self.request.path)
        elif 'save-tags' in self.request.POST:
            tags = self.request.POST.get('tags', '')
            self.object.untag_all()
            self.object.tag(tags, Language.en, self.request.user)
            messages.success(self.request, "Tags updated.")
            return HttpResponseRedirect(self.request.path)
        else:
            return super(Info, self).post(*args, **kwargs)
        return super(Info, self).get(*args, **kwargs)