Esempio n. 1
0
 def add_view(self,
              request,
              slice_id,
              node_id,
              form_url='',
              extra_context=None):
     """ Customizations needed for being nested to slices """
     # hook for future use on self.save_model()
     slice = get_object_or_404(Slice, pk=slice_id)
     node = get_object_or_404(Node, pk=node_id)
     if Sliver.objects.filter(node=node, slice=slice).exists():
         raise DisallowedSliverCreation()
     self.slice_id = slice_id
     self.node_id = node_id
     title = 'Add sliver %s@%s' % (get_admin_link(slice),
                                   get_admin_link(node))
     context = {
         'header_title': 'Add sliver',
         'title': mark_safe(title),
         'slice': slice,
     }
     context.update(extra_context or {})
     return super(SliceSliversAdmin, self).add_view(request,
                                                    form_url='',
                                                    extra_context=context)
Esempio n. 2
0
 def change_view(self, request, object_id, form_url='', extra_context=None):
     """ Linked title """
     # Check object_id is a valid pk (simplification of ModelAdmin.get_object)
     try:
         int(object_id)
     except ValueError:
         object_id = None
     sliver = get_object_or_404(Sliver, pk=object_id)
     slice_link = get_admin_link(sliver.slice)
     node_link = get_admin_link(sliver.node)
     context = {
         'title': mark_safe('Change sliver %s@%s' % (slice_link, node_link)),
         'header_title': 'Change sliver'
     }
     context.update(extra_context or {})
     # warn user when sliver.set_state > slice.set_state
     # Only check on GET (for avoid false/duplicated warnings on POSTing)
     sliver_state = sliver.set_state
     slice_state = sliver.slice.set_state
     if (request.method == 'GET' and
             state_value(sliver_state) > state_value(slice_state)):
         msg = "Note: the slice's set state \"%s\" overrides the sliver's \
                current set state \"%s\"."% (slice_state, sliver_state)
         messages.warning(request, msg)
     return super(SliverAdmin, self).change_view(request, object_id,
             form_url=form_url, extra_context=context)
Esempio n. 3
0
def create_slivers(modeladmin, request, queryset):
    """ Create slivers in selected nodes """
    opts = modeladmin.model._meta
    app_label = opts.app_label

    slice = get_object_or_404(Slice, pk=modeladmin.slice_id)

    if not modeladmin.has_change_permission(request, obj=slice):
        raise PermissionDenied

    n = queryset.count()
    if n == 1:
        node = queryset.get()
        return redirect('admin:slices_slice_add_sliver', modeladmin.slice_id,
                        node.pk)

    if request.POST.get('post'):
        form = SliverIfaceBulkForm(slice, queryset, request.POST)
        if form.is_valid():
            optional_ifaces = form.cleaned_data
            requested_ifaces = [
                field for field, value in optional_ifaces.iteritems() if value
            ]

            for node in queryset:
                sliver = Sliver(slice=slice, node=node)
                if not request.user.has_perm('slices.add_sliver', sliver):
                    raise PermissionDenied
                sliver.save()
                for iface_type in requested_ifaces:
                    SliverIface.objects.create_default(sliver, iface_type)
                slice_modeladmin = get_modeladmin(Slice)
                msg = 'Added sliver "%s"' % force_text(sliver)
                slice_modeladmin.log_change(request, slice, msg)
                sliver_modeladmin = get_modeladmin(Sliver)
                # AUTO_CREATE SliverIfaces
                sliver_modeladmin.log_addition(request, sliver)

            modeladmin.message_user(request,
                                    "Successfully created %d slivers." % n)
            # Return None to display the change list page again.
            return redirect('admin:slices_slice_change', slice.pk)

    context = {
        "title": "Are you sure?",
        "slice": slice,
        "deletable_objects": [[get_admin_link(node) for node in queryset]],
        'queryset': queryset,
        "opts": opts,
        "app_label": app_label,
        'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
        'form': SliverIfaceBulkForm(slice, queryset),
    }

    return TemplateResponse(
        request,
        "admin/slices/slice/create_slivers_confirmation.html",
        context,
        current_app=modeladmin.admin_site.name)
