-
Notifications
You must be signed in to change notification settings - Fork 1
/
weather.py
94 lines (69 loc) · 2.44 KB
/
weather.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
# -*- coding: utf-8 -*-
from collective.weather import _
from collective.weather.interfaces import IWeatherUtility
from plone.app.portlets.portlets import base
from plone.portlets.interfaces import IPortletDataProvider
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from zope import schema
from zope.component import getUtility
from zope.formlib import form
from zope.interface import implements
from zope.schema.interfaces import IVocabularyFactory
class IWeatherPortlet(IPortletDataProvider):
'''A weather portlet.
'''
header = schema.TextLine(
title=_(u'Portlet header'),
description=_(u'Title of the rendered portlet.'),
required=True,
)
location = schema.Choice(
title=_(u'Location'),
description=_(u'Choose one of the preconfigured locations.'),
required=True,
vocabulary='collective.weather.Locations',
)
class Assignment(base.Assignment):
'''Portlet assignment.
'''
implements(IWeatherPortlet)
header = u''
location = u''
def __init__(self, header=u'', location=u''):
self.header = header
self.location = location
@property
def title(self):
'''This property is used to give the title of the portlet in the
'manage portlets' screen.
'''
return self.header
class Renderer(base.Renderer):
'''Portlet renderer.
'''
render = ViewPageTemplateFile('weather.pt')
def update(self):
weather_utility = getUtility(IWeatherUtility)
factory = getUtility(
IVocabularyFactory, name='collective.weather.Locations')
vocab = factory(self.context)
self.current_city = None
if self.data.location in vocab:
location = vocab.by_value[self.data.location]
self.current_city = {'id': location.value, 'name': location.title}
self.weather_info = None
if not self.current_city is None:
weather_utility.update_weather_info(self.current_city['id'])
self.weather_info = weather_utility.get_weather_info(self.current_city)
class AddForm(base.AddForm):
'''Portlet add form.
'''
form_fields = form.Fields(IWeatherPortlet)
label = _(u'Add Weather Portlet')
def create(self, data):
return Assignment(**data)
class EditForm(base.EditForm):
'''Portlet edit form.
'''
form_fields = form.Fields(IWeatherPortlet)
label = _(u'Edit Weather Portlet')