aboutsummaryrefslogtreecommitdiffstats
path: root/jm2l/views.py
blob: b0be0e0e5af4fd2f85dc53a55db5beb167d45fd5 (plain)
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
# -*- coding: utf8 -*-
from pyramid.httpexceptions import HTTPFound, HTTPNotFound, HTTPForbidden
from pyramid.httpexceptions import HTTPBadRequest, HTTPUnauthorized
from pyramid.renderers import render_to_response
from pyramid.response import Response
from pyramid.view import notfound_view_config, forbidden_view_config
from pyramid.view import view_config
# Import Web Forms
from .forms import *
# Database access imports
from .models import *
from sqlalchemy.exc import DBAPIError
from sqlalchemy import func, or_
# Usefull tools
from slugify import slugify
from icalendar import Calendar
from pytz import timezone
from icalendar import Event as Evt
# Then, standard libs
import webhelpers.paginate as paginate
import unicodedata
import time
import datetime
import re 

CurrentYear = 2015

## =-=- Here, We keep some usefull function -=-=
def remove_accents(input_str):
    """ This function is intended to remove all accent from input unicode string """
    nkfd_form = unicodedata.normalize('NFKD', input_str)
    only_ascii = nkfd_form.encode('ASCII', 'ignore')
    return only_ascii

@view_config(route_name='tester', renderer="jm2l:templates/tester.mako")
def Tester(request):
    JTiers = DBSession.query(Tiers).all()
    MainTab = {'programme':'','presse':'', 'plan':'', 'participer':'',
        'Tiers':JTiers, "logged_in":request.authenticated_userid }
    return MainTab

## =-=- Here, We handle ICal requests -=-=
@view_config(route_name='progr_iCal', renderer="string")
def ICal_Progamme_Request(request):
    year = int(request.matchdict.get('year', CurrentYear))
    # Initialization
    DicResult = dict()
    # Query database
    # Compute days used by all events matching the specified input year
    Events = DBSession.query(Event)\
                .filter(Event.for_year == year)\
                .filter(Event.event_type != 'Stand')\
                .order_by(Event.start_time)
    
    cal = Calendar()
    cal.add('prodid', '-//Programme %d//jm2l.linux-azur.org//' % year)
    cal.add('version', '2.0')
    tz = timezone('Europe/Paris')
    for ev in Events:
        if ev.event_type:
            event = Evt()
            event['uid'] = "%d/%d" % ( year, ev.uid )
            event.add('summary', ev.name )
            event.add('dtstart', ev.start_time.replace(tzinfo=tz) )
            event.add('dtend', ev.end_time.replace(tzinfo=tz) )
            event.add('created', ev.last_change.replace(tzinfo=tz) )
            event.add('description', "http://www.linux-azur.org/event/%s/%s" % (ev.for_year, ev.slug)  )
            event.add('url', "http://www.linux-azur.org/event/%s/%s" % (ev.for_year, ev.slug) )
            event.add('priority', 5)
            cal.add_component(event)
    return cal.to_ical()

## =-=- Here, We handle Json requests -=-= 
@view_config(route_name='users_json', renderer="json")
def JSON_User_Request(request):
    """ Build a JSON answer with active users and pagination handling """
    # Check arguments consitency 
    pageSize = request.params.get('pageSize',"8")
    current_page = request.params.get('pageNum',"1")
    UserQuery = request.params.get('searchTerm', u"")
    # Don't answer to users that aren't logged    
    if not request.user:
        return HTTPUnauthorized('You have to be logged to hope an answer.')
    # Check consistancy of parameters        
    if pageSize.isdigit() and current_page.isdigit():
        current_page = int(current_page)
        pageSize = int(pageSize)
    else:
        return HTTPBadRequest('pageSize and pageNum accept only digits.')    
    # Query database
    Users = DBSession.query(User.uid, User.nom, User.prenom)\
        .filter(User.slug.contains( remove_accents(UserQuery) ))
    page_url = paginate.PageURL_WebOb(request)
    records = paginate.Page(Users, current_page, url=page_url, items_per_page=pageSize)
    ListMatchUser = map( lambda u:{"id": u.uid, "text":"%s %s" % ( u.prenom, u.nom )}, records )
    return { "Results": ListMatchUser, "Total":records.item_count,
            "logged_in":request.authenticated_userid }

@view_config(route_name='tiers_json', renderer="json")
def JSON_Tiers_Request(request):
    """ Build a JSON answer with active users and pagination handling """
    # Check arguments consitency 
    pageSize = request.params.get('pageSize',"8")
    current_page = request.params.get('pageNum',"1")
    TiersQuery = request.params.get('searchTerm', u"")
    # Don't answer to users that aren't logged    
    if not request.user:
        return HTTPUnauthorized('You have to be logged to hope an answer.')
    # Check consistancy of parameters        
    if pageSize.isdigit() and current_page.isdigit():
        current_page = int(current_page)
        pageSize = int(pageSize)
    else:
        return HTTPBadRequest('pageSize and pageNum accept only digits.')    
    # Query database
    JTiers = DBSession.query(Tiers.uid, Tiers.name)\
        .filter(Tiers.slug.contains( remove_accents(TiersQuery) ))
    page_url = paginate.PageURL_WebOb(request)
    records = paginate.Page(JTiers, current_page, url=page_url, items_per_page=pageSize)
    ListMatchTiers = map( lambda t:{"id": t.uid, "text": t.name }, records )
    return { "Results": ListMatchTiers, "Total":records.item_count,
            "logged_in":request.authenticated_userid }