Esempio n. 4
0
 def __init__(self, *args, **kwargs):
     super(UserRolesForm, self).__init__(*args, **kwargs)
     if 'group' in self.fields:
         # readonly forms doesn't have fields
         instance = kwargs.get('instance', None)
         if instance:
             self.fields['group'].widget = ReadOnlyWidget(original_value=instance.group.pk,
                     display_value=mark_safe('<b>'+get_admin_link(instance.group)))
Esempio n. 5
0
 def add_view(self, request, slice_id, node_id, form_url='', extra_context=None):
     """ Customizations needed for being nested to slices """
     # hook for future use on self.save_model()
     slice = get_object_or_404(Slice, pk=slice_id)
     node = get_object_or_404(Node, pk=node_id)
     if Sliver.objects.filter(node=node, slice=slice).exists():
         raise DisallowedSliverCreation()
     self.slice_id = slice_id
     self.node_id = node_id
     title = 'Add sliver %s@%s' % (get_admin_link(slice), get_admin_link(node))
     context = {
         'header_title': 'Add sliver',
         'title': mark_safe(title),
         'slice': slice,
     }
     context.update(extra_context or {})
     return super(SliceSliversAdmin, self).add_view(request, form_url='',
             extra_context=context)
Esempio n. 6
0
 def __init__(self, *args, **kwargs):
     super(UserRolesForm, self).__init__(*args, **kwargs)
     if 'group' in self.fields:
         # readonly forms doesn't have fields
         instance = kwargs.get('instance', None)
         if instance:
             self.fields['group'].widget = ReadOnlyWidget(
                 original_value=instance.group.pk,
                 display_value=mark_safe('<b>' +
                                         get_admin_link(instance.group)))
Esempio n. 7
0
 def __init__(self, *args, **kwargs):
     super(GroupRolesInlineForm, self).__init__(*args, **kwargs)
     if 'user' in self.fields:
         # readonly forms doesn't have fields
         instance = kwargs.get('instance', None)
         if instance:
             self.fields['user'].widget = ReadOnlyWidget(original_value=instance.user.pk,
                     display_value=mark_safe('<b>'+get_admin_link(instance.user)))
         elif self.group:
             users = User.objects.exclude(roles__group=self.group).distinct()
             self.fields['user'].queryset = users.order_by('username')
Esempio n. 8
0
 def display_summary(self, ticket):
     author_link = display_author(ticket, 'created_by')
     created = display_timesince(ticket.created_on)
     messages = ticket.messages.order_by('-created_on')
     updated = ''
     if messages:
         updated_on = display_timesince(messages[0].created_on)
         updated_by = get_admin_link(messages[0].author)
         updated = '. Updated by %s about %s' % (updated_by, updated_on)
     msg = '<h4>Added by %s about %s%s</h4>' % (author_link, created, updated)
     return mark_safe(msg)
Esempio n. 9
0
def create_slivers(modeladmin, request, queryset):
    """ Create slivers in selected nodes """
    opts = modeladmin.model._meta
    app_label = opts.app_label
    
    slice = get_object_or_404(Slice, pk=modeladmin.slice_id)
    
    if not modeladmin.has_change_permission(request, obj=slice):
        raise PermissionDenied
    
    n = queryset.count()
    if n == 1:
        node = queryset.get()
        return redirect('admin:slices_slice_add_sliver', modeladmin.slice_id, node.pk)
    
    if request.POST.get('post'):
        form = SliverIfaceBulkForm(slice, queryset, request.POST)
        if form.is_valid():
            optional_ifaces = form.cleaned_data
            requested_ifaces = [ field for field, value in optional_ifaces.iteritems() if value ]
            
            for node in queryset:
                sliver = Sliver(slice=slice, node=node)
                if not request.user.has_perm('slices.add_sliver', sliver):
                    raise PermissionDenied
                sliver.save()
                for iface_type in requested_ifaces:
                    iface = Sliver.get_registered_ifaces()[iface_type]
                    SliverIface.objects.create(sliver=sliver, name=iface.DEFAULT_NAME, type=iface_type)
                slice_modeladmin = get_modeladmin(Slice)
                msg = 'Added sliver "%s"' % force_text(sliver)
                slice_modeladmin.log_change(request, slice, msg)
                sliver_modeladmin = get_modeladmin(Sliver)
                # AUTO_CREATE SliverIfaces
                sliver_modeladmin.log_addition(request, sliver)
            
            modeladmin.message_user(request, "Successfully created %d slivers." % n)
            # Return None to display the change list page again.
            return redirect('admin:slices_slice_change', slice.pk)
    
    context = {
        "title": "Are you sure?",
        "slice": slice,
        "deletable_objects": [[ get_admin_link(node) for node in queryset ]],
        'queryset': queryset,
        "opts": opts,
        "app_label": app_label,
        'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
        'form': SliverIfaceBulkForm(slice, queryset),
    }
    
    return TemplateResponse(request, "admin/slices/slice/create_slivers_confirmation.html",
                            context, current_app=modeladmin.admin_site.name)
