Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add related names to django models (images, logger, msgmaint) #2541

Merged
merged 5 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions python/nav/models/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,28 @@ class Image(models.Model):

id = models.AutoField(db_column='imageid', primary_key=True)
room = models.ForeignKey(
Room, on_delete=models.CASCADE, db_column='roomid', null=True
Room,
on_delete=models.CASCADE,
db_column='roomid',
null=True,
related_name="images",
)
location = models.ForeignKey(
Location, on_delete=models.CASCADE, db_column='locationid', null=True
Location,
on_delete=models.CASCADE,
db_column='locationid',
null=True,
related_name="images",
)
title = VarcharField()
path = VarcharField()
name = VarcharField()
created = models.DateTimeField(auto_now_add=True)
uploader = models.ForeignKey(
Account, on_delete=models.CASCADE, db_column='uploader'
Account,
on_delete=models.CASCADE,
db_column='uploader',
related_name="images",
)
priority = models.IntegerField()

Expand Down
52 changes: 43 additions & 9 deletions python/nav/models/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ class Origin(models.Model):
origin = models.AutoField(db_column='origin', primary_key=True)
name = VarcharField(db_column='name')
category = models.ForeignKey(
LoggerCategory, on_delete=models.CASCADE, db_column='category'
LoggerCategory,
on_delete=models.CASCADE,
db_column='category',
related_name="origins",
)

def __str__(self):
Expand Down Expand Up @@ -79,7 +82,10 @@ class LogMessageType(models.Model):

type = models.AutoField(db_column='type', primary_key=True)
priority = models.ForeignKey(
Priority, on_delete=models.CASCADE, db_column='priority'
Priority,
on_delete=models.CASCADE,
db_column='priority',
related_name="log_message_types",
)
facility = VarcharField(db_column='facility')
mnemonic = VarcharField(db_column='mnemonic')
Expand All @@ -101,11 +107,24 @@ class LogMessage(models.Model):

id = models.AutoField(db_column='id', primary_key=True)
time = models.DateTimeField(db_column='time', auto_now=True)
origin = models.ForeignKey(Origin, on_delete=models.CASCADE, db_column='origin')
origin = models.ForeignKey(
Origin,
on_delete=models.CASCADE,
db_column='origin',
related_name="log_messages",
)
newpriority = models.ForeignKey(
Priority, on_delete=models.CASCADE, db_column='newpriority'
Priority,
on_delete=models.CASCADE,
db_column='newpriority',
related_name="log_messages",
)
type = models.ForeignKey(
LogMessageType,
on_delete=models.CASCADE,
db_column='type',
related_name="log_messages",
)
type = models.ForeignKey(LogMessageType, on_delete=models.CASCADE, db_column='type')
message = VarcharField(db_column='message')

class Meta(object):
Expand Down Expand Up @@ -133,14 +152,29 @@ class MessageView(models.Model):
"""

origin = models.ForeignKey(
Origin, on_delete=models.CASCADE, db_column='origin', primary_key=True
Origin,
on_delete=models.CASCADE,
db_column='origin',
primary_key=True,
related_name="message_views",
)
type = models.ForeignKey(
LogMessageType,
on_delete=models.CASCADE,
db_column='type',
related_name="message_views",
)
type = models.ForeignKey(LogMessageType, on_delete=models.CASCADE, db_column='type')
newpriority = models.ForeignKey(
Priority, on_delete=models.CASCADE, db_column='newpriority'
Priority,
on_delete=models.CASCADE,
db_column='newpriority',
related_name="message_views",
)
category = models.ForeignKey(
LoggerCategory, on_delete=models.CASCADE, db_column='category'
LoggerCategory,
on_delete=models.CASCADE,
db_column='category',
related_name="message_views",
)
time = models.DateTimeField(db_column='time')

Expand Down
12 changes: 9 additions & 3 deletions python/nav/models/msgmaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ class Message(models.Model):
null=True,
)
maintenance_tasks = models.ManyToManyField(
'MaintenanceTask', through='MessageToMaintenanceTask', blank=True
'MaintenanceTask',
through='MessageToMaintenanceTask',
blank=True,
related_name="messages",
)

class Meta(object):
Expand Down Expand Up @@ -137,7 +140,7 @@ def get_components(self):
"""
Returns the list of model objects involved in this task
"""
return [c.component for c in self.maintenancecomponent_set.all()]
return [c.component for c in self.maintenance_components.all()]

def get_event_subjects(self):
"""
Expand Down Expand Up @@ -171,7 +174,10 @@ class MaintenanceComponent(models.Model):