@view_config(route_name='progr_json', renderer="json")
def JSON_Progamme_Request(request):
    year = int(request.matchdict.get('year', CurrentYear))
    # Initialization
    DicResult = dict()
    # Query database
    # Compute days used by all events matching the specified input year
    Days = DBSession.query( func.strftime('%d', Event.start_time).label('day') )\
            .filter(Event.for_year == year)\
            .filter(Event.event_type != None)\
            .group_by(func.strftime('%d', Event.start_time)).all()

    for Day in Days:
        Events = DBSession.query(Event)\
                    .filter(Event.for_year == year)\
                    .filter(Event.event_type != 'Stand')\
                    .filter("strftime('%d', start_time) = :dow").params(dow=Day.day)\
                    .order_by(Event.start_time)
                    
        ListEv = []
        for ev in Events:
            if ev.event_type:
                ListEv.append( {
                    "uid":"%d/%d" % ( year, ev.uid ),
                    "desc":ev.name,
                    "startDate":ev.start_time.strftime('%Y-%m-%dT%H:%M:%S'),
                    "endDate":ev.end_time.strftime('%Y-%m-%dT%H:%M:%S'),
                    "placeName":ev.Salle and (ev.Salle.name or "unk") ,
                    "status":ev.event_type
                } )
        DicResult[Day.day] = ListEv
    return { 'all':DicResult }

@view_config(route_name='timeline_json', renderer="json")
def JSON_TimeLine_Request(request):
    year = int(request.matchdict.get('year', CurrentYear))
    # Initialization
    DicResult = dict()
    # Query database
    # Compute days used by all events matching the specified input year
    Days = DBSession.query( func.strftime('%d', Event.start_time).label('day') )\
            .filter(Event.for_year == year)\
            .filter(Event.event_type != None)\
            .group_by(func.strftime('%d', Event.start_time)).all()
    ListEv = []
    for Day in Days:
        Events = DBSession.query(Event)\
                    .filter(Event.for_year == year)\
                    .filter(Event.event_type != 'Stand')\
                    .filter("strftime('%d', start_time) = :dow").params(dow=Day.day)\
                    .order_by(Event.start_time)
                    
        ListEv = []
        for ev in Events:
            if ev.event_type:
                ListEv.append( {
                    #"uid":"%d/%d" % ( year, ev.uid ),
                    "headline":ev.name,
                    "startDate":ev.start_time.strftime('%Y,%m,%d,%H,%M'),
                    "endDate":ev.end_time.strftime('%Y,%m,%d,%H,%M'),                
                    "text":ev.Salle and (ev.Salle.name or "unk"),
                    "tags":ev.Salle and (ev.Salle.name or "unk") ,
                    #"status":ev.event_type,
                    "asset": {
                        "media":"", #http://jm2l.linux-azur.org/sites/jm2l.linux-azur.org/files/videos/2012/2012_Introduction_aux_logiciels_libres__Frederic_Couchet.ogv",
                        "credit":"",
                        "caption":"" }
                } )
        break
    DicResult = {
        "lang":"fr",
        "headline":"JM2L 2015",
        "type":"default",
        "startDate":"2015,11,28,10",
        "text":"<i><span class='c1'>9ème Édition</span></i>",
        "asset":
        {
            "media":"",
            "credit":"JM2L",
            "caption":""
        }
    }
    DicResult["date"] = ListEv
    return { 'timeline':DicResult }



## =-=- Here, We handle HTTP requests - Public Part -=-=
@view_config(route_name='home', renderer="jm2l:templates/NewIndex.mako")
def index_page(request):
    page = int(request.params.get('page', 1))
    paginator = Entry.get_paginator(request, page)
    profil = User.by_id(4)
    profil_form = ProfilForm(request.POST, profil, meta={'csrf_context': request.session})
    MainTab = {'programme':'','presse':'', 'plan':'', 'participer':'', 'paginator':paginator,
        "logged_in":request.authenticated_userid }
    return MainTab

@view_config(route_name='programme', renderer="jm2l:templates/Public/Programme.mako")
def programme(request):
    year = int(request.matchdict.get('year'))
    if 2006 > year:
        return HTTPBadRequest('The first JM2L event was in 2006.')
    # Query database about selected Year.
    Events = DBSession.query(Event)\
                .filter(Event.for_year == year)\
                .order_by(Event.start_time)
    Days = DBSession.query(func.strftime('%d-%m-%Y', Event.start_time))\
                .filter(Event.for_year == year)\
                .filter(Event.event_type != None)\
                .group_by(func.strftime('%d', Event.start_time)).all()
    ListDay = []
    for day in Days:
        RefDay = datetime.datetime.strptime(day[0],'%d-%m-%Y')
        ListDay.append( ( RefDay.strftime('%A %d %b %Y'), 
                          RefDay.strftime('%d') ) )
    MainTab = {'programme':'active','presse':'', 'plan':'', 'participer':'', 'DisplayYear':year, \
        'Events':Events, 'Event':Event, 'Days':ListDay, "logged_in":request.authenticated_userid  }
    return MainTab

@view_config(route_name='presse', renderer="jm2l:templates/Public/Presse.mako")
def static_presse(request):
    year = int(request.matchdict.get('year', None))
    content = DBSession.query(JM2L_Year).filter(JM2L_Year.year_uid==year).first()
    MainTab = {'programme':'','presse':'active', 'plan':'', 'participer':'',
    "logged_in":request.authenticated_userid, 'content':content, 'DisplayYear':year}
    return MainTab