Esempio n. 10
0
 def changelist_view(self, request, slice_id, extra_context=None):
     """ Just fixing title and breadcrumbs """
     slice = get_object_or_404(Slice, pk=slice_id)
     self.slice_id = slice_id
     title = 'Select one or more nodes for creating %s slivers' % get_admin_link(slice)
     context = {
         'title': mark_safe(title),
         'slice': slice,
     }
     context.update(extra_context or {})
     # call admin.ModelAdmin to avoid my_nodes default NodeAdmin changelist filter
     return admin.ModelAdmin.changelist_view(self, request, extra_context=context)
Esempio n. 11
0
    def change_view(self, request, object_id, form_url='', extra_context=None):
        """ Linked title """
        # Check object_id is a valid pk (simplification of ModelAdmin.get_object)
        try:
            int(object_id)
        except ValueError:
            object_id = None
        obj = get_object_or_404(Sliver, pk=object_id)
        slice_link = get_admin_link(obj.slice)
        node_link = get_admin_link(obj.node)
        context = {
            'title':
            mark_safe('Change sliver %s@%s' % (slice_link, node_link)),
            'header_title': 'Change sliver'
        }
        context.update(extra_context or {})

        if request.method == 'GET':
            # notify user about relevant sliver config
            sliver_state = obj.set_state
            slice_state = obj.slice.set_state
            if state_value(sliver_state) > state_value(slice_state):
                msg = ("Note: the slice's set state '%s' overrides the "
                       "sliver's current set state '%s'.") % (slice_state,
                                                              sliver_state)
                messages.warning(request, msg)

            if (not is_valid_description(obj.slice.description)
                    and not is_valid_description(obj.description)):
                msg = ("The sliver description is too short. Please provide "
                       "more details about the experiment or service. Help us "
                       "providing valuable feedback to Community Networks' "
                       "members.")
                messages.warning(request, msg)

        return super(SliverAdmin, self).change_view(request,
                                                    object_id,
                                                    form_url=form_url,
                                                    extra_context=context)
Esempio n. 12
0
 def changelist_view(self, request, operation_id, extra_context=None):
     """ Just fixing title and breadcrumbs """
     operation = get_object_or_404(Operation, pk=operation_id)
     self.operation_id = operation_id
     link = get_admin_link(operation)
     title = 'Select one or more nodes for executing %s operation' % link
     context = {
         'title': mark_safe(title),
         'header_title': 'Executing %s operation' % operation,
         'operation': operation
     }
     context.update(extra_context or {})
     # call admin.ModelAdmin to avoid my_nodes default NodeAdmin changelist filter
     return admin.ModelAdmin.changelist_view(self, request, extra_context=context)
Esempio n. 13
0
 def __init__(self, *args, **kwargs):
     super(GroupRolesInlineForm, self).__init__(*args, **kwargs)
     if 'user' in self.fields:
         # readonly forms doesn't have fields
         instance = kwargs.get('instance', None)
         if instance:
             self.fields['user'].widget = ReadOnlyWidget(
                 original_value=instance.user.pk,
                 display_value=mark_safe('<b>' +
                                         get_admin_link(instance.user)))
         elif self.group:
             users = User.objects.exclude(
                 roles__group=self.group).distinct()
             self.fields['user'].queryset = users.order_by('username')
