Skip to content

Commit

Permalink
WIP renewal work
Browse files Browse the repository at this point in the history
  • Loading branch information
jessiebelle committed Dec 16, 2023
1 parent 7ba7d36 commit 8928f59
Show file tree
Hide file tree
Showing 581 changed files with 209,663 additions and 4 deletions.
2 changes: 2 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 @@ -415,6 +416,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-16 19:14

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(default=False),
),
]
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(default=False)

assets = GenericRelation(GenericAsset)

Expand Down
10 changes: 8 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,14 @@ 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)
# context = _contract_context(contract, **context)
doc.render(context)
doc.save(output)
return output
Expand Down
21 changes: 21 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": False,
}
self.template = "sponsors/admin/preview-contract.html"

Expand Down Expand Up @@ -71,3 +72,23 @@ 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):
self.context.update({"renewal": True})
template = Path(settings.TEMPLATES_DIR) / "sponsors" / "admin" / "contract-renewal-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, self.contract)

MockDocxTemplate.assert_called_once_with(str(template.resolve()))
mocked_doc.render.assert_called_once_with(self.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.assertEqual(self.sponsorship.renewal, False)


def test_update_renewal_sponsorship_as_approved_and_create_contract(self):
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 = renewal

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

0 comments on commit 8928f59

Please sign in to comment.