@view_config(route_name='edit_presse', renderer="jm2l:templates/Staff/EditPresse.mako")
def edit_presse(request):
    year = int(request.matchdict.get('year', None))
    content = DBSession.query(JM2L_Year).filter(JM2L_Year.year_uid==year).first()
    form = DossPresse(request.POST, content, meta={'csrf_context': request.session})
    if request.method == 'POST' and form.validate():
        form.populate_obj(content)
    MainTab = {'programme':'','presse':'active', 'plan':'', 'participer':'',
    "logged_in":request.authenticated_userid, 'form':form, 'DisplayYear':year}
    
    return MainTab

@view_config(route_name='plan', renderer="jm2l:templates/Public/Plan.mako")
def static_plan(request): 
    MainTab = {'programme':'','presse':'', 'plan':'active', 'participer':'',
        "logged_in":request.authenticated_userid }
    return MainTab

## =-=- Here, We handle HTTP requests - Staff Logged Part -=-=
@view_config(route_name='list_task', renderer='jm2l:templates/Staff/list.mako')
def list_view(request):
    DicTask = {}
    taskgroup = DBSession.query( TasksArea ).all()
    for grp in taskgroup:
        tasks = DBSession.query( Tasks )\
            .filter( Tasks.area_uid==grp.uid )\
            .order_by(Tasks.closed, Tasks.due_date).all()
        DicTask[grp] = tasks
    return {'tasks': DicTask }


@view_config(route_name='handle_task', renderer='jm2l:templates/Staff/tasks.mako')
def tasks(request):
    task_id = request.matchdict.get('task_id')
    if task_id:
        Task = Tasks.by_id(int(task_id))
        if not Task:
            raise HTTPNotFound()
        form = EditStaffTasks(request.POST, Task, meta={'csrf_context': request.session})
    else:
        Task = Tasks()
        form = StaffTasks(request.POST, Task, meta={'csrf_context': request.session})
    # Put some areas on form
    Areas = DBSession.query(TasksArea.uid, TasksArea.name)\
                .order_by('name').all()
    form.area_uid.choices = Areas
    # Put some users on form
    Users = DBSession.query(User)\
                .filter(User.Staff==1)\
                .order_by('nom').all()
    form.closed_by.choices = [(u.uid, "%s %s" % (u.nom, u.prenom)) 
        for u in Users]
    if request.method == 'POST' and form.validate():
        form.populate_obj(Task)
        Task.closed = False
        if 'uid' in form._fields.keys():
            DBSession.merge(Task)
        else:
            DBSession.add(Task)
        return HTTPFound(location=request.route_url('list_task'))
    return {'form':form }


@view_config(route_name='handle_pole', renderer='jm2l:templates/Staff/pole.mako')
def tasks_area(request):
    pole_id = request.matchdict.get('pole_id')
    if pole_id:
        Pole = TasksArea.by_id(int(pole_id))
        if not Pole:
            raise HTTPNotFound()
        form = EditStaffArea(request.POST, Pole, meta={'csrf_context': request.session})
    else:
        Pole = TasksArea()
        form = StaffArea(request.POST, Pole, meta={'csrf_context': request.session})
    if request.method == 'POST' and form.validate():
        form.populate_obj(Pole)
        if 'uid' in form._fields.keys():
            DBSession.merge(Pole)
        else:
            DBSession.add(Pole)
        return HTTPFound(location=request.route_url('list_task'))
    return {'form':form }

@view_config(route_name='action_task')
def action_task(request):
    action = request.matchdict.get('action')
    task_id = request.matchdict.get('task_id')
    raise 'test'
    Task = Tasks.by_id(int(task_id))
    if action=='close':
        Task.closed = True
    if action=='open':
        Task.closed = False
    DBSession.merge(Task)
    request.session.flash('Task was successfully closed!')
    return HTTPFound(location=request.route_url('list_task'))


## =-=- Here, We handle HTTP requests - User Logged Part -=-=
@view_config(route_name='exchange', renderer="jm2l:templates/Logistique/Logistique.mako")
def exchange(request):
    modtype = request.matchdict.get('modtype', None)
    action  = request.matchdict.get('action', None)
    uid = int(request.matchdict.get('id', -1))
    Exch = Exchange.by_id(uid)
    if not Exch:
        MainTab = {
            'Exchanges':Exchange,
            'Type':modtype[-1:],
            'reload':True,
            'logged_in':request.authenticated_userid
        }
        return MainTab        
        
    if action in ['delete', 'accept', 'refuse', 'deal']:
        if action=='delete':   # delete exchange
            DBSession.delete(Exch)
        elif action=='accept': # accept exchange
            Exch.exch_done=True
            DBSession.merge(Exch)
        elif action=='refuse': # refuse exchange
            Exch.exch_done=False
            if Exch.exch_state=="Ask":
                Exch.provider_id = None
            elif Exch.exch_state=="Proposal":
                Exch.asker_id = None
            DBSession.merge(Exch)
        elif action=='deal':
            # ask to deal the exchange
            if Exch.exch_state=="Ask":
                Exch.provider_id = request.user.uid
            elif Exch.exch_state=="Proposal":            
                Exch.asker_id = request.user.uid
        # Return javascript to parent page
        response = render_to_response('jm2l:templates/modals_js.mako', 
            {'modtype':modtype, 'action':action},
            request=request)
        response.content_type = 'text/javascript'
        return response      
    else:
        MainTab = {
            'Exchanges':Exchange,
            'Type':modtype[-1:],
            'reload':True,
            'logged_in':request.authenticated_userid
        }
        return MainTab

