diff --git a/docs/dev/db-erd.md b/docs/dev/db-erd.md index e5135fae9..1292911f6 100644 --- a/docs/dev/db-erd.md +++ b/docs/dev/db-erd.md @@ -55,12 +55,12 @@ erDiagram } REQUEST_MESSAGE { - date date_message + date creation_date string content } SUPPORT_MESSAGE { - date date_message + date creation_date string content } diff --git a/service_catalog/migrations/0005_auto_20220302_1819.py b/service_catalog/migrations/0005_auto_20220302_1819.py new file mode 100644 index 000000000..d58b4e164 --- /dev/null +++ b/service_catalog/migrations/0005_auto_20220302_1819.py @@ -0,0 +1,36 @@ +# Generated by Django 3.2.12 on 2022-03-02 17:19 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('service_catalog', '0004_auto_20220228_1638'), + ] + + operations = [ + migrations.RenameField( + model_name='requestmessage', + old_name='date_message', + new_name='creation_date', + ), + migrations.RenameField( + model_name='supportmessage', + old_name='date_message', + new_name='creation_date', + ), + migrations.AddField( + model_name='requestmessage', + name='last_update_date', + field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now), + preserve_default=False, + ), + migrations.AddField( + model_name='supportmessage', + name='last_update_date', + field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now), + preserve_default=False, + ), + ] diff --git a/service_catalog/models/message.py b/service_catalog/models/message.py index 34cbf113e..acb59bf39 100644 --- a/service_catalog/models/message.py +++ b/service_catalog/models/message.py @@ -1,18 +1,26 @@ from django.contrib.auth.models import User from django.db import models +from django.db.models.signals import pre_save +from django.dispatch import receiver +from django.utils import timezone + from service_catalog.models import Request from service_catalog.models.support import Support class Message(models.Model): - sender = models.ForeignKey(User, blank=True, null=True, on_delete=models.CASCADE) - date_message = models.DateTimeField(auto_now_add=True) + creation_date = models.DateTimeField(auto_now_add=True) + last_update_date = models.DateTimeField(auto_now_add=True) content = models.TextField(null=False, blank=False, verbose_name="Message") class Meta: abstract = True + @receiver(pre_save) + def message_changed(sender, instance, **kwargs): + instance.last_update_date = timezone.now() + class RequestMessage(Message): request = models.ForeignKey(Request, diff --git a/service_catalog/urls.py b/service_catalog/urls.py index ac5d28910..b808dad84 100644 --- a/service_catalog/urls.py +++ b/service_catalog/urls.py @@ -22,6 +22,8 @@ path('request/delete-force/', views.request_bulk_delete, name='request_bulk_delete'), path('request/archived/', RequestArchivedListView.as_view(), name='request_archived_list'), path('request//', views.request_details, name='request_details'), + path('request//comment//', views.request_comment_edit, + name='request_comment_edit'), path('request//edit/', views.request_edit, name='request_edit'), path('request//cancel/', views.request_cancel, name='request_cancel'), path('request//comment/', views.request_comment, name='request_comment'), @@ -55,6 +57,8 @@ name='instance_new_support'), path('instance//support//', views.instance_support_details, name='instance_support_details'), + path('instance//support//message//', views.support_message_edit, + name='support_message_edit'), path('instance//edit/', views.instance_edit, name='instance_edit'), path('instance//delete/', views.instance_delete, name='instance_delete'), path('instance//archive/', views.instance_archive, name='instance_archive'), diff --git a/service_catalog/views/instance.py b/service_catalog/views/instance.py index 5b87d5d76..63afb1eeb 100644 --- a/service_catalog/views/instance.py +++ b/service_catalog/views/instance.py @@ -189,6 +189,35 @@ def instance_support_details(request, instance_id, support_id): return render(request, "service_catalog/common/instance-support-details.html", context) +@login_required +@permission_required_or_403('service_catalog.request_support_on_instance', (Instance, 'id', 'instance_id')) +def support_message_edit(request, instance_id, support_id, message_id): + support_message = get_object_or_404(SupportMessage, id=message_id) + if request.method == "POST": + form = SupportMessageForm(request.POST or None, request.FILES or None, sender=support_message.sender, + support=support_message.support, instance=support_message) + if form.is_valid(): + form.save() + return redirect('service_catalog:instance_support_details', support_message.support.instance.id, + support_message.support.id) + else: + form = SupportMessageForm(sender=request.user, support=support_message.support, instance=support_message) + context = { + 'form': form, + 'breadcrumbs': [ + {'text': 'Instances', 'url': reverse('service_catalog:instance_list')}, + {'text': f"{support_message.support.instance.name} ({support_message.support.instance.id})", + 'url': reverse('service_catalog:instance_details', args=[instance_id])}, + {'text': 'Support', 'url': ""}, + {'text': support_message.support.title, + 'url': reverse('service_catalog:instance_support_details', + kwargs={'instance_id': instance_id, 'support_id': support_id})}, + ], + 'action': 'edit' + } + return render(request, "generics/generic_form.html", context) + + @login_required @permission_required_or_403('service_catalog.view_instance', (Instance, 'id', 'instance_id')) def instance_details(request, instance_id): diff --git a/service_catalog/views/request.py b/service_catalog/views/request.py index 57c8d7b51..9c10a7f3d 100644 --- a/service_catalog/views/request.py +++ b/service_catalog/views/request.py @@ -72,6 +72,30 @@ def request_comment(request, request_id): return render(request, "service_catalog/common/request-comment.html", context) +@login_required +@permission_required_or_403('service_catalog.comment_request', (Request, 'id', 'request_id')) +def request_comment_edit(request, request_id, comment_id): + request_comment = get_object_or_404(RequestMessage, id=comment_id) + if request.method == "POST": + form = RequestMessageForm(request.POST or None, request.FILES or None, sender=request_comment.sender, + target_request=request_comment.request, instance=request_comment) + if form.is_valid(): + form.save() + return redirect('service_catalog:request_comment', request_id) + else: + form = RequestMessageForm(sender=request.user, target_request=request_comment.request, instance=request_comment) + context = { + 'form': form, + 'breadcrumbs': [ + {'text': 'Requests', 'url': reverse('service_catalog:request_list')}, + {'text': request_id, 'url': reverse('service_catalog:request_details', + kwargs={'request_id': request_id})}, + ], + 'action': 'edit' + } + return render(request, "generics/generic_form.html", context) + + @login_required def request_details(request, request_id): request_list = get_objects_for_user(request.user, 'service_catalog.view_request') diff --git a/templates/service_catalog/common/instance-support-details.html b/templates/service_catalog/common/instance-support-details.html index 11b74bdb9..85b525bb0 100644 --- a/templates/service_catalog/common/instance-support-details.html +++ b/templates/service_catalog/common/instance-support-details.html @@ -10,7 +10,7 @@ {% include "generics/breadcrumbs.html" %}
- {{ support.state }} + {{ support.state }}
@@ -26,9 +26,16 @@
user image - {{ message.sender.username }} - - {{ message.date_message }} + {{ message.sender.username }} + {% if request.user.is_superuser or message.sender == request.user %} + + + + {% endif %} + + {{ message.last_update_date }}