id = models.AutoField(primary_key=True) # Serial for faking primary key
maintenance_task = models.ForeignKey(
MaintenanceTask, on_delete=models.CASCADE, db_column='maint_taskid'
MaintenanceTask,
on_delete=models.CASCADE,
db_column='maint_taskid',
related_name="maintenance_components",
)
key = VarcharField()
value = VarcharField()
Expand Down
2 changes: 1 addition & 1 deletion python/nav/web/info/images/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def create_hash(something, salt=False):

def get_next_priority(obj):
"""Get the next priority value for the images in the room/location"""
priority = obj.image_set.all().aggregate(Max('priority'))['priority__max']
priority = obj.images.all().aggregate(Max('priority'))['priority__max']
return priority + 1 if priority is not None else 0


Expand Down
2 changes: 1 addition & 1 deletion python/nav/web/info/location/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def locationinfo(request, locationid):
{
"location": location,
"navpath": navpath,
"images": location.image_set.all(),
"images": location.images.all(),
"title": create_title(navpath),
},
)
Expand Down
2 changes: 1 addition & 1 deletion python/nav/web/info/room/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def filter_netboxes(room):
def roominfo(request, roomid):
"""Controller for displaying roominfo"""
room = get_object_or_404(Room, id=roomid)
images = room.image_set.all()
images = room.images.all()
navpath = get_path() + [(room.id,)]
room.sorted_data = sorted(room.data.items())
room.meta_data = get_room_meta(room)
Expand Down
8 changes: 4 additions & 4 deletions python/nav/web/maintenance/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def active(request):
state__in=(MaintenanceTask.STATE_SCHEDULED, MaintenanceTask.STATE_ACTIVE),
)
.order_by('-start_time', '-end_time')
.annotate(component_count=Count('maintenancecomponent'))
.annotate(component_count=Count('maintenance_components'))
)
for task in tasks:
# Tasks that have only one component should show a link
Expand Down Expand Up @@ -147,7 +147,7 @@ def planned(request):
state__in=(MaintenanceTask.STATE_SCHEDULED, MaintenanceTask.STATE_ACTIVE),
)
.order_by('-start_time', '-end_time')
.annotate(component_count=Count('maintenancecomponent'))
.annotate(component_count=Count('maintenance_components'))
)
return render(
request,
Expand All @@ -172,7 +172,7 @@ def historic(request):
)
)
.order_by('-start_time', '-end_time')
.annotate(component_count=Count('maintenancecomponent'))
.annotate(component_count=Count('maintenance_components'))
)
return render(
request,
Expand Down Expand Up @@ -270,7 +270,7 @@ def edit(request, task_id=None, start_time=None, **_):
'location': [],
'netboxgroup': [],
}
for key, value in task.maintenancecomponent_set.values_list('key', 'value'):
for key, value in task.maintenance_components.values_list('key', 'value'):
if key in PRIMARY_KEY_INTEGER:
value = int(value)
component_keys[key].append(value)
Expand Down
2 changes: 1 addition & 1 deletion python/nav/web/templates/info/images/upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</div>

<ul id="editimages" class="large-block-grid-4">
{% for image in object.image_set.all %}
{% for image in object.images.all %}
<li class="imagecardcontainer">
<div data-imageid="{{ image.id }}" class="imagecard">
<div class="actions">
Expand Down