Source code for stoq.gui.calendar

# -*- Mode: Python; coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4

## Copyright (C) 2011 Async Open Source <>
## 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
## 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:
## Author(s): Stoq Team <>

    Calendar application.

import urllib

from dateutil.parser import parse
from dateutil.relativedelta import MO, relativedelta
from 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,
from stoqlib.gui.editors.workordereditor import WorkOrderEditor
from stoqlib.gui.stockicons import (STOQ_CALENDAR_TODAY,
from stoqlib.gui.utils.keybindings import get_accels
from stoqlib.gui.widgets.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.threadutils import (threadit,
from stoqlib.lib.translation import stoqlib_gettext as _

from import ShellApp

[docs]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
[docs]class CalendarView(WebView): def __init__(self, app): self._loaded = False WebView.__init__(self) = 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: # 'month': _('ddd'), # week column format: eg, "Mon 9/7", see: # 'week': _('ddd M/d'), # day column format : eg "Monday 9/7", see: # 'day': _('dddd M/d'), } options['timeFormat'] = { # for agendaWeek and agendaDay, eg "5:00 - 6:30", see: # 'agenda': _('h:mm{ - h:mm}'), # for all other views, eg "7p", see: # '': _('h(:mm)t'), } options['titleFormat'] = { # month title, eg "September 2009", see: # 'month': _('MMMM yyyy'), # week title, eg "Sep 7 - 13 2009" see: # 'week': _("MMM d[ yyyy]{ '&#8212;'[ MMM] d yyyy}"), # day time, eg "Tuesday, Sep 8, 2009" see: # '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), client_birthdays=events.get('client-birthdays', True), work_orders=events.get('work-orders', 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'] events['client-birthdays'] = self._show_events['client_birthdays'] events['work-orders'] = self._show_events['work_orders'] 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') '<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 #
[docs] def web_open_uri(self, kwargs): if kwargs['method'] == 'changeView': view = kwargs['view'] if view == 'basicDay': jsdate = urllib.unquote(kwargs['date']) date = parse_javascript_date(jsdate) self._calendar_run('gotoDate', date.year, date.month,
# # Public API #
[docs] def set_daemon_uri(self, uri): self._daemon_uri = uri
[docs] def load(self): self._load_daemon_path('static/calendar-app.html')
[docs] def go_prev(self): self._calendar_run('prev') self._update_title()
[docs] def show_today(self): self._calendar_run('today') self._update_title()
[docs] def go_next(self): self._calendar_run('next') self._update_title()
[docs] 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()
[docs] def refresh(self): self.load()
[docs] def get_events(self): return self._show_events
[docs] 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()
[docs]class CalendarApp(ShellApp): app_title = _('Calendar') gladefile = 'calendar' def __init__(self, window, store=None): # Create this here because CalendarView will update it. # It will only be shown on create_ui though self.date_label = gtk.Label('') self._calendar = CalendarView(self) ShellApp.__init__(self, window, store=store) threadit(self._setup_daemon) def _setup_daemon(self): daemon = start_daemon() assert daemon.running self._calendar.set_daemon_uri(daemon.server_uri) schedule_in_main_thread(self._calendar.load) # # ShellApp overrides #
[docs] 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")), ('NewWorkOrder', None, _("Work order"), group.get('new_work_order'), _("Add a new work order")), # 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")), ('ClientBirthdaysEvents', None, _("Client Birthdays"), None, _("Show client birthdays in the list")), ('WorkOrderEvents', None, _("Work orders"), None, _("Show work orders 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'), client_birthdays=(self.ClientBirthdaysEvents, None, u'sales'), work_orders=(self.WorkOrderEvents, self.NewWorkOrder, u'services'), ) user = api.get_current_user( events = self._calendar.get_events() for event_name, value in events_info.items(): view_action, new_action, app = value = events[event_name] # Disable feature if user does not have acces to required # application if not user.profile.check_app_permission(app): = 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': = True elif view == 'basicWeek': = True else: = True
[docs] def create_ui(self): self.window.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.hbox.pack_start(self.date_label, False, False, 6) self.main_vbox.pack_start(self.hbox, False, False) self.main_vbox.pack_start(self._calendar) self.window.Print.set_tooltip(_("Print this calendar"))
[docs] def activate(self, refresh=True): self.window.SearchToolItem.set_sensitive(False)
# FIXME: Are we 100% sure we can always print something? # self.window.Print.set_sensitive(True)
[docs] def deactivate(self): # Put the toolbar back at where it was main_vbox = self.window.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.window.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(), client_birthdays=self.ClientBirthdaysEvents.get_active(), work_orders=self.WorkOrderEvents.get_active(), ) def _new_client_call(self): with api.new_store() as store: self.run_dialog(CallsEditor, store, None, None, Client) if store.committed: self._update_events() def _new_work_order(self): with api.new_store() as store: self.run_dialog(WorkOrderEditor, store) if store.committed: self._update_events() def _new_payment(self, editor): with api.new_store() as store: self.run_dialog(editor, store) if store.committed: self._update_events() # # Kiwi callbacks # # Toolbar
[docs] def new_activate(self): if not self.NewClientCall.get_sensitive(): return self._new_client_call()
[docs] def print_activate(self): self._calendar.print_()
[docs] def export_spreadsheet_activate(self): pass
[docs] def on_NewClientCall__activate(self, action): self._new_client_call()
[docs] def on_NewPayable__activate(self, action): self._new_payment(OutPaymentEditor)
[docs] def on_NewReceivable__activate(self, action): self._new_payment(InPaymentEditor)
[docs] def on_NewWorkOrder__activate(self, action): self._new_work_order()
[docs] def on_Back__activate(self, action): self._calendar.go_prev()
[docs] def on_Today__activate(self, action): self._calendar.show_today()
[docs] def on_Forward__activate(self, action): self._calendar.go_next()
[docs] def on_ViewMonth__activate(self, action): self._calendar.change_view('month')
[docs] def on_ViewWeek__activate(self, action): self._calendar.change_view('basicWeek')
[docs] def on_ViewDay__activate(self, action): self._calendar.change_view('basicDay')