@view_config(route_name='jm2l', renderer="jm2l:templates/jm2l.mako")
def jm2l_page(request):
    if request.user is None:    
        # Don't answer to users that aren't logged
        return HTTPUnauthorized('You have to be logged to hope an answer.')
    page = int(request.params.get('page', 1))
    UserNum = request.params.get('user')

    if UserNum:
        profil = User.by_id(int(UserNum))
        if not profil:
            raise HTTPNotFound()
    else:
        profil = request.user
    # Build Form
    profil_form = ProfilForm(request.POST, profil, meta={'csrf_context': request.session})
    if request.method == 'POST' and profil_form.validate():
        ToDelete = list()
        # First, we remove entries no more present
        for obj in profil_form.tiersship.object_data:
            MatchEntry = filter( lambda x: x.object_data and x.object_data._sa_instance_state == obj._sa_instance_state, 
                    profil_form.tiersship.entries )
            if not MatchEntry:
                ToDelete.append(obj)
        # Then, it's time to consider new entries
        for entry in profil_form.tiersship.entries:
            if entry.object_data is None:
                TmpUser = User_Tiers()
                entry.object_data = TmpUser
                profil.tiersship.append(TmpUser)
                profil_form.tiersship.object_data = profil.tiersship
        profil_form.populate_obj(profil)
        # We should remove it as it's not in original data
        for obj in ToDelete:
            #profil.tiersship.remove(obj)
            DBSession.delete(obj)
        profil.last_change = datetime.datetime.utcnow()
        profil.slug = slugify(remove_accents('%s %s' % (profil.prenom, profil.nom)).lower().strip())
        DBSession.merge(profil)
    MainTab = {'participer':'active',
        'Places':Place.get_list(False),
        'DBTiers':Tiers,
        'DBTiersOpt':TiersOpt,
        'Exchanges':Exchange,
        'profil_form':profil_form,     
        'uprofil':profil,
        'logged_in':request.authenticated_userid        
    }
    return MainTab

@view_config(route_name='modal', renderer="jm2l:templates/modals.mako")
def Modal(request):
    year = int(request.matchdict.get('year', None))
    modtype = request.matchdict.get('modtype', None)
    uid = int(request.matchdict.get('id', -1))
    session = request.session
    if modtype=='Password':
        form = UserPasswordForm(request.POST, request.user, meta={'csrf_context': request.session})
        if request.method == 'POST' and form.validate():
            response = render_to_response('jm2l:templates/modals_js.mako', 
                {'modtype':modtype},
                request=request)
            response.content_type = 'text/javascript'
            return response
    if modtype=='UserPicture':
        form = None
        if request.method == 'POST':
            response = render_to_response('jm2l:templates/modals_js.mako', 
                {'modtype':modtype},
                request=request)
            response.content_type = 'text/javascript'
            return response
    if modtype=='Place':
        if uid>0:
            place = Place.by_id(uid)
            if not place:
                raise HTTPNotFound()
            form = PlaceUpdateForm(request.POST, place, meta={'csrf_context': request.session})
        else:
            place = Place()
            form = PlaceCreateForm(request.POST, meta={'csrf_context': request.session})
        if request.method == 'POST' and form.validate():
            form.populate_obj(place)
            place.created_by=request.user.uid
            if uid>0:
                DBSession.merge(place)                
            else:
                DBSession.add(place)
            response = render_to_response('jm2l:templates/modals_js.mako', 
                {'modtype':modtype},
                request=request)
            response.content_type = 'text/javascript'
            return response
    if modtype in ['AskC', 'AskH', 'AskM', 'PropC', 'PropH', 'PropM']:
        if uid>0:
            Exch = Exchange.by_id(uid)
            if not Exch:
                raise HTTPNotFound()
            if modtype in ['AskC','PropC']:
                form = globals()["Update%sForm" % modtype](request.POST, Exch,
                    start_place = Exch.Itin.start_place,
                    arrival_place = Exch.Itin.arrival_place,
                    Hour_start = Exch.start_time.strftime("%H:%M"),
                    Day_start = Exch.start_time.strftime("%w"),
                    exch_id = uid, meta={'csrf_context': request.session}
                )
            elif modtype in ['AskM','PropM']:
                form = globals()["Update%sForm" % modtype](request.POST, Exch,
                    description = Exch.description,
                    exch_categ = Exch.exch_categ,
                    Hour_start = Exch.start_time.strftime("%H:%M"),
                    Day_start = Exch.start_time.strftime("%w"),
                    Hour_end = Exch.end_time.strftime("%H:%M"),
                    Day_end = Exch.end_time.strftime("%w"),                    
                    exch_id = uid, meta={'csrf_context': request.session}
                )
            elif modtype in ['AskH','PropH']:
                form = globals()["Update%sForm" % modtype](request.POST, Exch,
                    description = Exch.description,
                    exch_categ = Exch.exch_categ,
                    Day_start = Exch.start_time.strftime("%w"),              
                    exch_id = uid, meta={'csrf_context': request.session}
                )
            # Itinerary, first get itinerary
            if 0:
                form.itin.form.start_place.data = Exch.Itin.start_place
                form.itin.form.arrival_place.data = Exch.Itin.arrival_place
                form.dateform.form.Hour.data = Exch.start_time.strftime("%H:%M")
                form.dateform.form.Day.data = Exch.start_time.strftime("%w")
                form.exch_id.data = uid
        else:
            Exch = Exchange()
            form = globals()["%sForm" % modtype](request.POST, meta={'csrf_context': request.session})
        if modtype in ['AskC', 'PropC']:
            # Put some place on form
            Places = DBSession.query(Place.place_id, Place.display_name)\
                        .order_by('name').all()
            form.start_place.choices = Places
            form.arrival_place.choices = Places
        if modtype in ['PropH']:
            form.exch_categ.choices = DBSession.query( Exchange_Cat.cat_id, Exchange_Cat.exch_subtype)\
                                .filter( Exchange_Cat.exch_type=='H' ).all()
            form.place_id.choices = DBSession.query( Place.place_id, Place.display_name)\
                                .filter( Place.created_by==request.user.uid ).all()
        if modtype in ['AskM', 'PropM']:
            form.exch_categ.choices = DBSession.query( Exchange_Cat.cat_id, Exchange_Cat.exch_subtype)\
                                .filter( Exchange_Cat.exch_type=='M' ).all()

        if request.method == 'POST' and form.validate():
            # Form has been validated, it's time to create our Exchange
            Exch.for_year = year            
            Exch.exch_state = {'Ask':'Ask', 'Prop':'Proposal'}[modtype[:-1]]     
            Exch.exch_type = modtype[-1:]  
            if modtype in ['AskC', 'PropC']:
                # Itinerary, first Let's see if itinerary exist
                Itinerary = DBSession.query(Itineraire)\
                    .filter(Itineraire.start_place==form.start_place.data) \
                    .filter(Itineraire.arrival_place==form.arrival_place.data) \
                    .filter(Itineraire.tr_voiture==True) \
                    .first()
                if not Itinerary: # Not exist yet !
                    Itinerary = Itineraire(start_place=form.start_place.data, \
                        arrival_place=form.arrival_place.data, \
                        tr_voiture=True, \
                        created_by=1
                    )
                DBSession.add(Itinerary)
                DBSession.flush()                                 
                Exch.itin_id = Itinerary.itin_id
            # Start Time
            StartEvent = DBSession.query(JM2L_Year.start_time).filter(JM2L_Year.year_uid==year).first()
            Week = StartEvent[0].strftime("%W")
            # populate
            form.populate_obj(Exch)
            if modtype in ['AskC', 'PropC']:            
                Exch.itin_id = Itinerary.itin_id
            if form._fields.has_key("Hour_start"):
                TargetTime = datetime.datetime.strptime('%d %d %d %s' % (year, int(Week), \
                    int(form.Day_start.data), form.Hour_start.data), "%Y %W %w %H:%M")
                Exch.start_time = TargetTime
            elif form._fields.has_key("Day_start"):
                TargetTime = datetime.datetime.strptime('%d %d %d' % (year, int(Week), \
                    int(form.Day_start.data)), "%Y %W %w")
                Exch.start_time = TargetTime

            if form._fields.has_key("Hour_end"):
                TargetTime = datetime.datetime.strptime('%d %d %d %s' % (year, int(Week), \
                    int(form.Day_end.data), form.Hour_end.data), "%Y %W %w %H:%M")
                Exch.end_time = TargetTime
            elif form._fields.has_key("Day_end"):
                TargetTime = datetime.datetime.strptime('%d %d %d' % (year, int(Week), \
                    int(form.Day_end.data)), "%Y %W %w")
                Exch.end_time = TargetTime

            Exch.last_change = datetime.datetime.utcnow()
            if Exch.exch_state=='Ask':
                Exch.asker_id = request.user.uid          
            elif Exch.exch_state=='Proposal':
                Exch.provider_id = request.user.uid
            #print vars(form.itin.form)
            if uid>0:
                DBSession.merge(Exch)          
            else:
                DBSession.add(Exch)
            response = render_to_response('jm2l:templates/modals_js.mako', 
                {'modtype':modtype},
                request=request)
            response.content_type = 'text/javascript'
            return response            
        # Fallback to HTML Display with errors
        return {'modtype':modtype, 'form':form, 'update':uid>0,
            'logged_in':request.authenticated_userid }
    if modtype in ['ShowC', 'ShowH', 'ShowM']:
        if uid>0:
            Exch = Exchange.by_id(uid)
            if not Exch:
                raise HTTPNotFound()
        else:
            raise HTTPNotFound()            
        # Show Details around the Current Exchange
        return {'modtype':modtype, 'Exch':Exch, 'logged_in':request.authenticated_userid }
    
    MainTab = {'modtype':modtype, 'form':form, 'update':uid>0, 'uid':uid,
            'DisplayYear':year, 'session':session,
            'logged_in':request.authenticated_userid }
    return MainTab

