Added asset model and basic views

This commit is contained in:
Dirk Jahnke 2022-03-10 17:28:39 +01:00
parent 7e446bcbec
commit 16a0b92731
16 changed files with 156 additions and 0 deletions

0
asset/__init__.py Normal file
View File

3
asset/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
asset/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class AssetConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'asset'

View File

@ -0,0 +1,30 @@
# Generated by Django 3.2.12 on 2022-03-10 16:09
import asset.models
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Asset',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=40)),
('description', models.CharField(blank=True, max_length=250)),
('quantity', models.IntegerField(default=1)),
('created_ts', models.DateTimeField(auto_now_add=True, verbose_name='datetime created')),
('changed_ts', models.DateTimeField(auto_now=True, verbose_name='datetime updated')),
('changed_by', models.ForeignKey(on_delete=models.SET(asset.models.get_sentinel_user), related_name='changed_assets', to=settings.AUTH_USER_MODEL)),
('created_by', models.ForeignKey(on_delete=models.SET(asset.models.get_sentinel_user), related_name='created_assets', to=settings.AUTH_USER_MODEL)),
],
),
]

View File

@ -0,0 +1,23 @@
# Generated by Django 3.2.12 on 2022-03-10 16:20
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('asset', '0001_initial'),
]
operations = [
migrations.RemoveField(
model_name='asset',
name='name',
),
migrations.AddField(
model_name='asset',
name='named_id',
field=models.CharField(default='defval', max_length=40, unique=True),
preserve_default=False,
),
]

View File

18
asset/models.py Normal file
View File

@ -0,0 +1,18 @@
from django.db import models
from django.conf import settings
from django.contrib.auth import get_user_model
# from django.contrib.auth.models import User
from container.models import Container
def get_sentinel_user():
return get_user_model().objects.get_or_create(username='deleted')[0]
class Asset(models.Model):
named_id = models.CharField(max_length=40, unique=True)
description = models.CharField(max_length=250, blank=True)
quantity = models.IntegerField(default=1)
created_ts = models.DateTimeField('datetime created', auto_now_add=True)
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET(get_sentinel_user), related_name='created_assets')
changed_ts = models.DateTimeField('datetime updated', auto_now=True)
changed_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET(get_sentinel_user), related_name='changed_assets')

View File

@ -0,0 +1,6 @@
{% extends 'base.html' %}
{% load statics %}
{% block content %}
<h2>Confirm Asset Delete</h2>
{% endblock %}

View File

@ -0,0 +1,6 @@
{% extends 'base.html' %}
{% load statics %}
{% block content %}
<h2>Form for Asset</h2>
{% endblock %}

View File

@ -0,0 +1,22 @@
{% extends "base.html" %}
{% load static %}
{% block title %}Asset: {{ asset.named_id }}{% endblock %}
{% block content %}
<div class="container">
<div>New Assets:</div>
{% if asset_list %}
<ul>
{% for asset in asset_list %}
<li><a href="{% url 'asset:asset-detail' asset.id %}">{{ asset.named_id }}: {{ asset.description }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No new assets are available.</p>
{% endif %}
</div>
<div>
</div>
{% endblock content %}

3
asset/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

11
asset/urls.py Normal file
View File

@ -0,0 +1,11 @@
from django.urls import path
from asset.views import AssetCreateView, AssetDeleteView, AssetUpdateView, AssetIndexView
app_name = 'asset'
urlpatterns = [
path('', AssetIndexView.as_view(), name='index'),
path('add/', AssetCreateView.as_view(), name='asset-add'),
path('<int:pk>/', AssetUpdateView.as_view(), name='asset-update'),
path('<int:pk>/delete/', AssetDeleteView.as_view(), name='asset-delete'),
]

25
asset/views.py Normal file
View File

@ -0,0 +1,25 @@
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView, DeleteView, UpdateView
from django.views import generic
from asset.models import Asset
class AssetCreateView(CreateView):
model = Asset
#fields = ['name']
class AssetUpdateView(UpdateView):
model = Asset
#fields = ['name']
class AssetDeleteView(DeleteView):
model = Asset
success_url = reverse_lazy('asset-list')
class AssetIndexView(generic.ListView):
template_name = 'asset/asset_index.html'
context_object_name = 'asset_list'
def get_queryset(self):
# Return the last 20 created containers
return Asset.objects.order_by('-created_ts')[:20]

View File

@ -31,6 +31,7 @@ ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'asset.apps.AssetConfig',
'container.apps.ContainerConfig',
'django.contrib.admin',
'django.contrib.auth',

View File

@ -6,6 +6,7 @@
<h2>This is HOME</h2>
<a href="{% url 'container:container_type_index' %}">Container Types</a><br>
<a href="{% url 'container:index' %}">Container</a><br>
<a href="{% url 'asset:index' %}">Assets</a><br>
{% endblock %}

View File

@ -26,5 +26,6 @@ urlpatterns = [
path('home/', TemplateView.as_view(template_name="homelog/home.html"), name='home'),
path('about/', TemplateView.as_view(template_name="homelog/about.html"), name='about'),
path('container/', include('container.urls')),
path('asset/', include('asset.urls')),
path('admin/', admin.site.urls),
]