feat(itim): Ability to add and configure Cluster Types

ref: #244 #71
This commit is contained in:
2024-08-18 13:49:42 +09:30
parent b65e577017
commit 75203c022a
10 changed files with 405 additions and 21 deletions

View File

@ -0,0 +1,84 @@
from django import forms
from django.forms import ValidationError
from django.urls import reverse
from itim.models.clusters import ClusterType
from app import settings
from core.forms.common import CommonModelForm
class ClusterTypeForm(CommonModelForm):
class Meta:
fields = '__all__'
model = ClusterType
prefix = 'cluster_type'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class DetailForm(ClusterTypeForm):
tabs: dict = {
"details": {
"name": "Details",
"slug": "details",
"sections": [
{
"layout": "double",
"left": [
'name',
'organization',
'c_created',
'c_modified'
],
"right": [
'model_notes',
]
},
]
},
"notes": {
"name": "Notes",
"slug": "notes",
"sections": []
}
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['c_created'] = forms.DateTimeField(
label = 'Created',
input_formats=settings.DATETIME_FORMAT,
disabled = True,
initial = self.instance.created,
)
self.fields['c_modified'] = forms.DateTimeField(
label = 'Modified',
input_formats=settings.DATETIME_FORMAT,
disabled = True,
initial = self.instance.modified,
)
self.tabs['details'].update({
"edit_url": reverse('Settings:_cluster_type_change', args=(self.instance.pk,))
})
self.url_index_view = reverse('Settings:_cluster_types')

View File

@ -29,8 +29,8 @@ class Migration(migrations.Migration):
('organization', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='access.organization', validators=[access.models.TenancyObject.validatate_organization_exists])),
],
options={
'verbose_name': 'ClusterType',
'verbose_name_plural': 'ClusterTypes',
'verbose_name': 'Cluster Type',
'verbose_name_plural': 'Cluster Types',
'ordering': ['name'],
},
),

View File

@ -0,0 +1,25 @@
# Generated by Django 5.0.7 on 2024-08-18 03:57
import access.fields
import django.utils.timezone
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('itim', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='clustertype',
name='created',
field=access.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False),
),
migrations.AddField(
model_name='clustertype',
name='modified',
field=access.fields.AutoLastModifiedField(default=django.utils.timezone.now, editable=False),
),
]

View File

@ -18,9 +18,9 @@ class ClusterType(TenancyObject):
'name',
]
verbose_name = "ClusterType"
verbose_name = "Cluster Type"
verbose_name_plural = "ClusterTypes"
verbose_name_plural = "Cluster Types"
id = models.AutoField(
@ -39,6 +39,15 @@ class ClusterType(TenancyObject):
slug = AutoSlugField()
created = AutoCreatedField()
modified = AutoLastModifiedField()
def __str__(self):
return self.name
class Cluster(TenancyObject):

View File

@ -0,0 +1,22 @@
{% extends 'detail.html.j2' %}
{% load json %}
{% load markdown %}
{% block tabs %}
<div id="details" class="content-tab">
{% include 'content/section.html.j2' with tab=form.tabs.details %}
</div>
<div id="notes" class="content-tab">
{% include 'content/section.html.j2' with tab=form.tabs.notes %}
</div>
{% endblock %}

View File

@ -0,0 +1,47 @@
{% extends 'base.html.j2' %}
{% block content %}
<input type="button" value="New Cluster Type" onclick="window.location='{% url 'Settings:_cluster_type_add' %}';">
<table class="data">
<tr>
<th>Title</th>
<th>&nbsp;</th>
<th>Organization</th>
<th>&nbsp;</th>
</tr>
{% if items %}
{% for item in items %}
<tr>
<td><a href="{% url 'Settings:_cluster_type_view' pk=item.id %}">{{ item.name }}</a></td>
<td>&nbsp;</td>
<td>{{ item.organization }}</td>
<td>&nbsp;</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td colspan="4">Nothing Found</td>
</tr>
{% endif %}
</table>
<br>
<div class="pagination">
<span class="step-links">
{% if page_obj.has_previous %}
<a href="?page=1">&laquo; first</a>
<a href="?page={{ page_obj.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">next</a>
<a href="?page={{ page_obj.paginator.num_pages }}">last &raquo;</a>
{% endif %}
</span>
</div>
{% endblock %}

View File

@ -0,0 +1,185 @@
from django.contrib.auth import decorators as auth_decorator
from django.urls import reverse
from django.utils.decorators import method_decorator
from core.forms.comment import AddNoteForm
from core.models.notes import Notes
from core.views.common import AddView, ChangeView, DeleteView, IndexView
from itim.forms.cluster_type import ClusterTypeForm, DetailForm
from itim.models.clusters import ClusterType
from settings.models.user_settings import UserSettings
class Add(AddView):
form_class = ClusterTypeForm
model = ClusterType
permission_required = [
'itim.add_clustertype',
]
def get_initial(self):
initial: dict = {
'organization': UserSettings.objects.get(user = self.request.user).default_organization
}
if 'pk' in self.kwargs:
if self.kwargs['pk']:
initial.update({'parent': self.kwargs['pk']})
self.model.parent.field.hidden = True
return initial
def get_success_url(self, **kwargs):
return reverse('Settings:_cluster_type_types')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['content_title'] = 'New Cluster Type'
return context
class Change(ChangeView):
form_class = ClusterTypeForm
model = ClusterType
permission_required = [
'itim.change_clustertype',
]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['content_title'] = str(self.object)
return context
def get_success_url(self, **kwargs):
return reverse('Settings:_cluster_type_view', args=(self.kwargs['pk'],))
class Delete(DeleteView):
model = ClusterType
permission_required = [
'itim.delete_clustertype',
]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['content_title'] = 'Delete ' + str(self.object)
return context
def get_success_url(self, **kwargs):
return reverse('Settings:_cluster_types')
class Index(IndexView):
context_object_name = "items"
model = ClusterType
paginate_by = 10
permission_required = [
'itim.view_clustertype'
]
template_name = 'itim/cluster_type_index.html.j2'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['model_docs_path'] = self.model._meta.app_label + '/' + self.model._meta.model_name
context['content_title'] = 'Cluster Types'
return context
class View(ChangeView):
context_object_name = "item"
form_class = DetailForm
model = ClusterType
permission_required = [
'itim.view_clustertype',
]
template_name = 'itim/cluster_type.html.j2'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['notes_form'] = AddNoteForm(prefix='note')
context['notes'] = Notes.objects.filter(service=self.kwargs['pk'])
context['model_pk'] = self.kwargs['pk']
context['model_name'] = self.model._meta.model_name
context['model_delete_url'] = reverse('Settings:_cluster_type_delete', args=(self.kwargs['pk'],))
context['content_title'] = self.object.name
return context
def post(self, request, *args, **kwargs):
item = ClusterType.objects.get(pk=self.kwargs['pk'])
notes = AddNoteForm(request.POST, prefix='note')
if notes.is_bound and notes.is_valid() and notes.instance.note != '':
notes.instance.service = item
notes.instance.organization = item.organization
notes.save()
def get_success_url(self, **kwargs):
return reverse('Settings:_cluster_type_view', args=(self.kwargs['pk'],))