from django.db import models
from django.utils.translation import ugettext_lazy, ugettext as _
import django_countries
import jsonfield.fields
class PublicHolidayManager(models.Manager):
def on(self, date, country, province=None):
"""
If no province is provided, any holiday that falls on that day
for that country will return True
"""
# TODO: handle country names as well as codes.
qs = self.get_query_set().filter(date=date, country=country)
if province:
qs = qs.filter(models.Q(provinces=None) | models.Q(provinces__contains=province))
return qs.exists()
class PublicHoliday(models.Model):
"""
A PublicHoliday is an object that can be associated with a country, and
zero or more provinces. The semantic meaning of no listed provinces is
that the PublicHoliday is nationwide.
The provinces list should be CSV list of provinces from the django
localflavor module.
This is a seperate app, that relies on django-countries, with my
province patches applied, and django-jsonfield.
"""
#: Every PublicHoliday must have a name.
name = models.CharField(max_length=128)
#: PublicHolidays are single-day events, and do not automatically repeat.
date = models.DateField()
#: Each PublicHoliday can only apply to one country.
country = django_countries.CountryField()
#: Each PublicHoliday can apply to all, or a subset of provinces in
#: a country.
provinces = jsonfield.fields.JSONField(null=True, blank=True,
help_text=_(u'No selected provinces means that all provinces are affected.'))
objects = PublicHolidayManager()
class Meta:
app_label = 'public_holidays'
verbose_name = u'public holiday'
verbose_name_plural = u'public holidays'
ordering = ('date', 'country')
unique_together = (
('name', 'date', 'country'),
)
def __unicode__(self):
return "%s (%s) [%s]" % (self.name, self.date.year, self.country)
def public_holiday_on(date, country, province):
return PublicHoliday.objects.on(date, country, province)
挣扎的蓝藻
- 粉丝: 14w+
- 资源: 15万+
最新资源
- java.中间件.md
- java.缓存Redis.md
- SystemExit(解决方案).md
- runtime_error如何解决.md
- RenderTargetNotFoundException解决办法.md
- SystemStackError(解决方案).md
- type_error如何解决.md
- java.消息队列Kafka.md
- RenderTargetUnavailableException解决办法.md
- index_error如何解决.md
- value_error如何解决.md
- RenderingTimeoutException解决办法.md
- LocalJumpError(解决方案).md
- java.RabbitMQ.md
- java.日志与监控.md
- VertexBufferOverflowException解决办法.md
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