Esempio n. 14
0
 def change_view(self, request, object_id, form_url='', extra_context=None):
     """ Linked title """
     # Check object_id is a valid pk (simplification of ModelAdmin.get_object)
     try:
         int(object_id)
     except ValueError:
         object_id = None
     obj = get_object_or_404(Sliver, pk=object_id)
     slice_link = get_admin_link(obj.slice)
     node_link = get_admin_link(obj.node)
     context = {
         'title': mark_safe('Change sliver %s@%s' % (slice_link, node_link)),
         'header_title': 'Change sliver'
     }
     context.update(extra_context or {})
     
     if request.method == 'GET':
         # notify user about relevant sliver config
         sliver_state = obj.set_state
         slice_state = obj.slice.set_state
         if state_value(sliver_state) > state_value(slice_state):
             msg = ("Note: the slice's set state '%s' overrides the "
                    "sliver's current set state '%s'.") % (slice_state,
                     sliver_state)
             messages.warning(request, msg)
         
         if (not is_valid_description(obj.slice.description) and
             not is_valid_description(obj.description)):
             msg = ("The sliver description is too short. Please provide "
                    "more details about the experiment or service. Help us "
                    "providing valuable feedback to Community Networks' "
                    "members.")
             messages.warning(request, msg)
     
     return super(SliverAdmin, self).change_view(request, object_id,
             form_url=form_url, extra_context=context)
Esempio n. 15
0
 def changelist_view(self, request, slice_id, extra_context=None):
     """ Just fixing title and breadcrumbs """
     slice = get_object_or_404(Slice, pk=slice_id)
     self.slice_id = slice_id
     title = 'Select one or more nodes for creating %s slivers' % get_admin_link(
         slice)
     context = {
         'title': mark_safe(title),
         'slice': slice,
     }
     context.update(extra_context or {})
     # call admin.ModelAdmin to avoid my_nodes default NodeAdmin changelist filter
     return admin.ModelAdmin.changelist_view(self,
                                             request,
                                             extra_context=context)
Esempio n. 16
0
 def changelist_view(self, request, *args, **kwargs):
     """ Provide ping action """
     object_id = kwargs.get('object_id')
     request.state_id = object_id
     state = get_object_or_404(State, pk=object_id)
     related_obj_link = get_admin_link(state.content_object)
     context = kwargs.get('extra_context', {})
     context.update({
         'title': mark_safe('State history (%s)' % related_obj_link),
         'header_title': 'State history',
         'obj_opts': state.content_object._meta,
         'obj': state.content_object,
         'ip_addr': state.get_node().mgmt_net.addr,
     })
     return super(StateHistoryAdmin, self).changelist_view(request, extra_context=context)
Esempio n. 17
0
 def changelist_view(self, request, operation_id, extra_context=None):
     """ Just fixing title and breadcrumbs """
     operation = get_object_or_404(Operation, pk=operation_id)
     self.operation_id = operation_id
     link = get_admin_link(operation)
     title = 'Select one or more nodes for executing %s operation' % link
     context = {
         'title': mark_safe(title),
         'header_title': 'Executing %s operation' % operation,
         'operation': operation
     }
     context.update(extra_context or {})
     # call admin.ModelAdmin to avoid my_nodes default NodeAdmin changelist filter
     return admin.ModelAdmin.changelist_view(self,
                                             request,
                                             extra_context=context)
Esempio n. 18
0
 def changelist_view(self, request, extra_context=None):
     context = {}
     for query, model in [('execution__operation', Operation), ('execution', Execution)]:
         pk = request.GET.get(query, False)
         if pk:
             related_object = model.objects.get(pk=pk)
             url = get_admin_link(related_object)
             name = related_object._meta.object_name.lower()
             changelist_url = reverse('admin:maintenance_%s_changelist' % name)
             changelist_url = '<a href="%s">%s</a>' % (changelist_url, name.capitalize())
             context = {
                 'title': mark_safe('Manage instances of %s %s' % (name, url)),
                 'related_object_url': url,
                 'related_object_changelist_url': mark_safe(changelist_url)
             }
             break
     context.update(extra_context or {})
     return super(InstanceAdmin, self).changelist_view(request, extra_context=context)
