mirror of
https://github.com/dkmstr/openuds.git
synced 2024-12-22 13:34:04 +03:00
Adding calendar/rules models
This commit is contained in:
parent
7706262702
commit
6ed924655d
104
server/src/uds/core/util/calendar/__init__.py
Normal file
104
server/src/uds/core/util/calendar/__init__.py
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
#
|
||||||
|
# Copyright (c) 2015 Virtual Cable S.L.
|
||||||
|
# All rights reserved.
|
||||||
|
#
|
||||||
|
# Redistribution and use in source and binary forms, with or without modification,
|
||||||
|
# are permitted provided that the following conditions are met:
|
||||||
|
#
|
||||||
|
# * Redistributions of source code must retain the above copyright notice,
|
||||||
|
# this list of conditions and the following disclaimer.
|
||||||
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
# this list of conditions and the following disclaimer in the documentation
|
||||||
|
# and/or other materials provided with the distribution.
|
||||||
|
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
|
||||||
|
# may be used to endorse or promote products derived from this software
|
||||||
|
# without specific prior written permission.
|
||||||
|
#
|
||||||
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
'''
|
||||||
|
.. moduleauthor:: Adolfo Gómez, dkmaster at dkmon dot com
|
||||||
|
'''
|
||||||
|
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from uds.models.Util import NEVER
|
||||||
|
from uds.models.Util import getSqlDatetime
|
||||||
|
|
||||||
|
from uds.models.Calendar import Calendar
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
import bitarray
|
||||||
|
import logging
|
||||||
|
|
||||||
|
__updated__ = '2015-09-09'
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
class CalendarChecker(object):
|
||||||
|
calendar = None
|
||||||
|
inverse = False
|
||||||
|
cache = None
|
||||||
|
|
||||||
|
def __init__(self, calendar, inverse=False):
|
||||||
|
self.calendar = calendar
|
||||||
|
self.calendar_modified = None
|
||||||
|
self.inverse = inverse
|
||||||
|
self.data = None
|
||||||
|
self.data_time = None
|
||||||
|
|
||||||
|
def _updateData(self, dtime):
|
||||||
|
self.calendar_modified = self.calendar.modified
|
||||||
|
self.data_time = dtime.date()
|
||||||
|
self.data = bitarray.bitarray(60 * 24) # Granurality is minute
|
||||||
|
self.data.setall(False)
|
||||||
|
start = datetime.datetime.combine(datetime.date.today(), datetime.datetime.min.time())
|
||||||
|
end = datetime.datetime.combine(datetime.date.today(), datetime.datetime.max.time())
|
||||||
|
|
||||||
|
for rule in self.calendar.rules.all():
|
||||||
|
rr = rule.as_rrule()
|
||||||
|
duration = rule.duration
|
||||||
|
_start = start if start > rule.start else rule.start - datetime.timedelta(seconds=1)
|
||||||
|
_end = end if rule.end is None or end < rule.end else rule.end
|
||||||
|
print(_start, end)
|
||||||
|
for val in rr.between(_start, _end, inc=True):
|
||||||
|
pos = val.hour * 60 + val.minute
|
||||||
|
posdur = pos + duration
|
||||||
|
if posdur >= 60 * 24:
|
||||||
|
posdur = 60 * 24 - 1
|
||||||
|
print(pos, posdur)
|
||||||
|
self.data[pos:posdur] = True
|
||||||
|
|
||||||
|
# Now self.data can be accessed as an array of booleans, and if
|
||||||
|
|
||||||
|
def check(self, dtime=None):
|
||||||
|
'''
|
||||||
|
Checks if the given time is a valid event on calendar
|
||||||
|
@param dtime: Datetime object to check
|
||||||
|
'''
|
||||||
|
if dtime is None:
|
||||||
|
dtime = datetime.datetime.now()
|
||||||
|
if self.calendar_modified != self.calendar.modified or self.data is None or self.data_time != dtime.date():
|
||||||
|
self._updateData(dtime)
|
||||||
|
|
||||||
|
return self.data[dtime.hour * 60 + dtime.minute]
|
||||||
|
|
||||||
|
def debug(self):
|
||||||
|
if self.data is None:
|
||||||
|
self.check()
|
||||||
|
|
||||||
|
return '\n'.join([
|
||||||
|
'{1}:{2} is {0}'.format(self.data[i], i / 60, i % 60) for i in range(60 * 24)
|
||||||
|
])
|
45
server/src/uds/migrations/0017_calendar_calendarrule.py
Normal file
45
server/src/uds/migrations/0017_calendar_calendarrule.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import models, migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('uds', '0016_auto_20150617_0741'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Calendar',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
||||||
|
('uuid', models.CharField(default=None, max_length=50, unique=True, null=True)),
|
||||||
|
('name', models.CharField(default='', max_length=128)),
|
||||||
|
('comments', models.CharField(default='', max_length=256)),
|
||||||
|
('modified', models.DateTimeField(auto_now=True)),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
'db_table': 'uds_calendar',
|
||||||
|
},
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='CalendarRule',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
||||||
|
('uuid', models.CharField(default=None, max_length=50, unique=True, null=True)),
|
||||||
|
('name', models.CharField(max_length=128)),
|
||||||
|
('comments', models.CharField(max_length=256)),
|
||||||
|
('start', models.DateTimeField()),
|
||||||
|
('end', models.DateField(null=True, blank=True)),
|
||||||
|
('frequency', models.CharField(max_length=32, choices=[('YEARLY', 'Yearly'), ('MONTHLY', 'Monthly'), ('WEEKLY', 'Weekly'), ('DAILY', 'Daily'), ('WEEKDAYS', 'Weekdays')])),
|
||||||
|
('interval', models.IntegerField(default=1)),
|
||||||
|
('duration', models.IntegerField(default=0)),
|
||||||
|
('calendar', models.ForeignKey(related_name='rules', to='uds.Calendar')),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
'db_table': 'uds_calendar_rules',
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
61
server/src/uds/models/Calendar.py
Normal file
61
server/src/uds/models/Calendar.py
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Model based on https://github.com/llazzaro/django-scheduler
|
||||||
|
#
|
||||||
|
# Copyright (c) 2012 Virtual Cable S.L.
|
||||||
|
# All rights reserved.
|
||||||
|
#
|
||||||
|
# Redistribution and use in source and binary forms, with or without modification,
|
||||||
|
# are permitted provided that the following conditions are met:
|
||||||
|
#
|
||||||
|
# * Redistributions of source code must retain the above copyright notice,
|
||||||
|
# this list of conditions and the following disclaimer.
|
||||||
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
# this list of conditions and the following disclaimer in the documentation
|
||||||
|
# and/or other materials provided with the distribution.
|
||||||
|
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
|
||||||
|
# may be used to endorse or promote products derived from this software
|
||||||
|
# without specific prior written permission.
|
||||||
|
#
|
||||||
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
'''
|
||||||
|
.. moduleauthor:: Adolfo Gómez, dkmaster at dkmon dot com
|
||||||
|
'''
|
||||||
|
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
__updated__ = '2015-09-08'
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
from uds.models.UUIDModel import UUIDModel
|
||||||
|
from django.utils.encoding import python_2_unicode_compatible
|
||||||
|
from django.utils.translation import ugettext_lazy as _, ugettext
|
||||||
|
from dateutil.rrule import DAILY, MONTHLY, WEEKLY, YEARLY, HOURLY, MINUTELY, SECONDLY
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# @python_2_unicode_compatible
|
||||||
|
class Calendar(UUIDModel):
|
||||||
|
|
||||||
|
name = models.CharField(max_length=128, default='')
|
||||||
|
comments = models.CharField(max_length=256, default='')
|
||||||
|
modified = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
'''
|
||||||
|
Meta class to declare db table
|
||||||
|
'''
|
||||||
|
db_table = 'uds_calendar'
|
||||||
|
app_label = 'uds'
|
103
server/src/uds/models/CalendarRule.py
Normal file
103
server/src/uds/models/CalendarRule.py
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
#
|
||||||
|
# Copyright (c) 2012 Virtual Cable S.L.
|
||||||
|
# All rights reserved.
|
||||||
|
#
|
||||||
|
# Redistribution and use in source and binary forms, with or without modification,
|
||||||
|
# are permitted provided that the following conditions are met:
|
||||||
|
#
|
||||||
|
# * Redistributions of source code must retain the above copyright notice,
|
||||||
|
# this list of conditions and the following disclaimer.
|
||||||
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
# this list of conditions and the following disclaimer in the documentation
|
||||||
|
# and/or other materials provided with the distribution.
|
||||||
|
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
|
||||||
|
# may be used to endorse or promote products derived from this software
|
||||||
|
# without specific prior written permission.
|
||||||
|
#
|
||||||
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
'''
|
||||||
|
.. moduleauthor:: Adolfo Gómez, dkmaster at dkmon dot com
|
||||||
|
'''
|
||||||
|
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
__updated__ = '2015-09-09'
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
from django.utils.encoding import python_2_unicode_compatible
|
||||||
|
from django.utils.translation import ugettext_lazy as _, ugettext
|
||||||
|
from dateutil import rrule as rules
|
||||||
|
|
||||||
|
from .UUIDModel import UUIDModel
|
||||||
|
from .Calendar import Calendar
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
WEEKDAYS = 'WEEKDAYS'
|
||||||
|
|
||||||
|
# Frequencies
|
||||||
|
freqs = (("YEARLY", _("Yearly")),
|
||||||
|
("MONTHLY", _("Monthly")),
|
||||||
|
("WEEKLY", _("Weekly")),
|
||||||
|
("DAILY", _("Daily")),
|
||||||
|
(WEEKDAYS, _("Weekdays")))
|
||||||
|
|
||||||
|
frq_to_rrl = {
|
||||||
|
'YEARLY': rules.YEARLY,
|
||||||
|
'MONTHLY': rules.MONTHLY,
|
||||||
|
'WEEKLY': rules.WEEKLY,
|
||||||
|
'DAILY': rules.DAILY,
|
||||||
|
}
|
||||||
|
|
||||||
|
weekdays = [rules.SU, rules.MO, rules.TU, rules.WE, rules.TH, rules.FR, rules.SA]
|
||||||
|
|
||||||
|
|
||||||
|
@python_2_unicode_compatible
|
||||||
|
class CalendarRule(UUIDModel):
|
||||||
|
name = models.CharField(max_length=128)
|
||||||
|
comments = models.CharField(max_length=256)
|
||||||
|
|
||||||
|
start = models.DateTimeField()
|
||||||
|
end = models.DateField(null=True, blank=True)
|
||||||
|
frequency = models.CharField(choices=freqs, max_length=32)
|
||||||
|
interval = models.IntegerField(default=1) # If interval is for WEEKDAYS, every bit means a day of week (bit 0 = SUN, 1 = MON, ...)
|
||||||
|
duration = models.IntegerField(default=0) # Duration in minutes
|
||||||
|
|
||||||
|
calendar = models.ForeignKey(Calendar, related_name='rules')
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
'''
|
||||||
|
Meta class to declare db table
|
||||||
|
'''
|
||||||
|
db_table = 'uds_calendar_rules'
|
||||||
|
app_label = 'uds'
|
||||||
|
|
||||||
|
def as_rrule(self):
|
||||||
|
if self.frequency == WEEKDAYS:
|
||||||
|
dw = []
|
||||||
|
l = self.interval
|
||||||
|
for i in range(7):
|
||||||
|
if l & 1 == 1:
|
||||||
|
dw.append(weekdays[i])
|
||||||
|
l >>= 1
|
||||||
|
return rules.rrule(rules.DAILY, byweekday=dw, dtstart=self.start)
|
||||||
|
else:
|
||||||
|
return rules.rrule(frq_to_rrl[self.frequency], interval=self.interval, dtstart=self.start)
|
||||||
|
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return 'Rule {0}: {1}-{2}, {3}, Interval: {4}, duration: {5}'.format(self.name, self.start, self.end, self.frequency, self.interval, self.duration)
|
@ -91,10 +91,14 @@ from .DelayedTask import DelayedTask
|
|||||||
# Image galery related
|
# Image galery related
|
||||||
from .Image import Image
|
from .Image import Image
|
||||||
|
|
||||||
|
# Ticket storage
|
||||||
from .TicketStore import TicketStore
|
from .TicketStore import TicketStore
|
||||||
|
|
||||||
|
# Calendar related
|
||||||
|
from .Calendar import Calendar
|
||||||
|
from .CalendarRule import CalendarRule
|
||||||
|
|
||||||
__updated__ = '2015-05-14'
|
__updated__ = '2015-09-08'
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
Loading…
Reference in New Issue
Block a user