|
1
|
|
|
from django.shortcuts import render_to_response |
|
2
|
|
|
from formtools.wizard.views import SessionWizardView |
|
3
|
|
|
from django.core.urlresolvers import reverse |
|
4
|
|
|
from django.http import HttpResponseRedirect |
|
5
|
|
|
|
|
6
|
|
|
from django_th.models import TriggerService, UserService |
|
7
|
|
|
from django_th.forms.wizard import ConsumerForm |
|
8
|
|
|
from django_th.tools import get_service, class_for_name |
|
9
|
|
|
from django_th.services import default_provider |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
""" |
|
13
|
|
|
Part III : Service Wizard |
|
14
|
|
|
""" |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
class UserServiceWizard(SessionWizardView): |
|
18
|
|
|
|
|
19
|
|
|
def get_template_names(self): |
|
20
|
|
|
# name to find template : |
|
21
|
|
|
# form_name/wz-step-form.html |
|
22
|
|
|
# the form_name should be formed by the name of the service + form |
|
23
|
|
|
# for example : |
|
24
|
|
|
# rss/wz-1-form.html |
|
25
|
|
|
# rss/wz-3-form.html |
|
26
|
|
|
# evernote/wz-1-form.html |
|
27
|
|
|
# evernote/wz-3-form.html |
|
28
|
|
|
if self.steps.current in ('0', '2', '4'): |
|
29
|
|
|
folder = 'services_wizard' |
|
30
|
|
|
else: |
|
31
|
|
|
data = self.get_cleaned_data_for_step(self.get_prev_step( |
|
32
|
|
|
self.steps.current)) |
|
33
|
|
|
if data.get('provider'): |
|
34
|
|
|
folder = str(data.get('provider')).split('Service')[1] |
|
35
|
|
|
elif data.get('consumer'): |
|
36
|
|
|
folder = str(data.get('consumer')).split('Service')[1] |
|
37
|
|
|
|
|
38
|
|
|
return '%s/wz-%s-form.html' % (folder.lower(), self.steps.current) |
|
39
|
|
|
|
|
40
|
|
|
def get_form_initial(self, step): |
|
41
|
|
|
""" |
|
42
|
|
|
get the services provider/consumer of the current user |
|
43
|
|
|
:param step: current set |
|
44
|
|
|
:return: dict with initial data |
|
45
|
|
|
""" |
|
46
|
|
|
data = {'user': self.request.user} |
|
47
|
|
|
return self.initial_dict.get(step, data) |
|
48
|
|
|
|
|
49
|
|
|
def get_form(self, step=None, data=None, files=None): |
|
50
|
|
|
""" |
|
51
|
|
|
change the form instance dynamically from the data we entered |
|
52
|
|
|
at the previous step |
|
53
|
|
|
""" |
|
54
|
|
|
if step is None: |
|
55
|
|
|
step = self.steps.current |
|
56
|
|
|
|
|
57
|
|
|
if step == '1': |
|
58
|
|
|
|
|
59
|
|
|
prev_data = self.get_cleaned_data_for_step('0') |
|
60
|
|
|
service_name = str(prev_data.get('provider')).split('Service')[1] |
|
61
|
|
|
class_name = 'th_' + service_name.lower() + '.forms' |
|
62
|
|
|
form_name = service_name + 'ProviderForm' |
|
63
|
|
|
form_class = class_for_name(class_name, form_name) |
|
64
|
|
|
form = form_class(data) |
|
65
|
|
|
|
|
66
|
|
|
elif step == '2': |
|
67
|
|
|
step0_data = self.get_cleaned_data_for_step('0') |
|
68
|
|
|
form = ConsumerForm( |
|
69
|
|
|
data, initial={'provider': step0_data.get('provider'), |
|
70
|
|
|
'user': self.request.user}) |
|
71
|
|
|
|
|
72
|
|
|
elif step == '3': |
|
73
|
|
|
|
|
74
|
|
|
prev_data = self.get_cleaned_data_for_step('2') |
|
75
|
|
|
service_name = str(prev_data.get('consumer')).split('Service')[1] |
|
76
|
|
|
class_name = 'th_' + service_name.lower() + '.forms' |
|
77
|
|
|
form_name = service_name + 'ConsumerForm' |
|
78
|
|
|
form_class = class_for_name(class_name, form_name) |
|
79
|
|
|
form = form_class(data) |
|
80
|
|
|
|
|
81
|
|
|
else: |
|
82
|
|
|
form = super(UserServiceWizard, self).get_form(step, data, files) |
|
83
|
|
|
|
|
84
|
|
|
return form |
|
85
|
|
|
|
|
86
|
|
|
def done(self, form_list, **kwargs): |
|
87
|
|
|
""" |
|
88
|
|
|
Save info to the DB |
|
89
|
|
|
The process is : |
|
90
|
|
|
1) get the infos for the Trigger from step 0, 2, 4 |
|
91
|
|
|
2) save it to TriggerService |
|
92
|
|
|
3) get the infos from the "Provider" and "Consumer" services |
|
93
|
|
|
at step 1 and 3 |
|
94
|
|
|
4) save all of them |
|
95
|
|
|
""" |
|
96
|
|
|
# get the datas from the form for TriggerService |
|
97
|
|
|
i = 0 |
|
98
|
|
|
for form in form_list: |
|
99
|
|
|
# cleaning |
|
100
|
|
|
data = form.cleaned_data |
|
101
|
|
|
# get the service we selected at step 0 : provider |
|
102
|
|
|
if i == 0: |
|
103
|
|
|
trigger_provider = UserService.objects.get( |
|
104
|
|
|
name=data.get('provider'), |
|
105
|
|
|
user=self.request.user.id) |
|
106
|
|
|
model_provider = get_service(data.get('provider'), 'models') |
|
107
|
|
|
# get the service we selected at step 2 : consumer |
|
108
|
|
|
elif i == 2: |
|
109
|
|
|
trigger_consumer = UserService.objects.get( |
|
110
|
|
|
name=data.get('consumer'), |
|
111
|
|
|
user=self.request.user.id) |
|
112
|
|
|
model_consumer = get_service(data.get('consumer'), 'models') |
|
113
|
|
|
# get the description we gave for the trigger |
|
114
|
|
|
elif i == 4: |
|
115
|
|
|
trigger_description = data.get('description') |
|
116
|
|
|
i += 1 |
|
117
|
|
|
|
|
118
|
|
|
# save the trigger |
|
119
|
|
|
trigger = TriggerService( |
|
120
|
|
|
provider=trigger_provider, consumer=trigger_consumer, |
|
121
|
|
|
user=self.request.user, status=True, |
|
122
|
|
|
description=trigger_description) |
|
123
|
|
|
trigger.save() |
|
124
|
|
|
|
|
125
|
|
|
model_fields = {} |
|
126
|
|
|
# get the datas from the form for Service related |
|
127
|
|
|
# save the related models to provider and consumer |
|
128
|
|
|
i = 0 |
|
129
|
|
|
for form in form_list: |
|
130
|
|
|
model_fields = {} |
|
131
|
|
|
data = form.cleaned_data |
|
132
|
|
|
# get the data for the provider service |
|
133
|
|
|
if i == 1: |
|
134
|
|
|
for field in data: |
|
135
|
|
|
model_fields.update({field: data[field]}) |
|
136
|
|
|
model_fields.update({'trigger_id': trigger.id, 'status': True}) |
|
137
|
|
|
model_provider.objects.create(**model_fields) |
|
138
|
|
|
# get the data for the consumer service |
|
139
|
|
|
elif i == 3: |
|
140
|
|
|
for field in data: |
|
141
|
|
|
model_fields.update({field: data[field]}) |
|
142
|
|
|
model_fields.update({'trigger_id': trigger.id, 'status': True}) |
|
143
|
|
|
model_consumer.objects.create(**model_fields) |
|
144
|
|
|
i += 1 |
|
145
|
|
|
|
|
146
|
|
|
return HttpResponseRedirect(reverse('base')) |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
def finalcallback(request, **kwargs): |
|
150
|
|
|
""" |
|
151
|
|
|
let's do the callback of the related service after |
|
152
|
|
|
the auth request from UserServiceCreateView |
|
153
|
|
|
""" |
|
154
|
|
|
default_provider.load_services() |
|
155
|
|
|
service_name = kwargs.get('service_name') |
|
156
|
|
|
service_object = default_provider.get_service(service_name) |
|
157
|
|
|
lets_callback = getattr(service_object, 'callback') |
|
158
|
|
|
# call the auth func from this class |
|
159
|
|
|
# and redirect to the external service page |
|
160
|
|
|
# to auth the application django-th to access to the user |
|
161
|
|
|
# account details |
|
162
|
|
|
return render_to_response(lets_callback(request)) |
|
163
|
|
|
|