Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sponsorship - adding renewal option for contract generation #2344

Merged
merged 6 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sponsors/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ class SponsorshipReviewAdminForm(forms.ModelForm):
start_date = forms.DateField(widget=AdminDateWidget(), required=False)
end_date = forms.DateField(widget=AdminDateWidget(), required=False)
overlapped_by = forms.ModelChoiceField(queryset=Sponsorship.objects.select_related("sponsor", "package"), required=False)
renewal = forms.CheckboxInput()

def __init__(self, *args, **kwargs):
force_required = kwargs.pop("force_required", False)
Expand All @@ -404,6 +405,7 @@ def __init__(self, *args, **kwargs):
for field_name in self.fields:
self.fields[field_name].required = True


class Meta:
model = Sponsorship
fields = ["start_date", "end_date", "package", "sponsorship_fee"]
Expand All @@ -415,6 +417,7 @@ def clean(self):
cleaned_data = super().clean()
start_date = cleaned_data.get("start_date")
end_date = cleaned_data.get("end_date")
renewal = cleaned_data.get("renewal")

if start_date and end_date and end_date <= start_date:
raise forms.ValidationError("End date must be greater than start date")
Expand Down
18 changes: 18 additions & 0 deletions sponsors/migrations/0097_sponsorship_renewal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.24 on 2023-12-18 16:23

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('sponsors', '0096_auto_20231214_2108'),
]

operations = [
migrations.AddField(
model_name='sponsorship',
name='renewal',
field=models.BooleanField(blank=True, null=True),
),
]
3 changes: 2 additions & 1 deletion sponsors/models/sponsorship.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class Meta(OrderedModel.Meta):

class Sponsorship(models.Model):
"""
Represente a sponsorship application by a sponsor.
Represents a sponsorship application by a sponsor.
It's responsible to group the set of selected benefits and
link it to sponsor
"""
Expand Down Expand Up @@ -182,6 +182,7 @@ class Sponsorship(models.Model):
package = models.ForeignKey(SponsorshipPackage, null=True, on_delete=models.SET_NULL)
sponsorship_fee = models.PositiveIntegerField(null=True, blank=True)
overlapped_by = models.ForeignKey("self", null=True, on_delete=models.SET_NULL)
renewal = models.BooleanField(null=True, blank=True)

assets = GenericRelation(GenericAsset)

Expand Down
9 changes: 7 additions & 2 deletions sponsors/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def _contract_context(contract, **context):
"sponsorship": contract.sponsorship,
"benefits": _clean_split(contract.benefits_list.raw),
"legal_clauses": _clean_split(contract.legal_clauses.raw),
"renewal": contract.sponsorship.renewal,
})
return context

Expand All @@ -49,9 +50,13 @@ def render_contract_to_pdf_file(contract, **context):


def _gen_docx_contract(output, contract, **context):
template = os.path.join(settings.TEMPLATES_DIR, "sponsors", "admin", "contract-template.docx")
doc = DocxTemplate(template)
context = _contract_context(contract, **context)
renewal = context["renewal"]
if renewal:
template = os.path.join(settings.TEMPLATES_DIR, "sponsors", "admin", "renewal-contract-template.docx")
else:
template = os.path.join(settings.TEMPLATES_DIR, "sponsors", "admin", "contract-template.docx")
doc = DocxTemplate(template)
doc.render(context)
doc.save(output)
return output
Expand Down
36 changes: 36 additions & 0 deletions sponsors/tests/test_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def setUp(self):
"sponsorship": self.contract.sponsorship,
"benefits": [],
"legal_clauses": [],
"renewal": None,
}
self.template = "sponsors/admin/preview-contract.html"

Expand Down Expand Up @@ -71,3 +72,38 @@ def test_render_response_with_docx_attachment(self, MockDocxTemplate):
response.get("Content-Type"),
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)

