add deployment user to limit deployment
This commit is contained in:
		
							parent
							
								
									4e1a2ba20b
								
							
						
					
					
						commit
						50963324f3
					
				| @ -1,6 +1,10 @@ | |||||||
| from django.contrib import admin | from django.contrib import admin | ||||||
| 
 | 
 | ||||||
| from deployment.models import Deployment | from deployment.models import Deployment, DeploymentUser | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class DeploymentUserAdmin(admin.ModelAdmin): | ||||||
|  |     pass | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class DeploymentAdmin(admin.ModelAdmin): | class DeploymentAdmin(admin.ModelAdmin): | ||||||
| @ -8,3 +12,4 @@ class DeploymentAdmin(admin.ModelAdmin): | |||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| admin.site.register(Deployment, DeploymentAdmin) | admin.site.register(Deployment, DeploymentAdmin) | ||||||
|  | admin.site.register(DeploymentUser, DeploymentUserAdmin) | ||||||
|  | |||||||
| @ -9,9 +9,9 @@ def parse_channel(channel: str) -> tuple[str] | None: | |||||||
|     parts = channel.lstrip("deployment_").split("_") |     parts = channel.lstrip("deployment_").split("_") | ||||||
|     if len_part := len(parts): |     if len_part := len(parts): | ||||||
|         if len_part == 1: |         if len_part == 1: | ||||||
|             return (parts[0], "") |             return (int(parts[0]), "") | ||||||
|         if len_part == 2: |         if len_part == 2: | ||||||
|             return (parts[0], parts[1]) |             return (int(parts[0]), parts[1]) | ||||||
|     return |     return | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										25
									
								
								deployment/migrations/0002_deploymentuser.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								deployment/migrations/0002_deploymentuser.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,25 @@ | |||||||
|  | # Generated by Django 4.2.5 on 2023-09-23 14:21 | ||||||
|  | 
 | ||||||
|  | from django.conf import settings | ||||||
|  | import django.core.validators | ||||||
|  | from django.db import migrations, models | ||||||
|  | import django.db.models.deletion | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class Migration(migrations.Migration): | ||||||
|  | 
 | ||||||
|  |     dependencies = [ | ||||||
|  |         migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||||||
|  |         ('deployment', '0001_initial'), | ||||||
|  |     ] | ||||||
|  | 
 | ||||||
|  |     operations = [ | ||||||
|  |         migrations.CreateModel( | ||||||
|  |             name='DeploymentUser', | ||||||
|  |             fields=[ | ||||||
|  |                 ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||||||
|  |                 ('credits', models.SmallIntegerField(default=1, help_text='number of deployment allowed (-1 infinite)', validators=[django.core.validators.MinValueValidator(-1, message='Value must be greater than or equal to -1')])), | ||||||
|  |                 ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||||||
|  |             ], | ||||||
|  |         ), | ||||||
|  |     ] | ||||||
| @ -3,6 +3,7 @@ from typing import Any | |||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| from django.contrib.auth.models import User | from django.contrib.auth.models import User | ||||||
|  | from django.core.validators import MinValueValidator | ||||||
| from django.db import models | from django.db import models | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @ -28,6 +29,21 @@ class Status(Enum): | |||||||
|         return [(s.name, s.value) for s in cls] |         return [(s.name, s.value) for s in cls] | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | class DeploymentUser(models.Model): | ||||||
|  |     user = models.OneToOneField(User, on_delete=models.CASCADE) | ||||||
|  |     credits = models.SmallIntegerField( | ||||||
|  |         null=False, | ||||||
|  |         default=1, | ||||||
|  |         help_text="number of deployment allowed (-1 infinite)", | ||||||
|  |         validators=[ | ||||||
|  |             MinValueValidator(-1, message="Value must be greater than or equal to -1"), | ||||||
|  |         ], | ||||||
|  |     ) | ||||||
|  | 
 | ||||||
|  |     def __str__(self) -> str: | ||||||
|  |         return f"{self.user.username} - {self.credits}" | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| class Deployment(models.Model): | class Deployment(models.Model): | ||||||
|     id = models.UUIDField(primary_key=True) |     id = models.UUIDField(primary_key=True) | ||||||
|     name = models.TextField(null=False, blank=False) |     name = models.TextField(null=False, blank=False) | ||||||
|  | |||||||
| @ -2,11 +2,14 @@ from uuid import uuid4 | |||||||
| 
 | 
 | ||||||
| from celery.result import AsyncResult | from celery.result import AsyncResult | ||||||
| from celery.contrib.abortable import AbortableAsyncResult | from celery.contrib.abortable import AbortableAsyncResult | ||||||
|  | from django.contrib.auth.models import User | ||||||
|  | from django.core.exceptions import ObjectDoesNotExist | ||||||
| from django.core.paginator import Paginator | from django.core.paginator import Paginator | ||||||
| from django.http import ( | from django.http import ( | ||||||
|     HttpResponseRedirect, |     HttpResponseRedirect, | ||||||
|     HttpResponseServerError, |     HttpResponseServerError, | ||||||
|     HttpResponseBadRequest, |     HttpResponseBadRequest, | ||||||
|  |     HttpResponseForbidden, | ||||||
| ) | ) | ||||||
| from django.shortcuts import render, get_object_or_404 | from django.shortcuts import render, get_object_or_404 | ||||||
| 
 | 
 | ||||||
