-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
694 lines (592 loc) · 21 KB
/
main.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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
# coding: utf-8
# This file is part of Cae Mobile.
#
# Cae Mobile 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.
#
# Cae Mobile 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 Cae Mobile. If not, see <http://www.gnu.org/licenses/>.
''' Kivy interface to manage expenses in autonomie
'''
import json
import random
import datetime
import dateutil.parser
import subprocess
from functools import partial
from ConfigParser import SafeConfigParser
from kivy.app import App
from kivy.logger import Logger
from kivy.uix.screenmanager import (
Screen,
)
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.factory import Factory
from kivy.clock import Clock
from kivy.uix.widget import Widget
from kivy.utils import platform
from kivy.properties import (
BooleanProperty,
ListProperty,
NumericProperty,
ObjectProperty,
StringProperty,
DictProperty,
)
from connection import Connection
from utils import (
read_locale_date,
write_locale_date,
get_base_url,
get_action_path_and_method,
filter_expenses,
)
platform = platform()
DEFAULTSETTINGSFILE = 'default_config.ini'
SETTINGSFILE = 'config.ini'
SETTINGS_FILES = DEFAULTSETTINGSFILE, SETTINGSFILE
__version__ = '0.02'
if platform == 'android':
from jnius import autoclass
Intent = autoclass('android.content.Intent')
PythonActivity = autoclass('org.renpy.android.PythonActivity')
String = autoclass('java.lang.String')
Uri = autoclass('android.net.Uri')
class SyncPopup(Popup):
''' This popup display the current syncing state in the app
'''
progress = NumericProperty(0)
done = BooleanProperty(False)
errors = ListProperty([])
class SelectPrefilPopup(Popup):
entries = ListProperty([])
def on_entries(self, *args):
# we may be not open yet, delay to next frame
Clock.schedule_once(self.populate, 0)
def populate(self, *args):
print self.entries
self.ids.container.clear_widgets()
for e in self.entries:
self.ids.container.add_widget(
Factory.PrefilEntry(entry=e, popup=self))
class PrefilEntry(Button):
entry = ObjectProperty(None)
popup = ObjectProperty(None)
class ExpenseDispatcher(Widget):
expense = DictProperty({})
class ExpensePool(list):
"""
The pool of expenses, handles the sync status and other
particular stuff
"""
ids = []
def load(self, elements):
"""
Load expenses from a json string
"""
for elem in json.loads(elements):
# We store the ids to be able to handle unicity
self.ids.append(elem['local_id'])
self.append(elem)
def merge(self, expense_dict):
if 'local_id' in expense_dict:
return self.update_expense(expense_dict)
else:
return self.add_expense(expense_dict)
def add_expense(self, expense_dict):
"""
add an expense to our pool
"""
Logger.debug("Ndf : Storing an expense %s" % expense_dict)
expense_dict['local_id'] = self.get_uniq_id()
self.append(expense_dict)
expense_dict['todo'] = 'add'
return expense_dict
def update_expense(self, expense_dict):
"""
Update an expense
"""
Logger.debug("Ndf : Updating an expense %s" % expense_dict)
index, expense = self.get_expense_by_local_id(expense_dict['local_id'])
expense.update(expense_dict)
if 'id' in expense:
expense['todo'] = 'update'
expense['synced'] = False
return expense
def del_expense(self, expense_dict):
"""
Ask for expense deletion
"""
index, expense = self.get_expense_by_local_id(expense_dict['local_id'])
expense['synced'] = False
expense['todo'] = 'delete'
return expense
def remove(self, expense_dict):
"""
Remove an expense from the pool (after deletion)
"""
index, expense = self.get_expense_by_local_id(expense_dict['local_id'])
return self.pop(index)
def get_expense_by_local_id(self, local_id):
"""
Return an expense given its local id
"""
for index, elem in enumerate(self):
if elem['local_id'] == local_id:
return index, elem
# Should not happen
return None
def tosync(self):
"""
Return the list of elements to be synchronized
"""
return [elem for elem in self if not elem.get('synced', False)]
def get_uniq_id(self):
"""
Return a unique id
"""
temp = random.randint(0, 10000)
while temp in self.ids:
temp = random.randint(0, 10000)
return temp
def stored_version(self):
"""
Return this object as a json string
"""
return json.dumps(self)
class RestRequest(object):
def __init__(self, request, resp):
self.request = request
self.resp = resp
self.code = self.request.resp_status
if hasattr(self.resp, 'get'):
self.status = self.resp.get('status')
self.result = self.resp.get('result', {})
self.errors = self.resp.get('errors', {})
else:
self.status = None
self.result = {}
self.errors = {}
self.success = self.status == 'success'
class NdfApp(App):
"""
The main application object
Handle the settings and the expense add/edit/delete
"""
settings = ObjectProperty()
pool = ObjectProperty()
expenses = ObjectProperty()
def build(self):
"""
The build method (specific to kivy)
"""
settings = self.load_settings()
# need to load config *before* assigning to self.settings
self.settings = settings
self.manager = self.root.ids.screenmanager
self.expenses = self.manager.get_screen('expenses')
self.load_expenses()
if not self.is_configured():
self.manager.transition.direction = 'down'
self.manager.current = 'settings'
return self.root
def is_configured(self):
"""
Check if the application is fully configured
"""
for key in ('server', 'login', 'password'):
if not self.settings.get('settings', key):
return False
return True
def load_settings(self):
"""
Load the settings from the ini files
"""
settings = SafeConfigParser()
loaded_settings = settings.read(SETTINGS_FILES)
Logger.debug("Ndf: loaded settings: %s", loaded_settings)
return settings
def load_expenses(self):
"""
Build our expense pool and pass it to the expenselistscreen
"""
self.pool = ExpensePool()
self.pool.load(self.settings.get('main', 'expenses'))
self.property('pool').dispatch(self)
self.expenses.data = self.pool
def get_connection(self):
"""
Return a connection object
"""
return Connection(
login=self.settings.get('settings', 'login'),
password=self.settings.get('settings', 'password'),
server=self.settings.get('settings', 'server'),
to_sync=self.settings.items('tosync'),
)
def check_configuration(self):
"""
Entry point for app configuration
"""
self.check_auth()
def check_auth(self):
"""
Launch an authentication check
"""
Logger.info("Ndf : Checking auth : NDFAPP")
_connection = self.get_connection()
_connection.check_auth(self.check_auth_success, self.check_auth_error)
def check_auth_success(self, request, resp):
"""
Launched if the authentication test succeeded
"""
rest_req = RestRequest(request, resp)
if rest_req.code == 301:
self.check_auth_redirect(request, resp)
elif rest_req.success:
Logger.info("Ndf : Authentication test succeeded")
self.fetch_options()
else:
Logger.info("Ndf : Authentication test failed")
self.check_auth_error()
def check_auth_error(self, *args):
"""
Launched if the authentication test failed
"""
self.dialog(
title=u"Erreur d'authentification",
text=u"Veuillez vérifier votre configuration"
)
def check_auth_redirect(self, request, resp):
"""
Launch if the authentication test faced a redirect
"""
# 1- Change the url
# 2- Launch the check_auth again
Logger.info("Ndf : Page has moved permanently, we change the url")
#The page has moved permanently
url = self.settings.get('settings', 'server')
new_url = request.resp_headers.get('location', url)
new_url = get_base_url(new_url)
self.settings.set('settings', 'server', new_url)
self.property('settings').dispatch(self)
self.check_auth()
def fetch_options(self, silent=False):
"""
Fetch options for expense configuration
"""
path = "expenseoptions"
conn = self.get_connection()
success = partial(self.fetch_options_success, silent)
error = partial(self.fetch_options_error, silent)
conn.request(
path,
{},
on_success=success,
on_error=error)
def fetch_options_success(self, silent, request, resp):
"""
Fetch options success handler
"""
rest_req = RestRequest(request, resp)
Logger.info("Nd : Get back : %s" % resp)
if rest_req.success:
self.store_options(rest_req.result)
else:
self.fetch_options_error(silent, request, resp)
if not silent:
self.dialog(
title=u"Configuration réussie",
text=u"Votre application a bien été configurée")
def fetch_options_error(self, silent, request, resp):
"""
Error while fetching options
"""
Logger.info("Nd : Get back : %s" % resp)
if not silent:
self.dialog(
title=u"Erreur à la configuration",
text=u"Une erreur inconnue a été rencontrée lors de la "
"configuration de l'application.")
def store_options(self, options):
"""
Store expensetypes (options) retrieved from the server
"""
Logger.info("Ndf : Storing options %s" % options)
if not self.settings.has_section('main'):
self.settings.add_section('main')
self.settings.set('main', 'expensetypes', json.dumps(options))
self.property('settings').dispatch(self)
def sync_datas(self):
"""
Sync the pending expenses to the remote server
"""
# Handle deletion
#path = "expenses"
conn = self.get_connection()
Logger.info("Fetching server options")
self.fetch_options(silent=True)
Logger.info("Done fetching options")
Logger.info("Sending expense updates")
for expense in self.pool.tosync():
self.sync_expense(expense, conn=conn)
def sync_expense(self, expense, conn=None):
"""
Sync the given expense
"""
if conn is None:
conn = self.get_connection()
Logger.debug("Ndf : Syncing %s", expense)
path, method = get_action_path_and_method(expense)
success = partial(self.sync_success, expense)
error = partial(self.sync_error, expense)
conn.request(
path,
expense,
on_success=success,
on_error=error,
method=method)
Logger.debug("Done syncing %s", expense)
def sync_success(self, expense, req, resp):
"""
Successfull synchronisation
"""
rest_req = RestRequest(req, resp)
if rest_req.success:
Logger.info("Ndf : Synchronisation was successfull")
Logger.info("Ndf : %s" % resp)
if expense['todo'] == 'delete':
self.pool.remove(expense)
else:
expense.update(rest_req.result)
expense['synced'] = True
self.pool_updated()
Logger.info("Ndf : %s" % self.pool)
else:
self.sync_error(expense, req, resp)
def sync_error(self, expense, req, resp):
"""
Error while synchronizing
"""
Logger.error("Ndf : Synchronization error")
Logger.error("Ndf : error code : %s" % req.resp_status)
Logger.error("Ndf : %s" % resp)
rest_req = RestRequest(req, resp)
if rest_req.code == 404:
# This expense is not know anymore
if expense['todo'] == 'delete':
self.pool.remove(expense)
self.pool_updated()
else:
# This expense is not know on the server side, we pop its id to
# be able to simply add it next time
expense.pop('id')
expense['todo'] = 'add'
self.sync_expense(expense)
return
elif rest_req.status == 'error':
#errors = rest_req.resp
msg = u"Une erreur est survenue lors de la synchronisation de " \
u"vos données"
for key, value in rest_req.errors.items():
msg += u'\n'
msg += u"{key} : {value}".format(key=key, value=value)
else:
msg = u"Une erreur inconnue est survenue lors de la " \
u"synchronisation de vos données : \n %s" % resp
self.dialog("Erreur de synchronisation", msg)
def pool_updated(self):
"""
Called when the expense pool has been updated
"""
Logger.debug("Ndf Pool : pool updated")
self.settings.set('main', 'expenses', self.pool.stored_version())
self.property('pool').dispatch(self)
def dialog(self, title, text):
"""
Fires a simple dialog popup
"""
content = BoxLayout(orientation='vertical')
content.add_widget(Factory.NormalLabel(text=text))
btnclose = Factory.NormalButton(text="Fermer",
size_hint_y=None,
height='40sp')
content.add_widget(btnclose)
popup_ = Factory.NormalPopup(title=title, content=content)
btnclose.bind(on_release=popup_.dismiss)
popup_.open()
def on_pause(self, *args):
''' Implement on_pause to save data before going to sleep on android.
'''
return self.save_settings()
def on_stop(self, *args):
''' Called when the application is stopped, save data.
'''
return self.save_settings()
def save_settings(self):
with open(SETTINGSFILE, 'w') as f:
self.settings.write(f)
return True
def on_resume(self, *args):
''' Allow resuming app
'''
return True
def on_pool(self, *args):
Logger.debug("Ndf Pool : The pool update event has been received")
if self.expenses is not None:
Logger.debug("%s" % self.pool)
self.expenses.data = []
self.expenses.data = self.pool
def store_expense(self, screen_name, expense):
Logger.debug("Storing an expense : %s" % expense)
screen = self.manager.get_screen(screen_name)
# TODO: add validation
if expense:
self.pool.merge(expense)
self.pool_updated()
self.settings.set('main', 'expenses', self.pool.stored_version())
screen.expense = {}
def edit_expense(self, index):
expense = self.expenses.data[index]
name = "expense{0}".format(index)
if self.manager.has_screen(name):
self.manager.remove_widget(self.manager.get_screen(name))
if 'start' in expense:
form = KmEditFormScreen
else:
form = CommonEditFormScreen
view = form(
name=name,
expense=expense)
self.manager.add_widget(view)
self.manager.transition.direction = 'left'
self.manager.current = view.name
def delete_expense(self, expense):
self.pool.del_expense(expense)
self.pool_updated()
self.settings.set('main', 'expenses', self.pool.stored_version())
def select_expenses(self, expense):
"""returns a list of expenses with the same caracteristics as
the passed one
"""
if not self.expenses:
return
keys = (
'category',
'end',
'ht',
'km',
'start',
'tva',
'type_id',
'type',
'description',
'transport',
)
for e in filter_expenses(expense, self.expenses.data, keys):
yield e
def open_mail_link(self, mail):
if platform == 'linux':
process = subprocess.Popen(["xdg-email", mail])
Logger.debug("Spawned external mailer process: PID %i", process.pid)
return
if platform == 'android':
intent = Intent(Intent.ACTION_SENDTO,
Uri.parse('mailto:%s' % mail))
PythonActivity.mActivity.startActivity(
Intent.createChooser(
intent, String("Envoyer un mail à %s" % mail)))
def open_link(self, url):
if platform == 'linux':
process = subprocess.Popen(["xdg-open", url])
Logger.debug(
"Spawned external process: Url: %s - PID %i",
url, process.pid)
return
if platform == 'android':
intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
PythonActivity.mActivity.startActivity(
Intent.createChooser(
intent, String("Ouverture de l'url %s" % url)))
class ExpenseFormScreen(Screen):
expense = DictProperty({})
def set_value(self, key, value, *args):
Logger.debug(u"Ndf : Setting a value for {0} : {1}".format(key, value))
Logger.debug("Ndf : Alternative options : {0}".format(args))
if hasattr(self, 'on_%s' % key):
value = getattr(self, 'on_%s' % key)(value, *args)
self.expense[key] = value
def on_type(self, value, options):
"""
Set the type of the expense
"""
for option in options:
Logger.debug("Ndf : option : %s" % option)
if option['label'] == value:
self.set_value('type_id', option['value'])
break
return value
def on_date(self, value):
"""
Set the date
"""
Logger.debug("Ndf : Altering the date value")
if len(value) != 10:
Logger.debug("Ndf : date format is invalid, expecting ddmmyyyy")
value = ""
else:
try:
date = read_locale_date(value)
except:
date = datetime.date.today()
value = date.isoformat()
return value
def get_date(self):
"""
return the current date (today as default)
"""
if self.expense.get('date'):
date = dateutil.parser.parse(self.expense.get('date'))
else:
date = datetime.date.today()
self.set_value('date', date.isoformat())
return write_locale_date(date)
class KmAddFormScreen(ExpenseFormScreen):
pass
class KmEditFormScreen(ExpenseFormScreen):
"""
Form used to edit km expenses
"""
pass
class CommonAddFormScreen(ExpenseFormScreen):
pass
class CommonEditFormScreen(ExpenseFormScreen):
pass
class ExpenseListItem(BoxLayout):
index = NumericProperty()
description = StringProperty()
todo = StringProperty(allownone=True)
synced = BooleanProperty()
kmtype = BooleanProperty()
date = StringProperty()
class ExpenseListScreen(Screen):
data = ListProperty()
def args_converter(self, row_index, item):
date = write_locale_date(dateutil.parser.parse(item.get('date')))
return {'description': item.get("description", ""),
"synced": item.get('synced', False),
"todo": item.get('todo', ''),
'km_expense': 'km' in item,
'date': date,
'index': row_index}
if __name__ == '__main__':
NdfApp().run()