@view_config(route_name='participer', renderer="jm2l:templates/Participer.mako")
def participer(request):
    session = request.session
    session['year'] = 2015
    TmpUsr = User()
    form = UserRegisterForm(request.POST, TmpUsr, meta={'csrf_context': request.session})
    MyLink=None
    if request.method == 'POST' and form.validate():
        form.populate_obj(TmpUsr)
        TmpUsr.nom = TmpUsr.nom.capitalize()
        TmpUsr.prenom = TmpUsr.prenom.capitalize()
        TmpUsr.slug = slugify(remove_accents('%s %s' % (form.prenom.data, form.nom.data)).lower().strip())
        TmpUsr.password = TmpUsr.my_hash
        if len(TmpUsr.slug):
            CheckExist = DBSession.query(User)\
                .filter(User.slug==TmpUsr.slug)\
                .first()
        else:
            CheckExist=None
        if CheckExist:
            MyLink = CheckExist.my_hash
        else:
            DBSession.add(TmpUsr)
            DBSession.flush()
            MyLink = TmpUsr.my_hash
    MainTab = {'programme':'','presse':'', 'plan':'', 
            'participer':'active', 'form':form, "link": MyLink,
            'logged_in':request.authenticated_userid }
    return MainTab   

@view_config(route_name='year')
def change_year(request):
    year = int(request.matchdict.get('year', -1))
    session = request.session
    if year>-1:
        session['year'] = year
        return HTTPFound(location='/%s/le-programme' % year)
    return HTTPFound(location=request.route_url('home'))

@view_config(route_name='pict_user', renderer="jm2l:templates/Profil/pict_user.mako")
def pict_user(request):
    return {"uprofil":request.user}

