Mudanza de repo
This commit is contained in:
0
api/licence/__init__.py
Normal file
0
api/licence/__init__.py
Normal file
10
api/licence/admin.py
Normal file
10
api/licence/admin.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from django.contrib import admin
|
||||
from .models import Licencia
|
||||
# Register your models here.
|
||||
|
||||
class LicenciaAdmin(admin.ModelAdmin):
|
||||
list_display = ('id', 'nombre')
|
||||
search_fields = ('nombre',)
|
||||
list_filter = ('nombre',)
|
||||
|
||||
admin.site.register(Licencia, LicenciaAdmin)
|
||||
6
api/licence/apps.py
Normal file
6
api/licence/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class LicenceConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'api.licence'
|
||||
28
api/licence/migrations/0001_initial.py
Normal file
28
api/licence/migrations/0001_initial.py
Normal file
@@ -0,0 +1,28 @@
|
||||
# Generated by Django 5.2.3 on 2025-07-14 16:14
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Licencia',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('nombre', models.CharField(max_length=100)),
|
||||
('descripcion', models.TextField(blank=True, null=True)),
|
||||
('almacenamiento', models.PositiveIntegerField(default=0)),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Licencia',
|
||||
'verbose_name_plural': 'Licencias',
|
||||
'db_table': 'licencia',
|
||||
},
|
||||
),
|
||||
]
|
||||
0
api/licence/migrations/__init__.py
Normal file
0
api/licence/migrations/__init__.py
Normal file
20
api/licence/models.py
Normal file
20
api/licence/models.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
class Licencia(models.Model):
|
||||
# Define the Licencia model minimally for ForeignKey reference
|
||||
# Add fields as needed
|
||||
nombre = models.CharField(max_length=100)
|
||||
descripcion = models.TextField(blank=True, null=True)
|
||||
almacenamiento = models.PositiveIntegerField(default=0, blank=False, null=False) # in GB
|
||||
|
||||
|
||||
class Meta:
|
||||
verbose_name = "Licencia"
|
||||
verbose_name_plural = "Licencias"
|
||||
db_table = 'licencia'
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.nombre
|
||||
|
||||
9
api/licence/serializers.py
Normal file
9
api/licence/serializers.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from .models import Licencia
|
||||
|
||||
class LicenciaSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Licencia
|
||||
fields = '__all__'
|
||||
read_only_fields = ('created_at', 'updated_at')
|
||||
53
api/licence/tests.py
Normal file
53
api/licence/tests.py
Normal file
@@ -0,0 +1,53 @@
|
||||
|
||||
from django.urls import reverse
|
||||
from rest_framework.test import APITestCase, APIClient
|
||||
from rest_framework import status
|
||||
from django.contrib.auth import get_user_model
|
||||
from .models import Licencia
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
class LicenciaViewSetTests(APITestCase):
|
||||
def setUp(self):
|
||||
self.superuser = User.objects.create_superuser(username="superuser", password="superpass")
|
||||
self.user = User.objects.create_user(username="user", password="userpass")
|
||||
self.client = APIClient()
|
||||
|
||||
def test_superuser_can_list_licencias(self):
|
||||
Licencia.objects.create(nombre="Lic1", descripcion="desc1", almacenamiento=10)
|
||||
self.client.force_authenticate(user=self.superuser)
|
||||
url = reverse('licencia-list')
|
||||
response = self.client.get(url)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertTrue(len(response.data) >= 1)
|
||||
|
||||
def test_superuser_can_create_licencia(self):
|
||||
self.client.force_authenticate(user=self.superuser)
|
||||
url = reverse('licencia-list')
|
||||
data = {"nombre": "Lic2", "descripcion": "desc2", "almacenamiento": 20}
|
||||
response = self.client.post(url, data)
|
||||
self.assertIn(response.status_code, [status.HTTP_201_CREATED, status.HTTP_200_OK])
|
||||
|
||||
def test_superuser_can_update_licencia(self):
|
||||
lic = Licencia.objects.create(nombre="Lic3", descripcion="desc3", almacenamiento=30)
|
||||
self.client.force_authenticate(user=self.superuser)
|
||||
url = reverse('licencia-detail', args=[lic.id])
|
||||
data = {"descripcion": "updated"}
|
||||
response = self.client.patch(url, data)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['descripcion'], "updated")
|
||||
|
||||
def test_user_cannot_create_licencia(self):
|
||||
self.client.force_authenticate(user=self.user)
|
||||
url = reverse('licencia-list')
|
||||
data = {"nombre": "Lic4", "descripcion": "desc4", "almacenamiento": 40}
|
||||
response = self.client.post(url, data)
|
||||
self.assertNotIn(response.status_code, [status.HTTP_201_CREATED, status.HTTP_200_OK])
|
||||
|
||||
def test_user_cannot_update_licencia(self):
|
||||
lic = Licencia.objects.create(nombre="Lic5", descripcion="desc5", almacenamiento=50)
|
||||
self.client.force_authenticate(user=self.user)
|
||||
url = reverse('licencia-detail', args=[lic.id])
|
||||
data = {"descripcion": "updated"}
|
||||
response = self.client.patch(url, data)
|
||||
self.assertNotIn(response.status_code, [status.HTTP_200_OK])
|
||||
22
api/licence/urls.py
Normal file
22
api/licence/urls.py
Normal file
@@ -0,0 +1,22 @@
|
||||
# This file defines the URL patterns for the customs app in a Django project.
|
||||
from django.urls import path, include
|
||||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
# import necessary viewsets
|
||||
# from .views import YourViewSet # Import your viewsets here
|
||||
from .views import ViewSetLicencia
|
||||
# Create a router and register your viewsets with it
|
||||
|
||||
router = DefaultRouter()
|
||||
|
||||
# Register your viewsets with the router here
|
||||
# Example:
|
||||
# from .views import MyViewSet
|
||||
# router.register(r'myviewset', MyViewSet, basename='myviewset')
|
||||
router.register(r'licencias', ViewSetLicencia, basename='licencia')
|
||||
|
||||
# Import your viewsets here
|
||||
|
||||
urlpatterns = [
|
||||
path('', include(router.urls)),
|
||||
]
|
||||
61
api/licence/views.py
Normal file
61
api/licence/views.py
Normal file
@@ -0,0 +1,61 @@
|
||||
from django.shortcuts import render
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
|
||||
from core.permissions import IsSuperUser
|
||||
from .models import Licencia
|
||||
from .serializers import LicenciaSerializer
|
||||
|
||||
from api.logger.mixins import LoggingMixin
|
||||
# Create your views here.
|
||||
|
||||
class ViewSetLicencia(LoggingMixin, viewsets.ModelViewSet):
|
||||
"""
|
||||
ViewSet for Licencia model.
|
||||
"""
|
||||
permission_classes = [IsAuthenticated, IsSuperUser]
|
||||
|
||||
queryset = Licencia.objects.all()
|
||||
serializer_class = LicenciaSerializer
|
||||
filterset_fields = ['nombre', 'descripcion', 'fecha_emision']
|
||||
|
||||
my_tags = ['Licencias']
|
||||
|
||||
def perform_create(self, serializer):
|
||||
"""
|
||||
Override to add custom logic for creating a Licencia.
|
||||
"""
|
||||
if not self.request.user.is_authenticated:
|
||||
raise ValueError("Usuario no autenticado")
|
||||
|
||||
# If superuser, allow creating without organization
|
||||
if self.request.user.is_superuser:
|
||||
serializer.save()
|
||||
else:
|
||||
raise ValueError("Solo los superusuarios pueden crear licencias sin organización asignada")
|
||||
|
||||
def perform_update(self, serializer):
|
||||
"""
|
||||
Override to add custom logic for updating a Licencia.
|
||||
"""
|
||||
if not self.request.user.is_authenticated:
|
||||
raise ValueError("Usuario no autenticado")
|
||||
|
||||
# If superuser, allow updating without organization
|
||||
if self.request.user.is_superuser:
|
||||
serializer.save()
|
||||
else:
|
||||
raise ValueError("Solo los superusuarios pueden actualizar licencias sin organización asignada")
|
||||
|
||||
def perform_destroy(self, instance):
|
||||
"""
|
||||
Override to add custom logic for deleting a Licencia.
|
||||
"""
|
||||
if not self.request.user.is_authenticated:
|
||||
raise ValueError("Usuario no autenticado")
|
||||
|
||||
# If superuser, allow deleting without organization
|
||||
if self.request.user.is_superuser:
|
||||
instance.delete()
|
||||
else:
|
||||
raise ValueError("Solo los superusuarios pueden eliminar licencias sin organización asignada")
|
||||
Reference in New Issue
Block a user