Skip to content

Commit

Permalink
Merge pull request #42 from Kani999/36-short-description-and-file-name
Browse files Browse the repository at this point in the history
Add short description and shorten filename
  • Loading branch information
Kani999 authored Sep 7, 2023
2 parents bf39028 + 6e73781 commit 6b0d03e
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 26 deletions.
2 changes: 1 addition & 1 deletion netbox_attachments/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class NetBoxAttachmentSerializer(NetBoxModelSerializer):
class Meta:
model = NetBoxAttachment
fields = [
'id', 'url', 'display', 'content_type', 'object_id', 'parent', 'name', 'file', 'created', 'last_updated', 'comments',
'id', 'url', 'display', 'content_type', 'object_id', 'parent', 'name', 'description', 'file', 'created', 'last_updated', 'comments',
]

def validate(self, data):
Expand Down
9 changes: 7 additions & 2 deletions netbox_attachments/filtersets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import django_filters
from django.db.models import Q
from extras.filters import TagFilter
from netbox.filtersets import BaseFilterSet
from utilities.filters import ContentTypeFilter
Expand All @@ -14,13 +15,17 @@ class NetBoxAttachmentFilterSet(BaseFilterSet):
created = django_filters.DateTimeFilter()
content_type = ContentTypeFilter()
name = django_filters.CharFilter(lookup_expr="icontains")
description = django_filters.CharFilter(lookup_expr="icontains")
tag = TagFilter()

class Meta:
model = NetBoxAttachment
fields = ['id', 'content_type_id', 'object_id', 'name']
fields = ['id', 'content_type_id', 'object_id', 'name', 'description']

def search(self, queryset, name, value):
if not value.strip():
return queryset
return queryset.filter(name__icontains=value)

name_filter = Q(name__icontains=value)
description_filter = Q(description__icontains=value)
return queryset.filter(name_filter | description_filter)
3 changes: 2 additions & 1 deletion netbox_attachments/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ class NetBoxAttachmentForm(NetBoxModelForm):
class Meta:
model = NetBoxAttachment
fields = [
'name', 'file', 'comments', 'tags',
'name', 'description', 'file', 'comments', 'tags',
]


class NetBoxAttachmentFilterForm(NetBoxModelFilterSetForm):
model = NetBoxAttachment
name = forms.CharField(required=False)
description = forms.CharField(required=False)
content_type_id = DynamicModelMultipleChoiceField(
queryset=ContentType.objects.all(),
required=False,
Expand Down
18 changes: 18 additions & 0 deletions netbox_attachments/migrations/0005_netboxattachment_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.4 on 2023-09-07 11:11

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('netbox_attachments', '0004_netboxattachment_size_and_more'),
]

operations = [
migrations.AddField(
model_name='netboxattachment',
name='description',
field=models.CharField(blank=True, max_length=200),
),
]
28 changes: 19 additions & 9 deletions netbox_attachments/models.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from django.contrib.contenttypes.fields import GenericForeignKey
import os

from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.db.models.signals import pre_delete
from django.dispatch import receiver
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from netbox.models import NetBoxModel
from utilities.querysets import RestrictedQuerySet
from django.contrib.contenttypes.models import ContentType
from django.db.models.signals import pre_delete
from django.dispatch import receiver
from django.core.exceptions import ObjectDoesNotExist


from .utils import attachment_upload

Expand All @@ -35,6 +35,11 @@ class NetBoxAttachment(NetBoxModel):
max_length=254,
blank=True
)
description = models.CharField(
verbose_name=_('description'),
max_length=200,
blank=True
)
comments = models.TextField(
blank=True
)
Expand All @@ -51,8 +56,12 @@ class Meta:
def __str__(self):
if self.name:
return self.name
filename = self.file.name.rsplit('/', 1)[-1]
return filename.split('_', 2)[2]

return self.filename

@property
def filename(self):
return os.path.basename(self.file.name)

@property
def parent(self):
Expand Down Expand Up @@ -101,4 +110,5 @@ def pre_delete_receiver(sender, instance, **kwargs):
# As you don't have generic relation you should manually
# find related actitities
ctype = ContentType.objects.get_for_model(instance)
NetBoxAttachment.objects.filter(content_type=ctype, object_id=instance.pk).delete()
NetBoxAttachment.objects.filter(
content_type=ctype, object_id=instance.pk).delete()
1 change: 1 addition & 0 deletions netbox_attachments/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ class NetBoxAttachmentIndex(SearchIndex):
model = NetBoxAttachment
fields = (
('name', 100),
('description', 200),
('comments', 5000),
)
4 changes: 2 additions & 2 deletions netbox_attachments/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class NetBoxAttachmentTable(NetBoxTable):

class Meta(NetBoxTable.Meta):
model = NetBoxAttachment
fields = ('pk', 'id', 'name', 'parent', 'content_type', 'object_id', 'file',
fields = ('pk', 'id', 'name', 'description', 'parent', 'content_type', 'object_id', 'file',
'size', 'comments', 'actions', 'created', 'last_updated', 'tags')
default_columns = ('id', 'name', 'parent',
default_columns = ('id', 'name', 'description', 'parent',
'content_type', 'object_id', 'tags')
row_attrs = {
'class': get_missing_parent_row_class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,31 @@ <h5 class="card-header">Attachments</h5>
<table class="table table-hover">
<tr>
<th>Name</th>
<th>Comment</th>
<th>Description</th>
<th>Size</th>
<th>Created</th>
<th></th>
</tr>
{% for attachment in netbox_attachments %}
<tr{% if not attachment.size %} class="table-danger"{% endif %}>
<tr {% if not attachment.size %}class="table-danger"{% endif %}>
<td>
<a href="{% url 'plugins:netbox_attachments:netboxattachment' pk=attachment.pk %}">{{ attachment }}</a>
</td>
<td>{{ attachment.comments }}</td>
<td>{{ attachment.description }}</td>
<td>{{ attachment.size|filesizeformat }}</td>
<td>{{ attachment.created }}</td>
<td class="text-end noprint">
<a href="{{attachment.file.url}}"
target="_blank"
class="btn btn-sm btn-primary download-attachment"
title="Download">
<a href="{{ attachment.file.url }}"
target="_blank"
class="btn btn-sm btn-primary download-attachment"
title="Download">
<i class="mdi mdi-download"></i>
</a>
<a href="{% url 'plugins:netbox_attachments:netboxattachment' pk=attachment.pk %}?return_url={{ request.path }}"
<a href="{% url 'plugins:netbox_attachments:netboxattachment' pk=attachment.pk %}?return_url={{ request.path }}"
class="btn btn-info btn-sm lh-1"
title="Detail">
<i class="mdi mdi-information-outline" aria-hidden="true"></i>
</a>

<a href="{% url 'plugins:netbox_attachments:netboxattachment_edit' pk=attachment.pk %}?return_url={{ request.path }}"
class="btn btn-warning btn-sm lh-1"
title="Edit Attachment">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ <h5 class="card-header">Attachments</h5>
<th scope="row">Name</th>
<td>{{ object }}</td>
</tr>
<tr>
<th scope="row">Description</th>
<td>{{ object.description }}</td>
</tr>
<tr>
<th scope="row">Filename</th>
<td>
<a href="{{ object.file.url }}" target="_blank">{{ object.file }}</a>
<a href="{{ object.file.url }}" target="_blank">{{ object.filename }}</a>
</td>
</tr>
<tr>
Expand Down
2 changes: 1 addition & 1 deletion netbox_attachments/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.0.3"
__version__ = "2.0.4"

0 comments on commit 6b0d03e

Please sign in to comment.