@view_config(route_name='event', renderer="jm2l:templates/view_event.mako")
def show_event(request):
    year = int(request.matchdict.get('year', -1))    
    event_id = request.matchdict.get('event_id')
    if event_id.isdigit():
        TheEvent = Event.by_id(event_id)
        if TheEvent is None:
            raise HTTPNotFound()
    else:
        TheEvent = Event.by_slug(event_id, year)
        if TheEvent is None:
            raise HTTPNotFound()
    MainTab = {'programme':'','presse':'', 'plan':'', 'participer':'', 
        'event':TheEvent, 'logged_in':request.authenticated_userid }
    return MainTab

@view_config(route_name='link_event')
def link_event(request):
    """ Create user if not exist, add it to current event """
    year = int(request.matchdict.get('year', -1))
    form = AddIntervenant(request.POST, meta={'csrf_context': request.session})
    intervention = request.matchdict.get('intervention', None)    
    TargetEvent = Event.by_id(form.event_uid.data)
    Exist = DBSession.query(User)\
        .filter(User.nom==form.nom.data)\
        .filter(User.prenom==form.prenom.data)\
        .first()
    if Exist:
        TargetUser = Exist
    else:
        # Add it to user base
        TargetUser = User(nom=form.nom.data, 
            prenom=form.prenom.data, password=form.nom.data)
        DBSession.add(TargetUser)
        DBSession.flush()

    uev = User_Event(year_uid=year, role="Animateur", user_uid=TargetUser.uid)
    TargetEvent.interventions.append( uev )
            
    return HTTPFound(location=request.route_url('edit_event', sep='/',
        year=str(year), intervention=intervention, uid=str(TargetEvent.uid)))

@view_config(route_name='edit_event', renderer="jm2l:templates/edit_event.mako")
def edit_event(request):
    year = int(request.matchdict.get('year', -1))
    event_id = request.matchdict.get('event_id')    
    intervention = request.matchdict.get('intervention', None)
    IntervLabel = intervention.replace('_',' ').lower()
    if intervention=='Conference': 
        IntervLabel = u'conférence'
    # Check intervention
    if not intervention in ['Stand', 'Table ronde', 'Atelier', 'Conference']:
        raise HTTPNotFound(u"Ce type d'évenement n'est pas reconnu")
    TheYear = DBSession.query(JM2L_Year)\
            .filter(JM2L_Year.year_uid==year)\
            .all()
    # Check year avaibility
    if not TheYear:
        raise HTTPNotFound(u"Cette année n'est pas pris en charge")
    # Generate Timeslots for current year
    TimeSlots = list(enumerate( [ x.strftime('%a %d %b %H:%M') for x in
            TheYear[0].AvailableTimeSlots ] ))

    if event_id:
        # We try to update an existing record
        if event_id.isdigit():
            TheEvent = Event.by_id(event_id)
            if TheEvent is None:
                raise HTTPNotFound(u"Cette réference n'existe pas")
        else:
            TheEvent = Event.by_slug(event_id, year)
            if TheEvent is None:
                raise HTTPNotFound(u"Cette réference n'existe pas")

        if not (request.user.uid==1 or request.user in TheEvent.intervenants):
            return HTTPForbidden(u"Vous n'êtes pas identifié comme étant un participant à cette intervention.")
        # Compute some field value from selected event
        if TheEvent.start_time in TheYear[0].AvailableTimeSlots:
            start_sel = TheYear[0].AvailableTimeSlots.index(TheEvent.start_time)
        else:
            start_sel = len(TimeSlots)            
            TimeSlots.append( (len(TimeSlots), TheEvent.start_time.strftime('%a %d %b %H:%M')))            
        duration = (TheEvent.end_time - TheEvent.start_time).total_seconds()/60
        end = TheEvent.start_time + datetime.timedelta(minutes=duration)
        # prepare the form with update 
        form = ConfUpdateForm(request.POST, TheEvent, start_sel=start_sel, duration=duration, end_time=end,
            meta={'csrf_context': request.session} )
        # Customize labels
        form.name.label.text += IntervLabel
        form.description.label.text += IntervLabel
        # Each event can get severals members
        formAdd = AddIntervenant(event_uid=TheEvent.uid)
    else:
        TheEvent = Event()
        # prepare the form for creation
        form = ConfCreateForm(request.POST,
                event_type=intervention,
                for_year=str(year), meta={'csrf_context': request.session}
                )
        # Customize labels
        form.name.label.text += IntervLabel
        form.description.label.text += IntervLabel
        duration=60
        # No intervenant
        formAdd = None

    if intervention=="Conference":
        form.duration.choices =[ 
              (15,u'Lighting talk ( 5 min)'),
              (30,u'Conférence    (20 min)'),
              (60,u'Conférence    (50 min)'),
              (90,u'Conférence    (75 min)'),
            ]
        if not duration in [15, 30, 60, 90]:
            form.duration.choices.append( (duration,u'Conférence    (%d min)' % duration) )
        if not form._fields.has_key("uid"):            
            form.duration.data=60
    elif intervention=="Stand":
        form.duration.choices =[ 
            (8*60, u'Toute la journée'),        
            (4*60, u'une demi-journée')
        ]
    elif intervention=="Atelier":
        form.duration.choices = map( lambda d:(d, u'Atelier (%dh%.2d)' % (d/60, d%60) ), \
                [60, 90, 120, 150, 180, 210, 240] )
        if not duration in map(lambda (d,y): d, form.duration.choices):
            form.duration.choices.append( (duration,u'Atelier (%dh%.2d)' % (duration/60, duration%60) ) )
    elif intervention=="Table_Ronde":
        form.duration.choices = map( lambda d:(d, u'Table ronde (%dh%.2d)' % (d/60, d%60) ), \
                [60, 90, 120, 150] )
        if not duration in map(lambda (d,y): d, form.duration.choices):
            form.duration.choices.append( (duration,u'Table ronde (%dh%.2d)' % (duration/60, duration%60) ) )                    
    else:
        return HTTPForbidden(u"Pas encore disponible.")
        
    SalleDispo = DBSession.query(Salles)\
            .filter(Salles.year_uid==year)\
            .order_by('name')
            
    form.salle_uid.choices = [(s.salle_id, s.name) for s in SalleDispo]
    form.start_sel.choices = TimeSlots
    
    if request.method == 'POST' and form.validate():
        form.populate_obj(TheEvent)
        TheEvent.start_time = TheYear[0].AvailableTimeSlots[form.start_sel.data]
        TheEvent.end_time = TheEvent.start_time + datetime.timedelta(minutes=form.duration.data)
        # Ok, time to put in database
        if not form._fields.has_key("uid"):
            TheEvent.slug = slugify(TheEvent.name)
            DBSession.add(TheEvent)
            # Append creator by default
            if request.user.uid!=1:
                uev = User_Event(year_uid=TheYear.year_uid, role="Animateur")
                uev.user_uid = request.user.uid
                TheEvent.interventions.append( uev )
            DBSession.flush()
            return HTTPFound(location=request.route_url('edit_event', sep='/',
                year=str(year), intervention=intervention, event_id=str(TheEvent.slug)))
        else:
            DBSession.merge(TheEvent)
            
    MainTab = {'programme':'','presse':'', 'plan':'', 'participer':'',
            'event':TheEvent, 'form':form, 'formAdd':formAdd,
            'logged_in':request.authenticated_userid }
            
    return MainTab

