Related to https://github.com/IS-AgroSmart/AgroSmart-Web/issues/114, there is a hard limit on the number of images that a user can submit per month. Otherwise, a user could submit unlimited tasks to NodeODM Lightning and (since we pay NodeODM Lightning per image) cost us more money than his monthly payment.
Backend
[x] Add a remaining_images field on the User model, of type PositiveIntegerField, with a default value of 0.
[x] Optional idea: also add a image_quota field, same type, that defaults to 3000.
[x] Expose the remaining_images field on UserSerializer.
[x] Create a Django Admin action to manually reset a user's image quota. The action should reset user.remaining_images to user.image_quota (or 3000 if the image_quota is not implemented). Also call user.save()!
[x] Add tests:
[x] /upload-images returns 402 if user attempts to upload too many images (use users[1], check body of response)
[x] Images get substracted after successful upload
[x] Images get returned to the user after a failed flight
[x] Images DON'T get returned after a successful flight.
Frontend
On the frontend, we should add some client-side validation on the number of files selected, since there's no native way to do it :(
Related to https://github.com/IS-AgroSmart/AgroSmart-Web/issues/114, there is a hard limit on the number of images that a user can submit per month. Otherwise, a user could submit unlimited tasks to NodeODM Lightning and (since we pay NodeODM Lightning per image) cost us more money than his monthly payment.
Backend
remaining_images
field on the User model, of type PositiveIntegerField, with a default value of 0.image_quota
field, same type, that defaults to 3000.remaining_images
field on UserSerializer.upload_images
view, after L256, check iflen(request.FILES) <= user.remaining_images
. If not, raise an error HTTP 402, and returnHttpResponse(f"Subida fallida. Tiene un límite de ${user.remaining_images} imágenes.", status=402)
. https://github.com/IS-AgroSmart/AgroSmart-Web/blob/c226455c9dcc93948d737b1349666fae23449540/core/views.py#L249-L259HttpResponse("Subida fallida. Su almacenamiento está lleno.", status=402)
user.remaining_images -= len(request.FILES)
anduser.save()
).data["imagesCount"]
) from the NodeODM API) https://github.com/IS-AgroSmart/AgroSmart-Web/blob/7ee7507697b45996b04c43a55b5f7838b03e0b83/core/views.py#L277-L284invoice.paid
webhook, somehow parse the user from the response and reset hisremaining_images
to hisimage_quota
(or 3000 if theimage_quota
is not implemented) (see https://briancaffey.github.io/2021/01/02/using-the-stripe-api-for-recurring-monthly-saas-subscription-payments-in-django-and-vue-application/#iii-automatic-subscription-renewal)user.remaining_images
touser.image_quota
(or 3000 if theimage_quota
is not implemented). Also calluser.save()
!Frontend
On the frontend, we should add some client-side validation on the number of files selected, since there's no native way to do it :(
tooManyFiles() { return this.files.length > Math.min(this.$effectiveUser.remaining_images, 1000); }
. https://github.com/IS-AgroSmart/AgroSmart-Web/blob/c226455c9dcc93948d737b1349666fae23449540/frontend/src/components/UploadImages.vue#L32-L37:state="enoughFiles"
bit to be:state="enoughFiles && !tooManyFiles"
. Also change the error messages on the line below to include the case whentooManyFiles == true
. https://github.com/IS-AgroSmart/AgroSmart-Web/blob/c226455c9dcc93948d737b1349666fae23449540/frontend/src/components/UploadImages.vue#L7-L8if
branch change the body tothat.uploadError = error.response.data
https://github.com/IS-AgroSmart/AgroSmart-Web/blob/c226455c9dcc93948d737b1349666fae23449540/frontend/src/components/UploadImages.vue#L65-L71