Conversation
7e55003 to
4e0ca16
Compare
|
I am assuming that these are equivalent: diff --git a/tmp/before b/tmp/after
index 5cf0795e..1b38c590 100644
--- a/tmp/before
+++ b/tmp/after
@@ -1,27 +1,24 @@
- # A resource is selected that requires an account name selection but user has no account names
+ # Ensure user has account name if ALLOCATION_ACCOUNT_ENABLED
if (
ALLOCATION_ACCOUNT_ENABLED
and resource_obj.name in ALLOCATION_ACCOUNT_MAPPING
and AllocationAttributeType.objects.filter(name=ALLOCATION_ACCOUNT_MAPPING[resource_obj.name]).exists()
and not allocation_account
):
- form.add_error(
- None,
- format_html(
- 'You need to create an account name. Create it by clicking the link under the "Allocation account" field.'
- ),
+ raise ValidationError(
+ 'You need to create an account name. Create it by clicking the link under the "Allocation account" field.',
+ code="user_has_no_account_name",
)
- return self.form_invalid(form)
- allocation_limit_objs = resource_obj.resourceattribute_set.filter(
- resource_attribute_type__name="allocation_limit"
- ).first()
- if allocation_limit_objs:
- allocation_limit = int(allocation_limit_objs.value)
+ # Ensure this allocaiton wouldn't exceed the limit
+ allocation_limit = resource_obj.get_attribute("allocation_limit", typed=True)
+ if allocation_limit:
allocation_count = project_obj.allocation_set.filter(
resources=resource_obj,
status__name__in=["Active", "New", "Renewal Requested", "Paid", "Payment Pending", "Payment Requested"],
).count()
if allocation_count >= allocation_limit:
- form.add_error(None, format_html("Your project is at the allocation limit allowed for this resource."))
- return self.form_invalid(form)
+ raise ValidationError(
+ "Your project is at the allocation limit allowed for this resource.",
+ code="reached_allocation_limit",
+ ) |
|
I believe those two are equivalent (or approximately equivalent), yes. |
simonLeary42
left a comment
There was a problem hiding this comment.
LGTM, although my brain isn't quite big enough to keep track of all the noise and moved code, so I don't feel 100% confident.
|
Is there a high-level motivation for moving logic out of |
|
I've been reading https://docs.djangoproject.com/en/4.2/ref/forms/validation/ to try and wrap my head around all the different functions but it doesn't talk about |
Yes - The allocation status is also set/modified here because the status is an attribute in the
To me, |
|
Does |
|
It's called when the server receives a POST request to the view, so yes. |
Squahsed commits: - Use ModelForm for AllocationForm and CreateView for AllocationCreateView - Add 'allocation_account' to the fields list for AllocationForm - Allow initial values to be overriden Signed-off-by: Cecilia Lau <cecilialau6776@gmail.com>
2c13012 to
5da5387
Compare
|
Actually - I think it makes sense to override |
|
That sounds good but I don't really feel qualified to have an opinion on this. |
This is one in a series of PRs intended to clean up Allocation views in an attempt to better utilize Django's
ModelForms and generic views.