-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
97 lines (84 loc) · 3.72 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: UTF-8 -*-
# Copyright (C) 2011 Taverne Sylvain <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import from standard library
from datetime import date, datetime, time
# Import from itools
from itools.core import freeze
from itools.datatypes import Date, URI
from itools.gettext import MSG
# Import from ikaaro
from ikaaro.autoform import ImageSelectorWidget, DateWidget, TextWidget
# Import from itws
from itws.datatypes import TimeWithoutSecond
from itws.tags import TagsList
from itws.widgets import DualSelectWidget
class TagsAware_Edit(object):
"""Mixin to merge with the TagsAware edit view.
"""
# Little optimisation not to compute the schema too often
keys = ['tags', 'pub_datetime', 'pub_date', 'pub_time']
# Publication datetime is not mandatory
pub_datetime_mandatory = False
def _get_schema(self, resource, context):
pdm = self.pub_datetime_mandatory
return freeze({
'tags': TagsList(multiple=True, states=[]),
'pub_date': Date(mandatory=pdm),
'pub_time': TimeWithoutSecond(mandatory=pdm),
'thumbnail': URI(multilingual=True)})
def _get_widgets(self, resource, context):
return freeze(
[DualSelectWidget('tags', title=MSG(u'Tags'), is_inline=True,
has_empty_option=False),
ImageSelectorWidget('thumbnail', title=MSG(u'Thumbnail')),
DateWidget('pub_date',
title=MSG(u'Publication date (used by RSS and tags)')),
TextWidget('pub_time', tip=MSG(u'hour:minute'), size=5,
maxlength=5,
title=MSG(u'Publication time (used by RSS and tags)'))])
def get_value(self, resource, context, name, datatype):
if name == 'tags':
tags = resource.get_property('tags')
# tuple -> list (enumerate.get_namespace expects list)
return list(tags)
elif name in ('pub_date', 'pub_time'):
pub_datetime = resource.get_property('pub_datetime')
if pub_datetime is None:
return None
pub_datetime = context.fix_tzinfo(pub_datetime)
if name == 'pub_date':
return date(pub_datetime.year, pub_datetime.month,
pub_datetime.day)
else:
return time(pub_datetime.hour, pub_datetime.minute)
def set_value(self, resource, context, name, form):
if name == 'tags':
resource.set_property('tags', form['tags'])
elif name in ('pub_date', 'pub_time'):
pub_date = form['pub_date']
pub_time = form['pub_time']
if pub_date:
dt_kw = {}
if pub_time:
dt_kw = {'hour': pub_time.hour,
'minute': pub_time.minute}
dt = datetime(pub_date.year, pub_date.month, pub_date.day,
**dt_kw)
dt = context.fix_tzinfo(dt)
resource.set_property('pub_datetime', dt)
else:
resource.del_property('pub_datetime')
return False