Esempio n. 19
0
def execute_operation(modeladmin, request, queryset):
    if not request.user.is_superuser:
        raise PermissionDenied
    
    opts = modeladmin.model._meta
    app_label = opts.app_label
    
    operation = get_object_or_404(Operation, pk=modeladmin.operation_id)
    
    if request.POST.get('post'):
        form = ExecutionForm(request.POST)
        if form.is_valid():
            include_new_nodes = form.cleaned_data['include_new_nodes']
            retry_if_offline = form.cleaned_data['retry_if_offline']
        instances = operation.execute(queryset, include_new_nodes=include_new_nodes,
            retry_if_offline=retry_if_offline)
        for instance in instances:
            msg = 'Executed operation "%s"' % force_text(operation)
            modeladmin.log_change(request, operation, msg)
            instance_modeladmin = get_modeladmin(Instance)
            # AUTO_CREATE instances
            instance_modeladmin.log_addition(request, instance)
            modeladmin.message_user(request, "Successfully created %d instances." % len(instances))
            # Return None to display the change list page again.
            return redirect('admin:maintenance_operation_change', operation.pk)
    
    include_new_nodes = operation.executions.filter(include_new_nodes=True).exists()
    
    context = {
        "title": "Are you sure?",
        "operation": operation,
        "deletable_objects": [[ get_admin_link(node) for node in queryset ]],
        'queryset': queryset,
        "opts": opts,
        "app_label": app_label,
        'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
        'form': ExecutionForm(initial={'include_new_nodes': include_new_nodes}),
    }
    
    return TemplateResponse(request, "admin/maintenance/operation/execute_operation_confirmation.html",
                            context, current_app=modeladmin.admin_site.name)
Esempio n. 20
0
 def changelist_view(self, request, *args, **kwargs):
     """ Provide ping action """
     object_id = kwargs.get('object_id')
     request.state_id = object_id
     state = get_object_or_404(State, pk=object_id)
     related_obj_link = get_admin_link(state.content_object)
     context = kwargs.get('extra_context', {})
     context.update({
         'title':
         mark_safe('State history (%s)' % related_obj_link),
         'header_title':
         'State history',
         'obj_opts':
         state.content_object._meta,
         'obj':
         state.content_object,
         'ip_addr':
         state.get_node().mgmt_net.addr,
     })
     return super(StateHistoryAdmin,
                  self).changelist_view(request, extra_context=context)
Esempio n. 21
0
 def changelist_view(self, request, extra_context=None):
     context = {}
     for query, model in [('execution__operation', Operation),
                          ('execution', Execution)]:
         pk = request.GET.get(query, False)
         if pk:
             related_object = model.objects.get(pk=pk)
             url = get_admin_link(related_object)
             name = related_object._meta.object_name.lower()
             changelist_url = reverse('admin:maintenance_%s_changelist' %
                                      name)
             changelist_url = '<a href="%s">%s</a>' % (changelist_url,
                                                       name.capitalize())
             context = {
                 'title':
                 mark_safe('Manage instances of %s %s' % (name, url)),
                 'related_object_url': url,
                 'related_object_changelist_url': mark_safe(changelist_url)
             }
             break
     context.update(extra_context or {})
     return super(InstanceAdmin,
                  self).changelist_view(request, extra_context=context)
Esempio n. 22
0
 def display_owner(self, ticket):
     return get_admin_link(ticket.owner) or '-'
Esempio n. 23
0
 def owner_link(self, instance):
     return get_admin_link(instance.owner)
Esempio n. 24
0
 def display_queue(self, ticket):
     return get_admin_link(ticket.queue) or '-'
Esempio n. 25
0
 def group_links(self, instance):
     groups = instance.groups.all()
     return ', '.join([ get_admin_link(group) for group in groups ])