@@ -47,14 +54,14 @@ Comment {% if support.state == "OPENED" %} - + {% endif %} {% if support.state == "CLOSED" %} - + {% endif %} diff --git a/templates/service_catalog/common/request-comment.html b/templates/service_catalog/common/request-comment.html index a3e15de66..b9305387c 100644 --- a/templates/service_catalog/common/request-comment.html +++ b/templates/service_catalog/common/request-comment.html @@ -23,7 +23,7 @@

Comments

- {{ target_request.state }} + {{ target_request.state }}
@@ -32,9 +32,16 @@

user image - {{ message.sender.username }} - - {{ message.date_message }} + {{ message.sender.username }} + {% if request.user.is_superuser or message.sender == request.user %} + + + + {% endif %} + + {{ message.last_update_date }}

diff --git a/tests/test_service_catalog/test_api/test_request/test_request_state_machine/test_need_info.py b/tests/test_service_catalog/test_api/test_request/test_request_state_machine/test_need_info.py index 5ecbe5f2a..34da9fecf 100644 --- a/tests/test_service_catalog/test_api/test_request/test_request_state_machine/test_need_info.py +++ b/tests/test_service_catalog/test_api/test_request/test_request_state_machine/test_need_info.py @@ -19,7 +19,7 @@ def _need_info(self, status=200, data=None): if status == 200: self.test_request.refresh_from_db() self.assertEqual(self.test_request.state, RequestState.NEED_INFO) - last_message = self.test_request.comments.order_by("-date_message").first() + last_message = self.test_request.comments.order_by("-creation_date").first() if data: self.assertEqual(data["content"], last_message.content) self.assertEqual(response.wsgi_request.user, last_message.sender) diff --git a/tests/test_service_catalog/test_api/test_request/test_request_state_machine/test_re_submit.py b/tests/test_service_catalog/test_api/test_request/test_request_state_machine/test_re_submit.py index 026825977..2560b95fb 100644 --- a/tests/test_service_catalog/test_api/test_request/test_request_state_machine/test_re_submit.py +++ b/tests/test_service_catalog/test_api/test_request/test_request_state_machine/test_re_submit.py @@ -19,7 +19,7 @@ def _re_submit(self, status=200, data=None): if status == 200: self.test_request.refresh_from_db() self.assertEqual(self.test_request.state, RequestState.SUBMITTED) - last_message = self.test_request.comments.order_by("-date_message").first() + last_message = self.test_request.comments.order_by("-creation_date").first() if data: self.assertEqual(data["content"], last_message.content) self.assertEqual(response.wsgi_request.user, last_message.sender) diff --git a/tests/test_service_catalog/test_api/test_request/test_request_state_machine/test_reject.py b/tests/test_service_catalog/test_api/test_request/test_request_state_machine/test_reject.py index 68a5886b2..0c79b2510 100644 --- a/tests/test_service_catalog/test_api/test_request/test_request_state_machine/test_reject.py +++ b/tests/test_service_catalog/test_api/test_request/test_request_state_machine/test_reject.py @@ -19,7 +19,7 @@ def _reject(self, status=200, data=None): if status == 200: self.test_request.refresh_from_db() self.assertEqual(self.test_request.state, RequestState.REJECTED) - last_message = self.test_request.comments.order_by("-date_message").first() + last_message = self.test_request.comments.order_by("-creation_date").first() if data: self.assertEqual(data["content"], last_message.content) self.assertEqual(response.wsgi_request.user, last_message.sender)