Skip to content

Commit

Permalink
#679: add parent_contract & description to ContractForm, use autocomp…
Browse files Browse the repository at this point in the history
…lete for contracts
  • Loading branch information
Fasand committed Jul 4, 2024
1 parent 479ac50 commit 1e08db9
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var/
*.egg-info/
.installed.cfg
*.egg
.cronenv

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down
8 changes: 7 additions & 1 deletion Seeder/contracts/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from . import models
from contracts.constants import CREATIVE_COMMONS_TYPES_CHOICES

from dal import autocomplete
from django import forms


Expand All @@ -9,10 +10,15 @@ class Meta:
model = models.Contract
fields = ('state', 'year', 'contract_number', 'valid_from', 'valid_to',
'contract_file', 'creative_commons_type',
'parent_contract', 'description',
'in_communication')
widgets = {
'creative_commons_type': forms.Select(
choices=CREATIVE_COMMONS_TYPES_CHOICES)
choices=CREATIVE_COMMONS_TYPES_CHOICES),
'description': forms.Textarea(attrs={"rows": 2}),
'parent_contract': autocomplete.ModelSelect2(
url='contracts:autocomplete',
),
}


Expand Down
19 changes: 19 additions & 0 deletions Seeder/contracts/migrations/0007_auto_20240704_1041.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.2.28 on 2024-07-04 10:41

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('contracts', '0006_auto_20201207_1653'),
]

operations = [
migrations.AlterField(
model_name='contract',
name='parent_contract',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='sub_contracts', to='contracts.Contract', verbose_name='Parent'),
),
]
1 change: 1 addition & 0 deletions Seeder/contracts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class Contract(BaseModel):

parent_contract = models.ForeignKey(
'self',
verbose_name=_("Parent"),
null=True,
blank=True,
on_delete=models.DO_NOTHING,
Expand Down
1 change: 1 addition & 0 deletions Seeder/contracts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

urlpatterns = [
path('<int:pk>/detail', Detail.as_view(), name='detail'),
path('autocomplete', ContractAutocomplete.as_view(), name='autocomplete'),
path('<int:pk>/create', Create.as_view(), name='create'),
path('<int:pk>/assign', Assign.as_view(), name='assign'),
path('<int:pk>/edit', Edit.as_view(), name='edit'),
Expand Down
24 changes: 24 additions & 0 deletions Seeder/contracts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
from . import constants

from datetime import timedelta, date
from dal import autocomplete

from django.db.models.functions import Cast
from django.db.models import CharField
from django.views.generic import DetailView, FormView
from django.utils.translation import ugettext_lazy as _
from django.template.loader import render_to_string
Expand All @@ -31,6 +34,27 @@ class Detail(ContractView, DetailView, CommentViewGeneric):
template_name = 'contract.html'


class ContractAutocomplete(autocomplete.Select2QuerySetView):

def get_queryset(self):
if not self.request.user.is_authenticated:
return models.Contract.objects.none()
# Annotate as strings so we can use icontains
qs = models.Contract.objects.all().annotate(
contract_number_s=Cast("contract_number", output_field=CharField()),
year_s=Cast("year", output_field=CharField())
).order_by("contract_number", "year")
# Allow searching by "{contract_number} / {year}"
if self.q:
q_split = str(self.q or "").replace(" ", "").split("/")
if len(q_split) == 1:
qs = qs.filter(contract_number_s__icontains=q_split[0])
elif len(q_split) == 2:
qs = qs.filter(contract_number_s__icontains=q_split[0],
year_s__icontains=q_split[1])
return qs


class Create(LoginMixin, FormView, ObjectMixinFixed):
"""
Create contract based on existing source
Expand Down

0 comments on commit 1e08db9

Please sign in to comment.