Esempio n. 26
0
 def group_link(self, instance):
     """ Link to related Group """
     return mark_safe("<b>%s</b>" % get_admin_link(instance.group))
Esempio n. 27
0
def display_author(obj, field_name, href_name=''):
    """Get link or stored name if user doesn't exists (#289)"""
    author = getattr(obj, field_name)
    if author is None:
        return '<span class="author">%s</span>' % getattr(obj, field_name + '_name')
    return get_admin_link(author)
Esempio n. 28
0
 def state(self, instance):
     state = colored_state(instance)
     return mark_safe("<b>%s</b>" % get_admin_link(instance, href_name=state)) 
Esempio n. 29
0
 def group_link(self, instance):
     """ Link to related Group """
     return get_admin_link(instance.group)
Esempio n. 30
0
 def group_links(self, instance):
     groups = instance.groups.all()
     return ', '.join([get_admin_link(group) for group in groups])
Esempio n. 31
0
 def node_link(self, instance):
     return get_admin_link(instance.node)
Esempio n. 32
0
 def group_link(self, instance):
     """ Link to related Group """
     return mark_safe("<b>%s</b>" % get_admin_link(instance.group))
Esempio n. 33
0
 def sliver_link(self, instance):
     """ Link to related sliver used on change_view """
     return mark_safe(get_admin_link(instance.sliver))
Esempio n. 34
0
 def display_execution(self, instance):
     return get_admin_link(instance.execution)
Esempio n. 35
0
 def display_template(self, obj):
     """ Show SliverDefaults template on Slice changelist"""
     return get_admin_link(obj.sliver_defaults.template)
Esempio n. 36
0
 def slice_link(self, instance):
     return get_admin_link(instance.slice)
Esempio n. 37
0
 def node_link(self, instance):
     """ Display node change link on the inline form """
     return get_admin_link(instance.node)
Esempio n. 38
0
 def display_execution(self, instance):
     return get_admin_link(instance.execution)
Esempio n. 39
0
def state_link(instance):
    state = colored_state(instance)
    return mark_safe("<b>%s</b>" % get_admin_link(instance, href_name=state))
Esempio n. 40
0
 def execution_link(self, execution):
     return mark_safe('<b>' + get_admin_link(execution) + '</b>')
Esempio n. 41
0
 def display_template(self, obj):
     """ Show SliverDefaults template on Slice changelist"""
     return get_admin_link(obj.sliver_defaults.template)
Esempio n. 42
0
 def user_link(self, instance):
     """ Link to related User """
     return mark_safe("<b>%s</b>" % get_admin_link(instance.user))
Esempio n. 43
0
 def node_link(self, instance):
     """ Link to related node used on change_view """
     return mark_safe("<b>%s</b>" % get_admin_link(instance.node))
Esempio n. 44
0
 def group_link(self, instance):
     """ Link to related Group """
     return get_admin_link(instance.group)
Esempio n. 45
0
 def user_link(self, instance):
     """ Link to related User """
     return mark_safe("<b>%s</b>" % get_admin_link(instance.user))
Esempio n. 46
0
 def ticket_id(self, instance):
     return mark_safe('<b>%s</b>' % get_admin_link(instance))
Esempio n. 47
0
 def operation_link(self, instance):
     return mark_safe("<b>%s</b>" % get_admin_link(instance.operation))
Esempio n. 48
0
 def execution_link(self, execution):
     return mark_safe('<b>' + get_admin_link(execution) + '</b>')
Esempio n. 49
0
 def node_link(self, instance):
     return get_admin_link(instance.node)
Esempio n. 50
0
 def display_group(self, ticket):
     return get_admin_link(ticket.group) or '-'
Esempio n. 51
0
 def task_link(self, build):
     """ Display Celery task change view if exists """
     if build.db_task:
         return get_admin_link(build.db_task, href_name=build.db_task.task_id)
Esempio n. 52
0
 def slice_link(self, instance):
     return get_admin_link(instance.slice)
Esempio n. 53
0
 def task_link(self, build):
     """ Display Celery task change view if exists """
     if build.db_task:
         return get_admin_link(build.db_task,
                               href_name=build.db_task.task_id)