0001-handle-user-association-the-right-way-9527.patch
mandayejs/mandaye/forms.py | ||
---|---|---|
13 | 13 |
# |
14 | 14 |
# You should have received a copy of the GNU Affero General Public License |
15 | 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 |
from __future__ import absolute_import |
|
16 | 17 | |
17 | 18 |
from django import forms |
19 |
from django.conf import settings |
|
18 | 20 | |
21 |
from mandayejs.mandaye.models import UserCredentials |
|
19 | 22 | |
20 | 23 |
class FormFactory(forms.Form): |
21 | 24 |
def __init__(self, *args, **kwargs): |
22 |
fields = kwargs.pop('locators', None) |
|
23 | 25 |
super(FormFactory, self).__init__(*args, **kwargs) |
24 |
|
|
26 |
fields = getattr(settings, 'SITE_LOCATORS', []) |
|
25 | 27 |
if fields : |
26 | 28 |
for field in fields : |
27 | 29 |
if field['kind'] == 'string': |
... | ... | |
42 | 44 |
help_text=field['help'] |
43 | 45 |
) |
44 | 46 | |
45 |
def is_valid(self,): |
|
46 |
is_valid = super(FormFactory, self).is_valid() |
|
47 |
if not is_valid: |
|
48 |
return is_valid |
|
47 |
def is_valid(self): |
|
48 |
if not super(FormFactory, self).is_valid(): |
|
49 |
return False |
|
49 | 50 | |
50 | 51 |
if self.data == {}: |
51 | 52 |
return False |
52 | 53 | |
53 |
return is_valid
|
|
54 |
return True
|
|
54 | 55 |
mandayejs/mandaye/migrations/0007_auto_20160106_1746.py | ||
---|---|---|
1 |
# -*- coding: utf-8 -*- |
|
2 |
from __future__ import unicode_literals |
|
3 | ||
4 |
from django.db import models, migrations |
|
5 | ||
6 | ||
7 |
class Migration(migrations.Migration): |
|
8 | ||
9 |
dependencies = [ |
|
10 |
('mandaye', '0006_usercredentials_linked'), |
|
11 |
] |
|
12 | ||
13 |
operations = [ |
|
14 |
migrations.AlterField( |
|
15 |
model_name='usercredentials', |
|
16 |
name='linked', |
|
17 |
field=models.BooleanField(default=False, verbose_name='associated'), |
|
18 |
preserve_default=True, |
|
19 |
), |
|
20 |
] |
mandayejs/mandaye/models.py | ||
---|---|---|
23 | 23 |
class UserCredentials(models.Model): |
24 | 24 |
user = models.ForeignKey('auth.User') |
25 | 25 |
locators = JSONField(_('locators'), default={}, blank=True) |
26 |
linked = models.BooleanField(_('associated'), default=True, blank=True)
|
|
26 |
linked = models.BooleanField(_('associated'), default=False, blank=True)
|
|
27 | 27 | |
28 | 28 |
class Meta: |
29 | 29 |
unique_together = ('user',) |
mandayejs/mandaye/views.py | ||
---|---|---|
84 | 84 |
user = get_user_model().objects.get(username=request.user.username) |
85 | 85 |
logger.debug(user) |
86 | 86 |
credentials = UserCredentials.objects.get( |
87 |
user=user, |
|
88 |
linked=True) |
|
87 |
user=user) |
|
89 | 88 |
logger.debug(credentials) |
90 | 89 |
except (UserCredentials.DoesNotExist,): |
91 | 90 |
return HttpResponseRedirect(resolve_url('associate')) |
... | ... | |
97 | 96 |
@login_required |
98 | 97 |
@csrf_exempt |
99 | 98 |
def associate(request, *args, **kwargs): |
100 |
if request.POST: |
|
101 |
credentials, created = UserCredentials.objects.get_or_create(user=request.user) |
|
102 |
credentials.locators = request.POST |
|
103 |
credentials.linked = True |
|
104 |
credentials.save() |
|
105 |
form = FormFactory(request.POST, auto_id=True, locators=settings.SITE_LOCATORS) |
|
106 |
else: |
|
107 |
form = FormFactory(auto_id=True, locators=settings.SITE_LOCATORS) |
|
108 |
if not form.is_valid(): |
|
109 |
site_static_root = getattr(settings, 'SITE_STATIC_ROOT_PATH', '') |
|
110 |
associate_static = getattr(settings, 'SITE_ASSOCIATE_STATIC', |
|
111 |
{'css':'', 'js':''}) |
|
112 | ||
113 |
response = render(request, 'mandaye/associate.html', { |
|
114 |
'form': form, |
|
115 |
'associate_js': os.path.join(site_static_root, associate_static['js']), |
|
116 |
'associate_css': os.path.join(site_static_root, associate_static['css']) |
|
117 |
}) |
|
118 |
return response |
|
99 |
if request.method == 'POST': |
|
100 | ||
101 |
form = FormFactory(request.POST, auto_id=True) |
|
102 |
if form.is_valid(): |
|
103 |
credentials, created = UserCredentials.objects.get_or_create(user=request.user) |
|
104 |
credentials.locators = request.POST |
|
105 |
credentials.linked = False |
|
106 |
credentials.save() |
|
107 | ||
108 |
return HttpResponseRedirect(resolve_url('post-login')) |
|
109 |
else : |
|
110 |
form = FormFactory(auto_id=True) |
|
111 | ||
112 |
site_static_root = getattr(settings, 'SITE_STATIC_ROOT_PATH', '') |
|
113 |
associate_static = getattr(settings, 'SITE_ASSOCIATE_STATIC', |
|
114 |
{'css':'', 'js':''}) |
|
115 | ||
116 |
response = render(request, 'mandaye/associate.html', { |
|
117 |
'form': form, |
|
118 |
'associate_js': os.path.join(site_static_root, associate_static['js']), |
|
119 |
'associate_css': os.path.join(site_static_root, associate_static['css']) |
|
120 |
}) |
|
121 |
return response |
|
119 | 122 | |
120 |
return HttpResponseRedirect(resolve_url('post-login')) |
|
121 | 123 | |
122 | 124 |
@login_required |
123 | 125 |
def dissociate(request, *args, **kwargs): |
... | ... | |
150 | 152 |
logger.debug(login_info) |
151 | 153 |
result = exec_phantom(login_info) |
152 | 154 |
logger.debug(result) |
155 | ||
156 |
User = get_user_model() |
|
157 |
user = User.objects.get(username=request.user.username) |
|
158 |
c_user = user.usercredentials_set.get() |
|
159 | ||
153 | 160 |
if result.get('result') != 'ok': |
154 | 161 |
logger.debug('authentication failed') |
155 |
User = get_user_model() |
|
156 |
user = User.objects.get(username=request.user.username) |
|
157 |
c_user = user.usercredentials_set.get() |
|
158 |
c_user.linked = False |
|
159 |
c_user.save() |
|
162 |
|
|
163 |
|
|
160 | 164 |
logger.debug("redirecting to {}".format(resolve_url('associate'))) |
161 | 165 |
messages.error(request, _('wrong user credentials')) |
162 | 166 |
url = resolve_url('associate') |
163 | 167 |
else: |
168 |
c_user.linked = True |
|
169 |
c_user.save() |
|
164 | 170 |
url = getattr(settings, 'SITE_HOME_PATH', '/') |
165 | 171 | |
166 | 172 |
template = Template('<script type="text/javascript">\ |
167 |
- |