From 0bbb914b226f15dcaa7d4527b7e689536cb1652c Mon Sep 17 00:00:00 2001 From: lzambrano18 Date: Mon, 13 Mar 2017 19:49:33 -0400 Subject: [PATCH 1/4] Corrections base --- django/lab_reservations/api/timetable_api.py | 12 +++++++++++- django/lab_reservations/models.py | 8 ++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/django/lab_reservations/api/timetable_api.py b/django/lab_reservations/api/timetable_api.py index 03550fe..9833d4d 100644 --- a/django/lab_reservations/api/timetable_api.py +++ b/django/lab_reservations/api/timetable_api.py @@ -1,7 +1,9 @@ from rest_framework import generics, views, viewsets from rest_framework.response import Response -from lab_reservations.models import CHOICES_BLOCKS, CHOICES_DAYS, Reservation, TimeTable +from lab_reservations.models import ( + CHOICES_BLOCKS, CHOICES_DAYS, CHOICES_TYPE_RESERVATIONS, CHOICES_STATUS_RESERVATIONS, + Reservation, TimeTable) from lab_reservations.serializers import ReservationSerializer, RoomTimeTableSerializer, TimeTableSerializer from lab_rooms.models import Room, RoomCharacteristic, TypeCharacteristic, TypeInfrastructure @@ -28,6 +30,14 @@ def get(self, request, format=None): obj[0]: obj[1] for obj in CHOICES_DAYS } + base['type_reservations'] = { + obj[0]: obj[1] + for obj in CHOICES_TYPE_RESERVATIONS + } + base['status_reservations'] = { + obj[0]: obj[1] + for obj in CHOICES_STATUS_RESERVATIONS + } json = {} types = TypeInfrastructure.objects.all() diff --git a/django/lab_reservations/models.py b/django/lab_reservations/models.py index e88c94e..5ee7321 100644 --- a/django/lab_reservations/models.py +++ b/django/lab_reservations/models.py @@ -30,7 +30,7 @@ [6, _('Sunday')], ] -choices_type_reservations = [ +CHOICES_TYPE_RESERVATIONS = [ [1, _('Partial')], [2, _('Quiz')], [3, _('Preparaduria')], @@ -40,7 +40,7 @@ [7, _('Other')], ] -choices_status_reservations = [ +CHOICES_STATUS_RESERVATIONS = [ [1, _('Approved')], [2, _('Pending')], [3, _('Rejected')], @@ -158,7 +158,7 @@ class Reservation(models.Model): date = models.DateTimeField(verbose_name=_('date')) description = models.CharField(verbose_name=_('description'), max_length=200) - type = models.IntegerField(verbose_name=_('type of reservation'), choices=choices_type_reservations) + type = models.IntegerField(verbose_name=_('type of reservation'), choices=CHOICES_TYPE_RESERVATIONS) semester = models.ForeignKey('lab_subjects.Semester', verbose_name=_('semester')) subject = models.ForeignKey('lab_subjects.Subject', verbose_name=_('subject')) user = models.ForeignKey('lab_accounts.User', verbose_name=_('user')) @@ -186,7 +186,7 @@ class StatusReservationHistoric(models.Model): start_date = models.DateTimeField(verbose_name=_('start date')) end_date = models.DateTimeField(verbose_name=_('end date')) - status = models.IntegerField(verbose_name=_('status'), choices=choices_status_reservations) + status = models.IntegerField(verbose_name=_('status'), choices=CHOICES_STATUS_RESERVATIONS) reservation = models.ForeignKey('Reservation', verbose_name=_('reservation')) created_at = models.DateTimeField(auto_now_add=True, editable=False) updated_at = models.DateTimeField(auto_now=True, editable=False) From 805260594ef3e3850f27612ceee91c52d8b56c18 Mon Sep 17 00:00:00 2001 From: lzambrano18 Date: Fri, 17 Mar 2017 05:57:17 -0400 Subject: [PATCH 2/4] Advance reservation corrections endpoint --- django/lab_accounts/admin.py | 16 +++++++++++++- .../migrations/0003_auto_20170317_0542.py | 21 +++++++++++++++++++ django/lab_reservations/admin.py | 8 ++++++- .../migrations/0005_auto_20170317_0542.py | 20 ++++++++++++++++++ django/lab_reservations/models.py | 2 +- django/lab_reservations/serializers.py | 21 ++++++++++++++++++- django/labcomp/settings.py | 3 ++- 7 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 django/lab_accounts/migrations/0003_auto_20170317_0542.py create mode 100644 django/lab_reservations/migrations/0005_auto_20170317_0542.py diff --git a/django/lab_accounts/admin.py b/django/lab_accounts/admin.py index 99d0088..4a86a24 100644 --- a/django/lab_accounts/admin.py +++ b/django/lab_accounts/admin.py @@ -1,8 +1,22 @@ +from django import forms from django.contrib import admin from lab_accounts.models import User +class UserForm(forms.ModelForm): + class Meta: + model = User + fields = [ + 'first_name', + 'last_name', + 'identity_card', + 'username', + 'password', + 'email' + ] + + @admin.register(User) class UserAdmin(admin.ModelAdmin): - pass + form = UserForm diff --git a/django/lab_accounts/migrations/0003_auto_20170317_0542.py b/django/lab_accounts/migrations/0003_auto_20170317_0542.py new file mode 100644 index 0000000..ae87f78 --- /dev/null +++ b/django/lab_accounts/migrations/0003_auto_20170317_0542.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-03-17 05:42 +from __future__ import unicode_literals + +import django.contrib.auth.validators +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('lab_accounts', '0002_alter_by_locale'), + ] + + operations = [ + migrations.AlterField( + model_name='user', + name='username', + field=models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username'), + ), + ] diff --git a/django/lab_reservations/admin.py b/django/lab_reservations/admin.py index c454fe6..0ab4286 100644 --- a/django/lab_reservations/admin.py +++ b/django/lab_reservations/admin.py @@ -1,8 +1,14 @@ from django.contrib import admin -from .models import Section +from .models import Section, Reservation @admin.register(Section) class SectionAdmin(admin.ModelAdmin): pass + + +@admin.register(Reservation) +class ReservationAdmin(admin.ModelAdmin): + list_display = ('date', 'type', 'user') + diff --git a/django/lab_reservations/migrations/0005_auto_20170317_0542.py b/django/lab_reservations/migrations/0005_auto_20170317_0542.py new file mode 100644 index 0000000..02bfd19 --- /dev/null +++ b/django/lab_reservations/migrations/0005_auto_20170317_0542.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-03-17 05:42 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('lab_reservations', '0004_auto_20161027_2231'), + ] + + operations = [ + migrations.AlterField( + model_name='statusreservationhistoric', + name='end_date', + field=models.DateTimeField(null=True, verbose_name='end date'), + ), + ] diff --git a/django/lab_reservations/models.py b/django/lab_reservations/models.py index 5ee7321..84714cc 100644 --- a/django/lab_reservations/models.py +++ b/django/lab_reservations/models.py @@ -185,7 +185,7 @@ class StatusReservationHistoric(models.Model): """ start_date = models.DateTimeField(verbose_name=_('start date')) - end_date = models.DateTimeField(verbose_name=_('end date')) + end_date = models.DateTimeField(verbose_name=_('end date'), null=True) status = models.IntegerField(verbose_name=_('status'), choices=CHOICES_STATUS_RESERVATIONS) reservation = models.ForeignKey('Reservation', verbose_name=_('reservation')) created_at = models.DateTimeField(auto_now_add=True, editable=False) diff --git a/django/lab_reservations/serializers.py b/django/lab_reservations/serializers.py index 7dedafa..01c96dc 100644 --- a/django/lab_reservations/serializers.py +++ b/django/lab_reservations/serializers.py @@ -2,7 +2,9 @@ from lab_rooms.models import RoomCharacteristic, TypeCharacteristic -from .models import Reservation, Section, TimeTable +from .models import Reservation, Section, TimeTable, StatusReservationHistoric + +from datetime import datetime class TimeTableSerializer(serializers.ModelSerializer): @@ -92,3 +94,20 @@ class ReservationSerializer(serializers.ModelSerializer): class Meta: model = Reservation fields = '__all__' + exclude = ('date', ) + + def create(self, validated_data): + data_timetable = validated_data.pop('timetable') + timetable = TimeTable.objects.create(**data_timetable) + + reservation = Reservation.objects.create( + timetable=timetable, + date=datetime.now(), + **validated_data) + + StatusReservationHistoric.objects.create( + start_date=datetime.now(), + reservation=reservation, + status=2 + ) + return reservation diff --git a/django/labcomp/settings.py b/django/labcomp/settings.py index 33c9aae..96a2b69 100644 --- a/django/labcomp/settings.py +++ b/django/labcomp/settings.py @@ -173,7 +173,7 @@ def custom_show_toolbar(request): ), 'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.DjangoFilterBackend',) } - +'''' LOGGING = { 'version': 1, 'disable_existing_loggers': False, @@ -197,6 +197,7 @@ def custom_show_toolbar(request): } } } +''' # LDAP # The URL of the LDAP server. From 1161887d04f796b45b05d9a6465f472a36157eaa Mon Sep 17 00:00:00 2001 From: lzambrano18 Date: Sat, 18 Mar 2017 05:33:20 -0400 Subject: [PATCH 3/4] Admin reservation status --- django/lab_reservations/admin.py | 106 +++++++++++++++++- .../0006_remove_reservation_subject.py | 19 ++++ .../migrations/0007_auto_20170318_0249.py | 20 ++++ django/lab_reservations/models.py | 28 ++++- django/lab_reservations/serializers.py | 1 - 5 files changed, 169 insertions(+), 5 deletions(-) create mode 100644 django/lab_reservations/migrations/0006_remove_reservation_subject.py create mode 100644 django/lab_reservations/migrations/0007_auto_20170318_0249.py diff --git a/django/lab_reservations/admin.py b/django/lab_reservations/admin.py index 0ab4286..65739c9 100644 --- a/django/lab_reservations/admin.py +++ b/django/lab_reservations/admin.py @@ -1,6 +1,30 @@ from django.contrib import admin +from django.utils.translation import ugettext as _ +from django.conf.urls import url +from django.urls import reverse +from django.utils.html import format_html +from django.contrib.admin import SimpleListFilter +from datetime import datetime -from .models import Section, Reservation +from .models import Section, Reservation, StatusReservationHistoric, CHOICES_STATUS_RESERVATIONS + + +class StatusFilter(SimpleListFilter): + title = _('status') + parameter_name = _('status') + + def lookups(self, request, model_admin): + return CHOICES_STATUS_RESERVATIONS + + def queryset(self, request, queryset): + reservations = StatusReservationHistoric.objects.filter( + status=self.value(), + end_date=None).values_list('reservation', flat=True) + + if self.value(): + return queryset.filter(id__in=list(reservations)) + else: + return queryset @admin.register(Section) @@ -10,5 +34,83 @@ class SectionAdmin(admin.ModelAdmin): @admin.register(Reservation) class ReservationAdmin(admin.ModelAdmin): - list_display = ('date', 'type', 'user') + list_display_links = None + list_display = ( + 'date', 'user', 'get_room', 'get_section', 'get_day', + 'get_block_start', 'get_block_end', 'type', 'action') + list_filter = (StatusFilter,) + + def get_room(self, obj): + return obj.timetable.room + + def get_section(self, obj): + return obj.timetable.section + + def get_day(self, obj): + return obj.timetable.get_day() + + def get_block_start(self, obj): + return obj.timetable.get_block_start() + + def get_block_end(self, obj): + return obj.timetable.get_block_end() + + def action(self, obj): + if obj.get_status()[0] == 2: + return format_html( + 'Aceptar ' + 'Rechazar', + reverse('admin:change-status-approved', args=[obj.pk]), + reverse('admin:change-status-rejected', args=[obj.pk]) + ) + return obj.get_status()[1] + + get_room.short_description = _('room') + get_section.short_description = _('section') + get_day.short_description = _('day') + get_block_start.short_description = _('block start') + get_block_end.short_description = _('block end') + action.short_description = _('action') + + def has_add_permission(self, request): + return False + + def get_urls(self): + urls = super().get_urls() + custom_urls = [ + url( + r'^(?P.+)/approved/$', + self.admin_site.admin_view(self.change_status_approved), + name='change-status-approved', + ), + url( + r'^(?P.+)/rejected/$', + self.admin_site.admin_view(self.change_status_rejected), + name='change-status-rejected', + ), + ] + return custom_urls + urls + + def change_status_approved(self, request, reservation_id, *args, **kwargs): + reservation = StatusReservationHistoric.objects.get(reservation=reservation_id, end_date=None) + reservation.end_date = datetime.now() + reservation.save(update_fields=['end_date']) + + StatusReservationHistoric.objects.create( + reservation=reservation.reservation, + start_date=datetime.now(), + status=1 + ) + return super(ReservationAdmin, self).changelist_view(request) + + def change_status_rejected(self, request, reservation_id, *args, **kwargs): + reservation = StatusReservationHistoric.objects.get(reservation=reservation_id, end_date=None) + reservation.end_date = datetime.now() + reservation.save(update_fields=['end_date']) + StatusReservationHistoric.objects.create( + reservation=reservation.reservation, + start_date=datetime.now(), + status=3 + ) + return super(ReservationAdmin, self).changelist_view(request) diff --git a/django/lab_reservations/migrations/0006_remove_reservation_subject.py b/django/lab_reservations/migrations/0006_remove_reservation_subject.py new file mode 100644 index 0000000..f64760c --- /dev/null +++ b/django/lab_reservations/migrations/0006_remove_reservation_subject.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-03-18 01:00 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('lab_reservations', '0005_auto_20170317_0542'), + ] + + operations = [ + migrations.RemoveField( + model_name='reservation', + name='subject', + ), + ] diff --git a/django/lab_reservations/migrations/0007_auto_20170318_0249.py b/django/lab_reservations/migrations/0007_auto_20170318_0249.py new file mode 100644 index 0000000..41bb1e9 --- /dev/null +++ b/django/lab_reservations/migrations/0007_auto_20170318_0249.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-03-18 02:49 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('lab_reservations', '0006_remove_reservation_subject'), + ] + + operations = [ + migrations.AlterField( + model_name='reservation', + name='date', + field=models.DateField(verbose_name='date'), + ), + ] diff --git a/django/lab_reservations/models.py b/django/lab_reservations/models.py index 84714cc..25b5c11 100644 --- a/django/lab_reservations/models.py +++ b/django/lab_reservations/models.py @@ -118,6 +118,24 @@ def get_block_valid(block_start, block_end, day, room): return False return True + def get_block_start(self): + for x in CHOICES_BLOCKS: + if x[0] == self.block_start: + return x[1] + return self.block_start + + def get_block_end(self): + for x in CHOICES_BLOCKS: + if x[0] == self.block_end: + return x[1] + return self.block_end + + def get_day(self): + for x in CHOICES_DAYS: + if x[0] == self.day: + return x[1] + return self.day + class HourFreed(models.Model): """ @@ -156,11 +174,10 @@ class Reservation(models.Model): - timetable (ManyToManyField): Almacena los bloques de horas que ocupan en el horario la reservacion. """ - date = models.DateTimeField(verbose_name=_('date')) + date = models.DateField(verbose_name=_('date')) description = models.CharField(verbose_name=_('description'), max_length=200) type = models.IntegerField(verbose_name=_('type of reservation'), choices=CHOICES_TYPE_RESERVATIONS) semester = models.ForeignKey('lab_subjects.Semester', verbose_name=_('semester')) - subject = models.ForeignKey('lab_subjects.Subject', verbose_name=_('subject')) user = models.ForeignKey('lab_accounts.User', verbose_name=_('user')) timetable = models.ForeignKey('TimeTable', verbose_name=_('timetable')) created_at = models.DateTimeField(auto_now_add=True, editable=False) @@ -170,6 +187,13 @@ class Meta: verbose_name = _('reservation') verbose_name_plural = _('reservations') + def get_status(self): + status = self.statusreservationhistoric_set.filter(end_date=None) + for x in CHOICES_STATUS_RESERVATIONS: + if x[0] == status[0].status: + return x + return [] + class StatusReservationHistoric(models.Model): """ diff --git a/django/lab_reservations/serializers.py b/django/lab_reservations/serializers.py index 01c96dc..96fd221 100644 --- a/django/lab_reservations/serializers.py +++ b/django/lab_reservations/serializers.py @@ -102,7 +102,6 @@ def create(self, validated_data): reservation = Reservation.objects.create( timetable=timetable, - date=datetime.now(), **validated_data) StatusReservationHistoric.objects.create( From ef72206ac78c8efa019cdfcde2a01a9c51dc414b Mon Sep 17 00:00:00 2001 From: lzambrano18 Date: Sun, 19 Mar 2017 13:13:30 -0400 Subject: [PATCH 4/4] standars --- django/lab_accounts/admin.py | 1 - django/lab_reservations/admin.py | 11 ++++++----- django/lab_reservations/api/timetable_api.py | 9 ++++----- django/lab_reservations/serializers.py | 8 +++----- django/lab_reservations/urls.py | 3 +-- django/lab_rooms/admin.py | 1 - django/lab_subjects/admin.py | 1 - django/utils/task.py | 1 - 8 files changed, 14 insertions(+), 21 deletions(-) diff --git a/django/lab_accounts/admin.py b/django/lab_accounts/admin.py index 4a86a24..556365c 100644 --- a/django/lab_accounts/admin.py +++ b/django/lab_accounts/admin.py @@ -1,6 +1,5 @@ from django import forms from django.contrib import admin - from lab_accounts.models import User diff --git a/django/lab_reservations/admin.py b/django/lab_reservations/admin.py index 65739c9..b1e2696 100644 --- a/django/lab_reservations/admin.py +++ b/django/lab_reservations/admin.py @@ -1,12 +1,13 @@ -from django.contrib import admin -from django.utils.translation import ugettext as _ +from datetime import datetime + from django.conf.urls import url +from django.contrib import admin +from django.contrib.admin import SimpleListFilter from django.urls import reverse from django.utils.html import format_html -from django.contrib.admin import SimpleListFilter -from datetime import datetime +from django.utils.translation import ugettext as _ -from .models import Section, Reservation, StatusReservationHistoric, CHOICES_STATUS_RESERVATIONS +from .models import CHOICES_STATUS_RESERVATIONS, Reservation, Section, StatusReservationHistoric class StatusFilter(SimpleListFilter): diff --git a/django/lab_reservations/api/timetable_api.py b/django/lab_reservations/api/timetable_api.py index 9833d4d..cc8af52 100644 --- a/django/lab_reservations/api/timetable_api.py +++ b/django/lab_reservations/api/timetable_api.py @@ -1,11 +1,10 @@ -from rest_framework import generics, views, viewsets -from rest_framework.response import Response - from lab_reservations.models import ( - CHOICES_BLOCKS, CHOICES_DAYS, CHOICES_TYPE_RESERVATIONS, CHOICES_STATUS_RESERVATIONS, - Reservation, TimeTable) + CHOICES_BLOCKS, CHOICES_DAYS, CHOICES_STATUS_RESERVATIONS, CHOICES_TYPE_RESERVATIONS, Reservation, TimeTable, +) from lab_reservations.serializers import ReservationSerializer, RoomTimeTableSerializer, TimeTableSerializer from lab_rooms.models import Room, RoomCharacteristic, TypeCharacteristic, TypeInfrastructure +from rest_framework import generics, views, viewsets +from rest_framework.response import Response class TimeTableViewSet(viewsets.ModelViewSet): diff --git a/django/lab_reservations/serializers.py b/django/lab_reservations/serializers.py index 96fd221..794332e 100644 --- a/django/lab_reservations/serializers.py +++ b/django/lab_reservations/serializers.py @@ -1,10 +1,9 @@ -from rest_framework import serializers +from datetime import datetime from lab_rooms.models import RoomCharacteristic, TypeCharacteristic +from rest_framework import serializers -from .models import Reservation, Section, TimeTable, StatusReservationHistoric - -from datetime import datetime +from .models import Reservation, Section, StatusReservationHistoric, TimeTable class TimeTableSerializer(serializers.ModelSerializer): @@ -94,7 +93,6 @@ class ReservationSerializer(serializers.ModelSerializer): class Meta: model = Reservation fields = '__all__' - exclude = ('date', ) def create(self, validated_data): data_timetable = validated_data.pop('timetable') diff --git a/django/lab_reservations/urls.py b/django/lab_reservations/urls.py index e0155ff..1b9de66 100644 --- a/django/lab_reservations/urls.py +++ b/django/lab_reservations/urls.py @@ -1,9 +1,8 @@ from django.conf.urls import include, url -from rest_framework import routers - from lab_reservations.api.timetable_api import ( BaseTimeTableView, ReservationViewSet, RoomTimeTableView, TimeTableViewSet, ) +from rest_framework import routers # Routers provide an easy way of automatically determining the URL conf. router = routers.DefaultRouter() diff --git a/django/lab_rooms/admin.py b/django/lab_rooms/admin.py index 193e013..85d8014 100644 --- a/django/lab_rooms/admin.py +++ b/django/lab_rooms/admin.py @@ -1,5 +1,4 @@ from django.contrib import admin - from lab_rooms.models import Characteristic, Room, RoomCharacteristic, TypeCharacteristic, TypeInfrastructure diff --git a/django/lab_subjects/admin.py b/django/lab_subjects/admin.py index 6040abe..4f441af 100644 --- a/django/lab_subjects/admin.py +++ b/django/lab_subjects/admin.py @@ -1,6 +1,5 @@ from django.contrib import admin from django.utils.html import format_html - from lab_subjects.models import Color, Department, ReservationPermission, Semester, Subject diff --git a/django/utils/task.py b/django/utils/task.py index 8c90c38..1e7308d 100644 --- a/django/utils/task.py +++ b/django/utils/task.py @@ -2,7 +2,6 @@ from django.core.mail import EmailMultiAlternatives from django.template import Context from django.template.loader import get_template - from labcomp.celery import app