~stoq-dev/stoq/master

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# -*- Mode: Python; coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4

##
## Copyright (C) 2011 Async Open Source <http://www.async.com.br>
## All rights reserved
##
## 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 2 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, write to the Free Software
## Foundation, Inc., or visit: http://www.gnu.org/.
##
## Author(s): Stoq Team <stoq-devel@async.com.br>
##
"""
stoq/gui/calendar.py:

    Calendar application.
"""

import urllib

from dateutil.parser import parse
from dateutil.relativedelta import MO, relativedelta
from dateutil.tz import tzlocal, tzutc
import gtk

from stoqlib.api import api
from stoqlib.domain.person import Client
from stoqlib.gui.editors.callseditor import CallsEditor
from stoqlib.gui.editors.paymenteditor import (InPaymentEditor,
                                               OutPaymentEditor)
from stoqlib.gui.keybindings import get_accels
from stoqlib.gui.stockicons import (STOQ_CALENDAR_TODAY,
                                    STOQ_CALENDAR_WEEK,
                                    STOQ_CALENDAR_MONTH,
                                    STOQ_CALENDAR_LIST)
from stoqlib.gui.webview import WebView
from stoqlib.lib import dateutils
from stoqlib.lib.daemonutils import start_daemon
from stoqlib.lib.defaults import get_weekday_start
from stoqlib.lib.translation import stoqlib_gettext as _

from stoq.gui.application import AppWindow


def parse_javascript_date(jsdate):
    dt = parse(jsdate, fuzzy=True)
    dt = dt.replace(tzinfo=tzlocal())
    date = dt.astimezone(tzutc())
    date += relativedelta(months=-1)
    return date


class CalendarView(WebView):
    def __init__(self, app):
        self._loaded = False
        WebView.__init__(self)
        self.app = app
        self.get_view().connect(
            'load-finished',
            self._on_view__document_load_finished)

        self._load_user_settings()

    def _load_finished(self):
        self._startup()
        self._loaded = True
        view = self.get_view()
        view.connect('size-allocate', self._on_view__size_allocate)
        x, y, width, height = view.get_allocation()
        self._update_calendar_size(width, height)

    def _startup(self):
        options = {}
        options['monthNames'] = dateutils.get_month_names()
        options['monthNamesShort'] = dateutils.get_short_month_names()
        options['dayNames'] = dateutils.get_day_names()
        options['dayNamesShort'] = dateutils.get_short_day_names()
        options['buttonText'] = {"today": _('today'),
                                 "month": _('month'),
                                 "week": _('week'),
                                 "day": _('day')}
        options['defaultView'] = api.user_settings.get(
            'calendar-view', 'month')

        # FIXME: This should not be tied to the language, rather be
        #        picked up from libc, but it's a bit of work to translate
        #        one into another so just take a shortcut
        options['columnFormat'] = {
            # month column format, eg "Mon", see:
            # http://arshaw.com/fullcalendar/docs/text/columnFormat/
            'month': _('ddd'),
            # week column format: eg, "Mon 9/7", see:
            # http://arshaw.com/fullcalendar/docs/text/columnFormat/
            'week': _('ddd M/d'),
            # day column format : eg "Monday 9/7", see:
            # http://arshaw.com/fullcalendar/docs/text/columnFormat/
            'day': _('dddd M/d'),
            }

        options['timeFormat'] = {
            # for agendaWeek and agendaDay, eg "5:00 - 6:30", see:
            # http://arshaw.com/fullcalendar/docs/text/timeFormat/
            'agenda': _('h:mm{ - h:mm}'),
            # for all other views, eg "7p", see:
            # http://arshaw.com/fullcalendar/docs/text/timeFormat/
            '': _('h(:mm)t'),
            }

        options['titleFormat'] = {
            # month title, eg "September 2009", see:
            # http://arshaw.com/fullcalendar/docs/text/titleFormat/
            'month': _('MMMM yyyy'),
            # week title, eg "Sep 7 - 13 2009" see:
            # http://arshaw.com/fullcalendar/docs/text/titleFormat/
            'week': _("MMM d[ yyyy]{ '&#8212;'[ MMM] d yyyy}"),
            # day time, eg "Tuesday, Sep 8, 2009" see:
            # http://arshaw.com/fullcalendar/docs/text/titleFormat/
            'day': _('dddd, MMM d, yyyy'),
            }

        if get_weekday_start() == MO:
            firstday = 1
        else:
            firstday = 0

        options['firstDay'] = firstday
        options['isRTL'] = (
            gtk.widget_get_default_direction() == gtk.TEXT_DIR_RTL)
        options['data'] = self._show_events
        options['loading_msg'] = _('Loading calendar content, please wait...')
        self.js_function_call('startup', options)
        self._update_title()

    def _calendar_run(self, name, *args):
        if not self._loaded:
            return
        self.js_function_call("$('#calendar').fullCalendar", name, *args)

    def _load_daemon_path(self, path):
        uri = '%s/%s' % (self._daemon_uri, path)
        self.load_uri(uri)

    def _load_user_settings(self):
        events = api.user_settings.get('calendar-events', {})
        self._show_events = dict(
            in_payments=events.get('in-payments', True),
            out_payments=events.get('out-payments', True),
            purchase_orders=events.get('purchase-orders', True),
            client_calls=events.get('client-calls', True),
            )

    def _save_user_settings(self):
        events = api.user_settings.get('calendar-events', {})
        events['in-payments'] = self._show_events['in_payments']
        events['out-payments'] = self._show_events['out_payments']
        events['purchase-orders'] = self._show_events['purchase_orders']
        events['client-calls'] = self._show_events['client_calls']

    def _update_calendar_size(self, width, height):
        self._calendar_run('option', 'aspectRatio', float(width) / height)

    def _update_title(self):
        # Workaround to get the current calendar date
        view = self.get_view()
        view.execute_script("document.title = $('.fc-header-title').text()")
        title = view.get_property('title')
        self.app.date_label.set_markup(
            '<big><b>%s</b></big>' % api.escape(title))

    #
    # Callbacks
    #

    def _on_view__document_load_finished(self, view, frame):
        self._load_finished()

    def _on_view__size_allocate(self, widget, req):
        x, y, width, height = req
        self._update_calendar_size(width, height)

    #
    # WebView
    #

    def web_open_uri(self, kwargs):
        if kwargs['method'] == 'changeView':
            view = kwargs['view']
            if view == 'basicDay':
                self.app.ViewDay.set_active(True)
                jsdate = urllib.unquote(kwargs['date'])
                date = parse_javascript_date(jsdate)
                self._calendar_run('gotoDate', date.year, date.month, date.day)
    #
    # Public API
    #

    def set_daemon_uri(self, uri):
        self._daemon_uri = uri

    def load(self):
        self._load_daemon_path('web/static/calendar-app.html')

    def go_prev(self):
        self._calendar_run('prev')
        self._update_title()

    def show_today(self):
        self._calendar_run('today')
        self._update_title()

    def go_next(self):
        self._calendar_run('next')
        self._update_title()

    def change_view(self, view_name):
        self._calendar_run('removeEvents')
        self._calendar_run('changeView', view_name)
        self._calendar_run('refetchEvents')
        api.user_settings.set('calendar-view', view_name)
        self._update_title()

    def refresh(self):
        self.load()

    def get_events(self):
        return self._show_events

    def update_events(self, **events):
        self._show_events.update(**events)
        if not self._loaded:
            return
        self.js_function_call("update_options", self._show_events)

        self._calendar_run('refetchEvents')
        self._save_user_settings()


