Skip to content

Commit

Permalink
feat: short URL for pre-compiled tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
francesco-filicetti committed Feb 3, 2023
1 parent aeb3f9c commit 80035a1
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 3 deletions.
22 changes: 22 additions & 0 deletions uniticket/uni_ticket/migrations/0005_compiledticket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2.15 on 2023-02-03 07:48

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('uni_ticket', '0004_alter_ticketcategorywsprotocollo_protocollo_uo'),
]

operations = [
migrations.CreateModel(
name='CompiledTicket',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('url_path', models.CharField(max_length=255)),
('content', models.TextField()),
('created', models.DateTimeField(auto_now_add=True)),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.15 on 2023-02-03 07:51

from django.db import migrations, models
import uuid


class Migration(migrations.Migration):

dependencies = [
('uni_ticket', '0005_compiledticket'),
]

operations = [
migrations.AlterField(
model_name='compiledticket',
name='url_path',
field=models.CharField(default=uuid.uuid4, max_length=255, unique=True),
),
]
18 changes: 18 additions & 0 deletions uniticket/uni_ticket/migrations/0007_compiledticket_one_time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.15 on 2023-02-03 09:08

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('uni_ticket', '0006_alter_compiledticket_url_path'),
]

operations = [
migrations.AddField(
model_name='compiledticket',
name='one_time',
field=models.BooleanField(default=False),
),
]
24 changes: 24 additions & 0 deletions uniticket/uni_ticket/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
NEW_TICKET_CREATED_ALERT,
ORGANIZATION_EMPLOYEE_LABEL,
ORGANIZATION_USER_LABEL,
PRECOMPILED_TICKET_EXPIRE_DAYS,
PRIORITY_LEVELS,
SHOW_HEADING_TEXT,
TICKET_ATTACHMENT_FOLDER,
Expand All @@ -48,6 +49,10 @@
from .validators import *


PRECOMPILED_TICKET_EXPIRE_DAYS = getattr(settings,
'PRECOMPILED_TICKET_EXPIRE_DAYS',
PRECOMPILED_TICKET_EXPIRE_DAYS)

logger = logging.getLogger("__name__")


Expand Down Expand Up @@ -1608,3 +1613,22 @@ class Meta:

def __str__(self):
return "{} - {}".format(self.name, self.organizational_structure)


class CompiledTicket(models.Model):
url_path = models.CharField(max_length=255, unique=True, default=uuid.uuid4)
content = models.TextField()
one_time = models.BooleanField(default=False)
created = models.DateTimeField(auto_now_add=True)

@staticmethod
def clear():
""" """
if PRECOMPILED_TICKET_EXPIRE_DAYS:
to_clear = []
precompiled_tickets = CompiledTicket.objects.all()
for precompiled in precompiled_tickets:
if (timezone.now() - precompiled.created).days >= PRECOMPILED_TICKET_EXPIRE_DAYS:
to_clear.append(precompiled.pk)
entries_to_clean = CompiledTicket.objects.filter(pk__in=to_clear)
entries_to_clean.delete()
2 changes: 2 additions & 0 deletions uniticket/uni_ticket/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,3 +507,5 @@
)

STATS_SHOW_TICKETS_BY_USER = True

PRECOMPILED_TICKET_EXPIRE_DAYS = 7
18 changes: 15 additions & 3 deletions uniticket/uni_ticket/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,18 @@ def get_assets(self, structure_slug, category_slug) -> None:
def get_modulo_and_form(self) -> None:
# if there is an encrypted token with ticket params in URL
if self.request.GET.get("import"):
CompiledTicket.clear()
encoded_data = self.request.GET["import"]
try:
compiled_ticket = get_object_or_404(CompiledTicket, url_path=encoded_data)
except Exception:
return custom_message(self.request, _("Il ticket precompilato è scaduto"))
try:
# decrypt and get imported form content
imported_data = json.loads(decrypt_from_jwe(encoded_data))
imported_data = json.loads(decrypt_from_jwe(compiled_ticket.content))
# one time
if compiled_ticket.one_time:
compiled_ticket.delete()
except Exception:
return custom_message(self.request, _("Dati da importare non consistenti."))
# get input_module id from imported data
Expand Down Expand Up @@ -613,7 +621,9 @@ def post(self, request, structure_slug, category_slug, api=False):
)
)
# build url to display in message
url = base_url + "?import=" + encrypted_data
compiled_ticket = CompiledTicket.objects.create(content=encrypted_data)
# url = base_url + "?import=" + encrypted_data
url = f"{base_url}?import={compiled_ticket.url_path}"
messages.add_message(
request,
messages.SUCCESS,
Expand Down Expand Up @@ -1623,14 +1633,16 @@ def ticket_clone(request, ticket_id):

# build encrypted url param with form data
encrypted_data = encrypt_to_jwe(json.dumps(form_data).encode())
compiled_ticket = CompiledTicket.objects.create(content=encrypted_data,
one_time=True)
base_url = reverse(
"uni_ticket:add_new_ticket",
kwargs={
"structure_slug": category.organizational_structure.slug,
"category_slug": category.slug,
},
)
return HttpResponseRedirect(base_url + "?import={}".format(encrypted_data))
return HttpResponseRedirect(f"{base_url}?import={compiled_ticket.url_path}")


@login_required
Expand Down

0 comments on commit 80035a1

Please sign in to comment.