| @ -16,6 +19,21 @@ from deployment.models import Deployment, Status | |||||||
| from deployment.tasks import deploy as launch_deploy | from deployment.tasks import deploy as launch_deploy | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | def check_user_credits(user: User) -> bool: | ||||||
|  |     if not user.is_superuser: | ||||||
|  |         try: | ||||||
|  |             dep_user = user.deploymentuser | ||||||
|  |         except (ObjectDoesNotExist, AttributeError): | ||||||
|  |             return False | ||||||
|  |         else: | ||||||
|  |             if dep_user.credits < 0: | ||||||
|  |                 return True | ||||||
|  | 
 | ||||||
|  |             deployments = Deployment.objects.filter(user=user) | ||||||
|  |             return not len(deployments) >= dep_user.credits | ||||||
|  |     return True | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| def index(request): | def index(request): | ||||||
|     deployments = Deployment.objects.filter(user=request.user.id).order_by( |     deployments = Deployment.objects.filter(user=request.user.id).order_by( | ||||||
|         "-created_at" |         "-created_at" | ||||||
| @ -32,6 +50,7 @@ def index(request): | |||||||
|             "page_obj": page_obj, |             "page_obj": page_obj, | ||||||
|             "range_pages": (i + 1 for i in range(page_obj.paginator.num_pages)), |             "range_pages": (i + 1 for i in range(page_obj.paginator.num_pages)), | ||||||
|             "url": f"events/{request.user.id}/", |             "url": f"events/{request.user.id}/", | ||||||
|  |             "can_create": check_user_credits(request.user), | ||||||
|         }, |         }, | ||||||
|     ) |     ) | ||||||
| 
 | 
 | ||||||
| @ -109,6 +128,11 @@ def details(request, deployment_id): | |||||||
| 
 | 
 | ||||||
| def create(request): | def create(request): | ||||||
|     if request.method == "POST": |     if request.method == "POST": | ||||||
|  |         if not check_user_credits(request.user): | ||||||
|  |             return HttpResponseForbidden( | ||||||
|  |                 "unable to launch deployment, contact administrator for support" | ||||||
|  |             ) | ||||||
|  | 
 | ||||||
|         form = DeploymentForm(request.POST) |         form = DeploymentForm(request.POST) | ||||||
|         if form.is_valid(): |         if form.is_valid(): | ||||||
|             try: |             try: | ||||||
|  | |||||||
| @ -8,7 +8,8 @@ exclude = [ | |||||||
|     ".git", |     ".git", | ||||||
|     ".ruff_cache", |     ".ruff_cache", | ||||||
|     "venv", |     "venv", | ||||||
|     "settings.py" |     "settings.py", | ||||||
|  |     "deployment/migrations/*.py" | ||||||
| ] | ] | ||||||
| 
 | 
 | ||||||
| line-length = 88 | line-length = 88 | ||||||
|  | |||||||
| @ -58,9 +58,12 @@ | |||||||
|                         {% endfor %} |                         {% endfor %} | ||||||
|                     </table> |                     </table> | ||||||
|                     {% include 'pagination.html' %} |                     {% include 'pagination.html' %} | ||||||
|                     <button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#create-deployment-modal"> |                     <button type="button" {% if can_create %} class="btn btn-success" data-bs-toggle="modal" data-bs-target="#create-deployment-modal" {% else %} class="btn btn-danger" disabled {% endif %}> | ||||||
|                         Create |                         Create | ||||||
|                     </button>                   |                     </button> | ||||||
|  |                     {% if not can_create %} | ||||||
|  |                         <p style="font-style: italic;">You can't create new deployments, contact administrator for support.</p> | ||||||
|  |                     {% endif %} | ||||||
|                 {% else %} |                 {% else %} | ||||||
|                     <h4 style="text-align: center;">Please log in !</h4> |                     <h4 style="text-align: center;">Please log in !</h4> | ||||||
|                 {% endif %} |                 {% endif %} | ||||||
| @ -70,7 +73,9 @@ | |||||||
| {% endblock %} | {% endblock %} | ||||||
| 
 | 
 | ||||||
| {% block modal %} | {% block modal %} | ||||||
|     {% include 'deployment/create_modal.html' %} |     {% if can_create %} | ||||||
|  |         {% include 'deployment/create_modal.html' %} | ||||||
|  |     {% endif %} | ||||||
| {% endblock %} | {% endblock %} | ||||||
| 
 | 
 | ||||||
| {% block script %} | {% block script %} | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user