Skip to content

Commit

Permalink
[Feature] Message can be edited
Browse files Browse the repository at this point in the history
  • Loading branch information
EliasBoulharts committed Mar 9, 2022
1 parent 4efb278 commit b8760c5
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 21 deletions.
4 changes: 2 additions & 2 deletions docs/dev/db-erd.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
36 changes: 36 additions & 0 deletions service_catalog/migrations/0005_auto_20220302_1819.py
Original file line number Diff line number Diff line change
@@ -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,
),
]
12 changes: 10 additions & 2 deletions service_catalog/models/message.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
4 changes: 4 additions & 0 deletions service_catalog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<int:request_id>/', views.request_details, name='request_details'),
path('request/<int:request_id>/comment/<int:comment_id>/', views.request_comment_edit,
name='request_comment_edit'),
path('request/<int:request_id>/edit/', views.request_edit, name='request_edit'),
path('request/<int:request_id>/cancel/', views.request_cancel, name='request_cancel'),
path('request/<int:request_id>/comment/', views.request_comment, name='request_comment'),
Expand Down Expand Up @@ -55,6 +57,8 @@
name='instance_new_support'),
path('instance/<int:instance_id>/support/<int:support_id>/', views.instance_support_details,
name='instance_support_details'),
path('instance/<int:instance_id>/support/<int:support_id>/message/<int:message_id>/', views.support_message_edit,
name='support_message_edit'),
path('instance/<int:instance_id>/edit/', views.instance_edit, name='instance_edit'),
path('instance/<int:instance_id>/delete/', views.instance_delete, name='instance_delete'),
path('instance/<int:instance_id>/archive/', views.instance_archive, name='instance_archive'),
Expand Down
29 changes: 29 additions & 0 deletions service_catalog/views/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=request.user,
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):
Expand Down
24 changes: 24 additions & 0 deletions service_catalog/views/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.user,
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')
Expand Down
25 changes: 15 additions & 10 deletions templates/service_catalog/common/instance-support-details.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{% include "generics/breadcrumbs.html" %}
</div>
<div class="col-sm-6">
<span title="state" class="float-sm-right badge bg-{{ support.state |map_support_state }} p-1 mr-2">{{ support.state }}</span>
<span title="state" class="float-sm-right badge bg-{{ support.state |map_support_state }} p-1 mr-2">{{ support.state }}</span>
</div>
</div>
</div><!-- /.container-fluid -->
Expand All @@ -26,9 +26,14 @@
<div class="user-block">
<img class="img-circle img-bordered-sm" src="{% if message.sender.is_superuser %}{% static '/squest/img/admin.png' %}{% else %}{% static '/squest/img/user.png' %}{% endif %}" alt="user image">
<span class="username">
<a href="#">{{ message.sender.username }}</a>
</span>
<span class="description">{{ message.date_message }}</span>
<a href="#">{{ message.sender.username }}</a>
<a title="Edit"
class="btn btn-primary float-right"
href="{% url "service_catalog:support_message_edit" message.support.instance.id message.support.id message.id %}">
<i class="fas fa-pencil-alt"></i>
</a>
</span>
<span class="description">{{ message.last_update_date }}</span>
</div>
<!-- /.user-block -->
<p>
Expand All @@ -47,14 +52,14 @@
<i class="fas fa-comment"></i> Comment
</button>
{% if support.state == "OPENED" %}
<button class="btn btn-danger" type="submit" name="btn_close">
<i class="fas fa-times-circle"></i> Close
</button>
<button class="btn btn-danger" type="submit" name="btn_close">
<i class="fas fa-times-circle"></i> Close
</button>
{% endif %}
{% if support.state == "CLOSED" %}
<button class="btn btn-success" type="submit" name="btn_re_open">
<i class="fas fa-undo"></i> Re-open
</button>
<button class="btn btn-success" type="submit" name="btn_re_open">
<i class="fas fa-undo"></i> Re-open
</button>
{% endif %}
</div>
</div>
Expand Down
13 changes: 9 additions & 4 deletions templates/service_catalog/common/request-comment.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h3 class="card-title">
Comments
</h3>
<div class="card-tools">
<span title="state" class="badge bg-{{ target_request.state |map_request_state }} p-1 mr-2">{{ target_request.state }}</span>
<span title="state" class="badge bg-{{ target_request.state |map_request_state }} p-1 mr-2">{{ target_request.state }}</span>
</div>
</div>
<div class="card-body">
Expand All @@ -32,9 +32,14 @@ <h3 class="card-title">
<div class="user-block">
<img class="img-circle img-bordered-sm" src="{% if message.sender.is_superuser %}{% static '/squest/img/admin.png' %}{% else %}{% static '/squest/img/user.png' %}{% endif %}" alt="user image">
<span class="username">
<a href="#">{{ message.sender.username }}</a>
</span>
<span class="description">{{ message.date_message }}</span>
<a href="#">{{ message.sender.username }}</a>
<a title="Edit"
class="btn btn-primary float-right"
href="{% url "service_catalog:request_comment_edit" message.request.id message.id %}">
<i class="fas fa-pencil-alt"></i>
</a>
</span>
<span class="description">{{ message.last_update_date }}</span>
</div>
<!-- /.user-block -->
<p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b8760c5

Please sign in to comment.