@view_config(route_name='entities', renderer="jm2l:templates/list_tiers.mako")
def list_tiers(request):
    Entities = dict()
    EntityType = DBSession.query(TiersOpt.entity_type)\
            .group_by(TiersOpt.entity_type).all()
    for EType in EntityType:
        Entities[EType.entity_type] = DBSession.query(Tiers).join(TiersOpt)\
            .filter(TiersOpt.entity_type==EType.entity_type)\
            .order_by(TiersOpt.entity_subtype, Tiers.name)
    MainTab = {'programme':'','presse':'', 'plan':'', 'participer':'', 
        'entities':Entities, 'logged_in':request.authenticated_userid }
    return MainTab

@view_config(route_name='show_entity', renderer="jm2l:templates/view_tiers.mako")
def show_tiers(request):
    tiers_type = request.matchdict.get('tiers_type')
    entity_id = request.matchdict.get('entity_id')
    if entity_id.isdigit():
        TheTiers = Tiers.by_id(entity_id)
        if TheTiers is None:
            raise HTTPNotFound()
    else:
        TheTiers = Tiers.by_slug(entity_id)
        if TheTiers is None:
            raise HTTPNotFound()        
    MainTab = {'programme':'','presse':'', 'plan':'', 'participer':'', 
        'entity':TheTiers, 'logged_in':request.authenticated_userid }
    return MainTab

@view_config(route_name='add_entity', renderer="jm2l:templates/edit_tiers.mako")
@view_config(route_name='edit_entity', renderer="jm2l:templates/edit_tiers.mako",
    permission='edit')
def edit_tiers(request):
    entity_id = request.matchdict.get('entity_id', None)
    TargetList = list()
    entity_types = DBSession.query(TiersOpt.entity_type).group_by(TiersOpt.entity_type).all()
    for entity_type in entity_types:
        entity_subtypes = DBSession.query(TiersOpt)\
            .filter(TiersOpt.entity_type==entity_type.entity_type)\
            .group_by(TiersOpt.entity_subtype).all()
        ListType = [(i.uid, i.entity_subtype) for i in entity_subtypes]
        TargetList.append( (entity_type.entity_type, ListType) )
    if entity_id:
        if entity_id.isdigit():
            TheTiers = Tiers.by_id(entity_id)
            if TheTiers is None:
                raise HTTPNotFound()
        else:
            TheTiers = Tiers.by_slug(entity_id)
            if TheTiers is None:
                raise HTTPNotFound()
        form = UpdateTiersForm(request.POST,  TheTiers, meta={'csrf_context': request.session})
        UserOptions = DBSession.query(TiersOpt)\
            .filter(TiersOpt.entity_type==TheTiers.tiers_type)\
            .all()
        form.tiers_type.choices = TargetList
    else:
        TheTiers = Tiers()
        # prepare the form for creation
        form = TiersForm(request.POST, TheTiers, meta={'csrf_context': request.session})
        form.tiers_type.choices = TargetList
        UserOptions = list()

    #test_form = TiersForm(request.POST, TheTiers, meta={'csrf_context': request.session})
    if request.method == 'POST' and form.validate():
        ToDelete = list()
        # First, we remove entries no more present
        for obj in form.membership.object_data:
            MatchEntry = filter( lambda x: x.object_data and x.object_data._sa_instance_state == obj._sa_instance_state, 
                    form.membership.entries )
            if not MatchEntry:
                ToDelete.append(obj)
        # We should remove it as it's not in original data
        for obj in ToDelete:
            TheTiers.membership.remove(obj)
            DBSession.delete(obj)
        # Then, it's time to consider new entries
        for entry in form.membership.entries:
            if entry.object_data is None:
                TmpUser = User_Tiers()
                entry.object_data = TmpUser
                TheTiers.membership.append(TmpUser)
                form.membership.object_data = TheTiers.membership
        form.populate_obj(TheTiers)
        # Handle Remove of accents
        TheTiers.slug = slugify(form.name.data)
        if not form._fields.has_key('uid'):
            TheTiers.creator_id = request.user.uid
            DBSession.add(TheTiers)
            DBSession.flush()
            return HTTPFound(location=request.route_url('edit_entity', sep='/', 
                entity_id=str(TheTiers.slug), tiers_type=TheTiers.get_entity_type.entity_type))
        DBSession.merge(TheTiers)
        return HTTPFound(location=request.route_url('entities'))        
    MainTab = {'programme':'','presse':'', 'plan':'', 'participer':'', 
    'form':form, 'DBUser':User, 'UserOptions':UserOptions, 
    'logged_in':request.authenticated_userid }
    return MainTab 

