users disponibilities
This commit is contained in:
parent
f3b4195ecf
commit
f270bbb280
25
accounts/migrations/0013_availabilitytimeslot.py
Normal file
25
accounts/migrations/0013_availabilitytimeslot.py
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Generated by Django 1.10 on 2016-10-10 21:08
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('accounts', '0012_auto_20161006_1853'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='AvailabilityTimeslot',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('start', models.DateTimeField(blank=True)),
|
||||||
|
('end', models.DateTimeField(blank=True)),
|
||||||
|
('participation', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='availabilities', to='accounts.Participation')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
|
@ -105,3 +105,26 @@ class Participation(PonyConfModel):
|
||||||
@property
|
@property
|
||||||
def talk_set(self):
|
def talk_set(self):
|
||||||
return self.user.talk_set.filter(site=self.site)
|
return self.user.talk_set.filter(site=self.site)
|
||||||
|
|
||||||
|
# return True, False or None if availabilities have not been filled
|
||||||
|
def is_available(self, start, end=None):
|
||||||
|
if not self.availabilities.exists():
|
||||||
|
return None
|
||||||
|
for timeslot in self.availabilities.all():
|
||||||
|
if start < timeslot.start:
|
||||||
|
continue
|
||||||
|
if start > timeslot.end:
|
||||||
|
continue
|
||||||
|
if end:
|
||||||
|
assert(start < end)
|
||||||
|
if end > timeslot.end:
|
||||||
|
continue
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
class AvailabilityTimeslot(models.Model):
|
||||||
|
|
||||||
|
participation = models.ForeignKey(Participation, related_name='availabilities')
|
||||||
|
start = models.DateTimeField(blank=True)
|
||||||
|
end = models.DateTimeField(blank=True)
|
||||||
|
|
|
@ -71,3 +71,38 @@ class AccountTests(TestCase):
|
||||||
}).status_code, 200)
|
}).status_code, 200)
|
||||||
self.assertEqual(User.objects.get(username='a').profile.biography, 'foo')
|
self.assertEqual(User.objects.get(username='a').profile.biography, 'foo')
|
||||||
self.assertEqual(Participation.objects.get(user=User.objects.get(username='a')).video_licence, 2)
|
self.assertEqual(Participation.objects.get(user=User.objects.get(username='a')).video_licence, 2)
|
||||||
|
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
from .models import AvailabilityTimeslot
|
||||||
|
class DisponibilitiesTests(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.user = User.objects.create_user('a', email='a@example.org', password='a')
|
||||||
|
self.participation = Participation.objects.create(user=self.user, site=Site.objects.first())
|
||||||
|
|
||||||
|
def test_is_available(self):
|
||||||
|
from django.utils.timezone import is_naive, get_default_timezone
|
||||||
|
tz = get_default_timezone()
|
||||||
|
d = {}
|
||||||
|
for i in range(8, 18, 1):
|
||||||
|
d[i] = datetime(2016, 10, 10, i, 0, 0, tzinfo=tz)
|
||||||
|
self.assertEquals(self.participation.is_available(d[10]), None)
|
||||||
|
AvailabilityTimeslot.objects.create(participation=self.participation, start=d[10], end=d[12])
|
||||||
|
self.assertEquals(self.participation.is_available(d[9]), False)
|
||||||
|
self.assertEquals(self.participation.is_available(d[11]), True)
|
||||||
|
self.assertEquals(self.participation.is_available(d[13]), False)
|
||||||
|
self.assertEquals(self.participation.is_available(d[8], d[9]), False)
|
||||||
|
self.assertEquals(self.participation.is_available(d[9], d[11]), False)
|
||||||
|
self.assertEquals(self.participation.is_available(d[10], d[11]), True)
|
||||||
|
self.assertEquals(self.participation.is_available(d[11], d[12]), True)
|
||||||
|
self.assertEquals(self.participation.is_available(d[10], d[12]), True)
|
||||||
|
self.assertEquals(self.participation.is_available(d[11], d[13]), False)
|
||||||
|
self.assertEquals(self.participation.is_available(d[13], d[14]), False)
|
||||||
|
AvailabilityTimeslot.objects.create(participation=self.participation, start=d[14], end=d[16])
|
||||||
|
self.assertEquals(self.participation.is_available(d[10], d[12]), True)
|
||||||
|
self.assertEquals(self.participation.is_available(d[14], d[16]), True)
|
||||||
|
self.assertEquals(self.participation.is_available(d[11], d[15]), False)
|
||||||
|
self.assertEquals(self.participation.is_available(d[11], d[17]), False)
|
||||||
|
self.assertEquals(self.participation.is_available(d[13], d[17]), False)
|
||||||
|
self.assertEquals(self.participation.is_available(d[9], d[15]), False)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user