Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Labcomp base #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions django/lab_accounts/admin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
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
21 changes: 21 additions & 0 deletions django/lab_accounts/migrations/0003_auto_20170317_0542.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
111 changes: 110 additions & 1 deletion django/lab_reservations/admin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,117 @@
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.utils.translation import ugettext as _

from .models import CHOICES_STATUS_RESERVATIONS, Reservation, Section, StatusReservationHistoric


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)

from .models import Section
if self.value():
return queryset.filter(id__in=list(reservations))
else:
return queryset


@admin.register(Section)
class SectionAdmin(admin.ModelAdmin):
pass


@admin.register(Reservation)
class ReservationAdmin(admin.ModelAdmin):
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(
'<a class="button" href="{}">Aceptar</a>&nbsp;'
'<a class="button" style="background: #ba2121;" href="{}">Rechazar</a>',
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<reservation_id>.+)/approved/$',
self.admin_site.admin_view(self.change_status_approved),
name='change-status-approved',
),
url(
r'^(?P<reservation_id>.+)/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)
17 changes: 13 additions & 4 deletions django/lab_reservations/api/timetable_api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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_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):
Expand All @@ -28,6 +29,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()
Expand Down
20 changes: 20 additions & 0 deletions django/lab_reservations/migrations/0005_auto_20170317_0542.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
Original file line number Diff line number Diff line change
@@ -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',
),
]
20 changes: 20 additions & 0 deletions django/lab_reservations/migrations/0007_auto_20170318_0249.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
38 changes: 31 additions & 7 deletions django/lab_reservations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
[6, _('Sunday')],
]

choices_type_reservations = [
CHOICES_TYPE_RESERVATIONS = [
[1, _('Partial')],
[2, _('Quiz')],
[3, _('Preparaduria')],
Expand All @@ -40,7 +40,7 @@
[7, _('Other')],
]

choices_status_reservations = [
CHOICES_STATUS_RESERVATIONS = [
[1, _('Approved')],
[2, _('Pending')],
[3, _('Rejected')],
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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)
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)
Expand All @@ -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):
"""
Expand All @@ -185,8 +209,8 @@ 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)
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)
updated_at = models.DateTimeField(auto_now=True, editable=False)
20 changes: 18 additions & 2 deletions django/lab_reservations/serializers.py
Original file line number Diff line number Diff line change
@@ -1,8 +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
from .models import Reservation, Section, StatusReservationHistoric, TimeTable


class TimeTableSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -92,3 +93,18 @@ class ReservationSerializer(serializers.ModelSerializer):
class Meta:
model = Reservation
fields = '__all__'

def create(self, validated_data):
data_timetable = validated_data.pop('timetable')
timetable = TimeTable.objects.create(**data_timetable)

reservation = Reservation.objects.create(
timetable=timetable,
**validated_data)

StatusReservationHistoric.objects.create(
start_date=datetime.now(),
reservation=reservation,
status=2
)
return reservation
3 changes: 1 addition & 2 deletions django/lab_reservations/urls.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
1 change: 0 additions & 1 deletion django/lab_rooms/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.contrib import admin

from lab_rooms.models import Characteristic, Room, RoomCharacteristic, TypeCharacteristic, TypeInfrastructure


Expand Down
1 change: 0 additions & 1 deletion django/lab_subjects/admin.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
Loading