class CalendarApp(AppWindow):

    app_name = _('Calendar')
    gladefile = 'calendar'
    embedded = True

    def __init__(self, app, store=None):
        self._calendar = CalendarView(self)
        AppWindow.__init__(self, app, store=store)
        self._setup_daemon()

    @api.async
    def _setup_daemon(self):
        daemon = yield start_daemon()
        self._calendar.set_daemon_uri(daemon.base_uri)

        proxy = daemon.get_client()
        yield proxy.callRemote('start_webservice')
        self._calendar.load()

    #
    # AppWindow overrides
    #

    def create_actions(self):
        group = get_accels('app.calendar')
        actions = [
            # File
            ('NewClientCall', None, _("Client call"),
             group.get('new_client_call'), _("Add a new client call")),
            ('NewPayable', None, _("Account payable"),
             group.get('new_payable'), _("Add a new account payable")),
            ('NewReceivable', None, _("Account receivable"),
             group.get('new_receivable'), _("Add a new account receivable")),
            # View
            ('Back', gtk.STOCK_GO_BACK, _("Back"),
             group.get('go_back'), _("Go back")),
            ('Forward', gtk.STOCK_GO_FORWARD, _("Forward"),
             group.get('go_forward'), _("Go forward")),
            ('Today', STOQ_CALENDAR_TODAY, _("Show today"),
             group.get('show_today'), _("Show today")),
            ('CalendarEvents', None, _("Calendar events")),
            ('CurrentView', None, _("Display view as")),
            ]
        self.calendar_ui = self.add_ui_actions('', actions,
                                                filename='calendar.xml')
        self.set_help_section(_("Calendar help"), 'app-calendar')

        toggle_actions = [
            ('AccountsPayableEvents', None, _("Accounts payable"),
             None, _("Show accounts payable in the list")),
            ('AccountsReceivableEvents', None, _("Accounts receivable"),
             None, _("Show accounts receivable in the list")),
            ('PurchaseEvents', None, _("Purchases"),
             None, _("Show purchases in the list")),
            ('ClientCallEvents', None, _("Client Calls"),
             None, _("Show client calls in the list")),
            ]
        self.add_ui_actions('', toggle_actions, 'ToggleActions',
                            'toggle')

        events_info = dict(
            in_payments=(self.AccountsReceivableEvents, self.NewReceivable,
                         u'receivable'),
            out_payments=(self.AccountsPayableEvents, self.NewPayable,
                          u'payable'),
            purchase_orders=(self.PurchaseEvents, None, u'stock'),
            client_calls=(self.ClientCallEvents, self.NewClientCall, u'sales'),
        )

        user = api.get_current_user(self.store)
        events = self._calendar.get_events()
        for event_name, value in events_info.items():
            view_action, new_action, app = value
            view_action.props.active = events[event_name]
            # Disable feature if user does not have acces to required
            # application
            if not user.profile.check_app_permission(app):
                view_action.props.active = False
                view_action.set_sensitive(False)
                if new_action:
                    new_action.set_sensitive(False)

            view_action.connect('notify::active', self._update_events)
        self._update_events()

        radio_actions = [
            ('ViewMonth', STOQ_CALENDAR_MONTH, _("View as month"),
             '', _("Show one month")),
            ('ViewWeek', STOQ_CALENDAR_WEEK, _("View as week"),
             '', _("Show one week")),
            ('ViewDay', STOQ_CALENDAR_LIST, _("View as day"),
             '', _("Show one day")),
            ]
        self.add_ui_actions('', radio_actions, 'RadioActions',
                            'radio')
        self.ViewMonth.set_short_label(_("Month"))
        self.ViewWeek.set_short_label(_("Week"))
        self.ViewDay.set_short_label(_("Day"))
        self.ViewMonth.props.is_important = True
        self.ViewWeek.props.is_important = True
        self.ViewDay.props.is_important = True

        view = api.user_settings.get('calendar-view', 'month')
        if view == 'month':
            self.ViewMonth.props.active = True
        elif view == 'basicWeek':
            self.ViewWeek.props.active = True
        else:
            self.ViewDay.props.active = True

    def create_ui(self):
        self.app.launcher.add_new_items([self.NewClientCall,
                                         self.NewPayable,
                                         self.NewReceivable])

        # Reparent the toolbar, to show the date next to it.
        self.hbox = gtk.HBox()
        toolbar = self.uimanager.get_widget('/toolbar')
        toolbar.reparent(self.hbox)

        # A label to show the current calendar date.
        self.date_label = gtk.Label('')
        self.date_label.show()
        self.hbox.pack_start(self.date_label, False, False, 6)
        self.hbox.show()

        self.main_vbox.pack_start(self.hbox, False, False)

        self.main_vbox.pack_start(self._calendar)
        self._calendar.show()
        self.app.launcher.Print.set_tooltip(_("Print this calendar"))

    def activate(self, params):
        self.app.launcher.SearchToolItem.set_sensitive(False)
        # FIXME: Are we 100% sure we can always print something?
        # self.app.launcher.Print.set_sensitive(True)

    def deactivate(self):
        # Put the toolbar back at where it was
        main_vbox = self.app.launcher.main_vbox
        toolbar = self.uimanager.get_widget('/toolbar')
        self.hbox.remove(toolbar)
        main_vbox.pack_start(toolbar, False, False)
        main_vbox.reorder_child(toolbar, 1)

        self.uimanager.remove_ui(self.calendar_ui)
        self.app.launcher.SearchToolItem.set_sensitive(True)

    # Private

    def _update_events(self, *args):
        self._calendar.update_events(
            out_payments=self.AccountsPayableEvents.get_active(),
            in_payments=self.AccountsReceivableEvents.get_active(),
            purchase_orders=self.PurchaseEvents.get_active(),
            client_calls=self.ClientCallEvents.get_active(),
            )

    def _new_client_call(self):
        with api.trans() as store:
            self.run_dialog(CallsEditor, store, None, None, Client)

        if store.committed:
            self._update_events()

    def _new_payment(self, editor):
        with api.trans() as store:
            self.run_dialog(editor, store)

        if store.committed:
            self._update_events()

    #
    # Kiwi callbacks
    #

    # Toolbar

    def new_activate(self):
        if not self.NewClientCall.get_sensitive():
            return
        self._new_client_call()

    def print_activate(self):
        self._calendar.print_()

    def export_spreadsheet_activate(self):
        pass

    def on_NewClientCall__activate(self, action):
        self._new_client_call()

    def on_NewPayable__activate(self, action):
        self._new_payment(OutPaymentEditor)

    def on_NewReceivable__activate(self, action):
        self._new_payment(InPaymentEditor)

    def on_Back__activate(self, action):
        self._calendar.go_prev()

    def on_Today__activate(self, action):
        self._calendar.show_today()

    def on_Forward__activate(self, action):
        self._calendar.go_next()

    def on_ViewMonth__activate(self, action):
        self._calendar.change_view('month')

    def on_ViewWeek__activate(self, action):
        self._calendar.change_view('basicWeek')

    def on_ViewDay__activate(self, action):
        self._calendar.change_view('basicDay')