Add slug field for tools, and base URLs on it

This commit is contained in:
Adam Goldsmith 2021-05-19 17:01:28 -04:00
parent cb28da70e9
commit 6918a3d497
5 changed files with 41 additions and 13 deletions

View File

@ -28,6 +28,4 @@ class TaskAdmin(MarkdownxModelAdmin):
@admin.register(Tool)
class ToolAdmin(admin.ModelAdmin):
inlines = [GroupToolSubscriptionInline]
prepopulated_fields = {"slug": ("name",)}

View File

@ -0,0 +1,29 @@
from django.db import migrations, models
from django.utils.text import slugify
def slugify_name(apps, schema_editor):
Tool = apps.get_model('tasks', 'tool')
for tool in Tool.objects.all():
tool.slug = slugify(tool.name)
tool.save(update_fields=['slug'])
class Migration(migrations.Migration):
dependencies = [
('tasks', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='tool',
name='slug',
field=models.SlugField(blank=True, null=True, unique=True),
),
migrations.RunPython(slugify_name),
migrations.AlterField(
model_name='tool',
name='slug',
field=models.SlugField(unique=True),
)
]

View File

@ -9,13 +9,14 @@ from markdownx.models import MarkdownxField
class Tool(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
asset_tag = models.CharField(max_length=10, unique=True, blank=True)
def __str__(self):
return f"{self.name} - {self.asset_tag}"
return f"{self.name}{(' - ' + self.asset_tag) if self.asset_tag else ''}"
def get_absolute_url(self):
return reverse('toolDetail', args=[self.asset_tag])
return reverse('toolDetail', args=[self.slug])
class Task(models.Model):
@ -33,7 +34,7 @@ class Task(models.Model):
return f"{self.tool.name}: {self.name}"
def get_absolute_url(self):
return reverse('taskDetail', args=[self.tool.asset_tag, self.slug])
return reverse('taskDetail', args=[self.tool.slug, self.slug])
@property
def last_event(self):

View File

@ -6,9 +6,9 @@ from . import views
urlpatterns = [
path('', views.index, name='index'),
# ex: /CMS00001/
path('<str:asset_tag>/', views.toolDetail, name='toolDetail'),
path('<str:tool_slug>/', views.toolDetail, name='toolDetail'),
# ex: /CMS00001/tasks/
path('<str:asset_tag>/tasks/', lambda request, asset_tag: redirect('toolDetail', asset_tag)),
path('<str:tool_slug>/tasks/', lambda request, tool_slug: redirect('toolDetail', tool_slug)),
# ex: /CMS00001/tasks/task_name/
path('<str:asset_tag>/tasks/<str:task_slug>', views.taskDetail, name='taskDetail'),
path('<str:tool_slug>/tasks/<str:task_slug>', views.taskDetail, name='taskDetail'),
]

View File

@ -13,8 +13,8 @@ def index(request):
return render(request, 'tasks/index.djhtml', context)
def toolDetail(request, asset_tag):
tool = get_object_or_404(Tool, asset_tag=asset_tag)
def toolDetail(request, tool_slug):
tool = get_object_or_404(Tool, slug=tool_slug)
tasks = tool.task_set.all()
context = {
'tool': tool,
@ -23,8 +23,8 @@ def toolDetail(request, asset_tag):
return render(request, 'tasks/toolDetail.djhtml', context)
def taskDetail(request, asset_tag, task_slug):
tool = get_object_or_404(Tool, asset_tag=asset_tag)
def taskDetail(request, tool_slug, task_slug):
tool = get_object_or_404(Tool, slug=tool_slug)
task = get_object_or_404(tool.task_set, slug=task_slug)
events = task.event_set.all()