@patch("sponsors.pdf.DocxTemplate")
def test_render_response_with_docx_attachment__renewal(self, MockDocxTemplate):
renewal_contract = baker.make_recipe("sponsors.tests.empty_contract", sponsorship__start_date=date.today(),
sponsorship__renewal=True)
text = f"{renewal_contract.benefits_list.raw}\n\n**Legal Clauses**\n{renewal_contract.legal_clauses.raw}"
html = render_md(text)
renewal_context = {
"contract": renewal_contract,
"start_date": renewal_contract.sponsorship.start_date,
"start_day_english_suffix": format(self.contract.sponsorship.start_date, "S"),
"sponsor": renewal_contract.sponsorship.sponsor,
"sponsorship": renewal_contract.sponsorship,
"benefits": [],
"legal_clauses": [],
"renewal": True,
}
renewal_template = "sponsors/admin/preview-contract.html"

template = Path(settings.TEMPLATES_DIR) / "sponsors" / "admin" / "renewal-contract-template.docx"
self.assertTrue(template.exists())
mocked_doc = Mock(DocxTemplate)
MockDocxTemplate.return_value = mocked_doc

request = Mock(HttpRequest)
response = render_contract_to_docx_response(request, renewal_contract)

MockDocxTemplate.assert_called_once_with(str(template.resolve()))
mocked_doc.render.assert_called_once_with(renewal_context)
mocked_doc.save.assert_called_once_with(response)
self.assertEqual(response.get("Content-Disposition"), "attachment; filename=contract.docx")
self.assertEqual(
response.get("Content-Type"),
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
18 changes: 18 additions & 0 deletions sponsors/tests/test_use_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ def test_update_sponsorship_as_approved_and_create_contract(self):
self.assertEqual(self.sponsorship.sponsorship_fee, 100)
self.assertEqual(self.sponsorship.package, self.package)
self.assertEqual(self.sponsorship.level_name, self.package.name)
self.assertFalse(self.sponsorship.renewal)


def test_update_renewal_sponsorship_as_approved_and_create_contract(self):
ewdurbin marked this conversation as resolved.
Show resolved Hide resolved
self.data.update({"renewal": True})
self.use_case.execute(self.sponsorship, **self.data)
self.sponsorship.refresh_from_db()

today = timezone.now().date()
self.assertEqual(self.sponsorship.approved_on, today)
self.assertEqual(self.sponsorship.status, Sponsorship.APPROVED)
self.assertTrue(self.sponsorship.contract.pk)
self.assertTrue(self.sponsorship.start_date)
self.assertTrue(self.sponsorship.end_date)
self.assertEqual(self.sponsorship.sponsorship_fee, 100)
self.assertEqual(self.sponsorship.package, self.package)
self.assertEqual(self.sponsorship.level_name, self.package.name)
self.assertEqual(self.sponsorship.renewal, True)

def test_send_notifications_using_sponsorship(self):
self.use_case.execute(self.sponsorship, **self.data)
Expand Down
3 changes: 3 additions & 0 deletions sponsors/use_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ def execute(self, sponsorship, start_date, end_date, **kwargs):
sponsorship.approve(start_date, end_date)
package = kwargs.get("package")
fee = kwargs.get("sponsorship_fee")
renewal = kwargs.get("renewal", False)
if package:
sponsorship.package = package
sponsorship.level_name = package.name
if fee:
sponsorship.sponsorship_fee = fee
if renewal:
sponsorship.renewal = True

sponsorship.save()
contract = Contract.new(sponsorship)
Expand Down
3 changes: 2 additions & 1 deletion templates/sponsors/admin/approve_application.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ <h1>Generate Contract for Signing</h1>
{{ form.as_p }}

<input name="confirm" value="yes" style="display:none">

<input type="checkbox" id="renewal" name="renewal" value="renewal">
<label for="renewal">Renewal of a previous sponsorship</label><br>
<div class="submit-row">
<input type="submit" value="Approve" class="default">
</div>
Expand Down
Binary file not shown.
Loading