@view_config(route_name='edit_entity_cat', renderer="jm2l:templates/edit_tiers_categ.mako")
def edit_tiers_category(request):
    DicResult = dict()
    ListChanges = list()
    if request.method == 'POST':
        # Reformat data
        RegExist = re.compile('collection\[(?P<slug>[\w-]+)\]\[(?P<num>\d+)\]\[(?P<id>\d+)\]')
        RegTitle = re.compile('collection\[(?P<slug>[\w-]+)\]\[title]')
        RegNew   = re.compile('collection\[(?P<slug>[\w-]+)\]\[(?P<num>\d+)\]\[id\]')        
        for key, value in request.POST.iteritems():
            regN= RegNew.match(key)
            regT= RegTitle.match(key)
            reg = RegExist.match(key)                                
            if reg:
                if not DicResult.has_key(reg.group('slug')):
                    DicResult[reg.group('slug')] = dict()
                
                if DicResult[reg.group('slug')].has_key('items'):
                    DicResult[reg.group('slug')]['items'].append( ( int(reg.group('id')), value ) )
                else:
                    DicResult[reg.group('slug')]['items'] = [ ( int(reg.group('id')), value ) ]
            elif regN:
                if not DicResult.has_key(regN.group('slug')):
                    DicResult[regN.group('slug')] = dict()
                    
                if DicResult[regN.group('slug')].has_key('items'):
                    DicResult[regN.group('slug')]['items'].append( ( 'id', value ) )
                else:
                    DicResult[regN.group('slug')]['items'] = [ ( 'id', value ) ]
                ListChanges.append(('add', 0, DicResult[regN.group('slug')]['title'], value))
            elif regT:
                if not DicResult.has_key(regT.group('slug')):
                    DicResult[regT.group('slug')] = dict()
                DicResult[regT.group('slug')]['title'] = value
            else:
                raise

        for opt in DBSession.query(TiersOpt).all():
            if DicResult.has_key(opt.slug_entity_type):
                found = filter( lambda (x,y): opt.uid==x, 
                    DicResult[opt.slug_entity_type].get('items', []))
                if not found:
                    ListChanges.append(('remove', opt.uid, opt.entity_type, opt.entity_subtype))
                else:
                    for tst in found:
                        # Check changes on Cat Name
                        if DicResult[opt.slug_entity_type]['title']!=opt.entity_type or \
                            tst[1]!=opt.entity_subtype:
                            ListChanges.append(('changed', opt.uid, 
                                DicResult[opt.slug_entity_type]['title'],
                                tst[1]))
            else:
                ListChanges.append(('remove', opt.uid, opt.entity_type, opt.entity_subtype))
                
        # Do The change
        for action, uid, entity, subentity in ListChanges:
            if action=="changed":
                opt = TiersOpt.by_id(uid)
                opt.entity_type = entity
                opt.entity_subtype = subentity
            elif action=="remove":
                opt = TiersOpt.by_id(uid)
                DBSession.delete(opt)
            elif action=="add":
                opt = TiersOpt()
                opt.entity_type = entity
                opt.entity_subtype = subentity
                DBSession.add(opt)
                
    MainTab = {'programme':'','presse':'', 'plan':'', 'participer':'', 
        'logged_in':request.authenticated_userid, 'TiersOpt':TiersOpt }
    return MainTab    

@view_config(route_name='show_user', renderer="jm2l:templates/view_user.mako")
def show_user(request):
    user_slug = request.matchdict.get('user_slug', None)
    # Query database
    DispUser = User.by_slug(user_slug)
    MainTab = {'programme':'','presse':'', 'plan':'', 'participer':'', 
        'DispUser':DispUser, 'logged_in':request.authenticated_userid }    
    return MainTab


#@view_config(route_name='link_user_entity')
def link_user_entity(request):
    uid = int(request.matchdict.get('uid', -1))
    year = int(request.matchdict.get('year', -1))
    user_id = int(request.matchdict.get('uid', -1))
    TheTiers = Tiers.by_id(uid)
    if TheTiers is None:
        raise HTTPNotFound()
    return HTTPFound(location=request.route_url('edit_entity', uid=uid) )
            
#@view_config(route_name='link_role_entity')
def link_role_entity(request):
    uid = int(request.matchdict.get('uid', -1))
    year = int(request.matchdict.get('year', -1))   
    role_id = int(request.matchdict.get('role_id', -1))
    TheTiers = Tiers.by_id(uid)
    if TheTiers is None:
        raise HTTPNotFound()
    return HTTPFound(location=request.route_url('edit_entity', uid=uid) )    

@notfound_view_config()
def notfound(reason, request):
    request.response.status = 404
    return render_to_response('jm2l:templates/Errors/404.mak', { "reason":reason },
        request=request)


@forbidden_view_config()
def forbidden(reason, request):
    return Response('forbidden')
    request.response.status = 404
    return render_to_response('jm2l:templates/Errors/404.mak', { "reason":reason },
        request=request)