contacts: add to authentic then get id from wcs (#8190)

This currently relies on HTTP Basic authentication, username/passwords should
be set in the settings, as AUTHENTIC_AUTH_TUPLE.
This commit is contained in:
Frédéric Péters 2015-11-16 14:32:31 +01:00
parent 4d7d626cf9
commit 62429906e6
4 changed files with 54 additions and 8 deletions

View File

@ -15,8 +15,12 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
import random
import requests
import time
from django import template
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.http import HttpResponse
from django.template import RequestContext
@ -24,7 +28,7 @@ from django.views.decorators.csrf import csrf_exempt
from django.views.generic import TemplateView, FormView
from welco.qualif.models import Association
from welco.utils import get_wcs_data
from welco.utils import get_wcs_data, get_authentic_url
from .forms import ContactAddForm
@ -113,9 +117,30 @@ class ContactAdd(FormView):
form = self.get_form(self.get_form_class())
msg = {}
for field_key in form.fields:
msg[field_key] = form[field_key].value()
if form[field_key].value():
msg[field_key] = form[field_key].value()
msg['password'] = str(random.SystemRandom().random())
authentic_response = requests.post(
get_authentic_url() + 'api/users/',
data=json.dumps(msg),
headers={'Content-type': 'application/json'},
auth=settings.AUTHENTIC_AUTH_TUPLE)
user_uuid = authentic_response.json().get('uuid')
for i in range(20):
try:
user_details = get_wcs_data('api/users/%s/' % user_uuid)
result = {'data': {'user_id': user_details.get('id')}}
break
except requests.HTTPError as e:
if e.response.status_code != 404:
raise
time.sleep(0.1)
else:
result = {'err': 1, 'data': 'timeout when calling wcs'}
response = HttpResponse(content_type='application/json')
result = {'data': []}
json.dump(result, response, indent=2)
return response

View File

@ -168,6 +168,8 @@ VALIDATION_STEPS = {
'mail': ['done-qualif', 'done-dgs', 'done-dga'],
}
AUTHENTIC_AUTH_TUPLE = ('username', 'password')
local_settings_file = os.environ.get('WELCO_SETTINGS_FILE',
os.path.join(os.path.dirname(__file__), 'local_settings.py'))

View File

@ -280,17 +280,34 @@ $(function() {
);
});
$(document).on('gadjo:dialog-done', function(ev) {
$(document).on('gadjo:dialog-done', function(ev, data) {
if (ev.target && ev.target.id != 'create-new-contact') return;
$.ajax({url: $('#create-new-contact').data('url'),
data: $('form.contact-add').serialize(),
var source_type = $('div.source div[data-source-type]').data('source-type');
var source_pk = $('div.source .active[data-source-pk]').data('source-pk');
var selected_user_id = data.data.user_id;
$.ajax({url: $('.cell.contacts').data('zone-url'),
data: {user_id: selected_user_id,
source_type: source_type,
source_pk: source_pk},
method: 'POST',
dataType: 'json',
dataType: 'html',
success: function(data) {
console.log('got data', data);
var fragment_url = $('.cell.contacts [data-contact-fragment-url]'
).data('contact-fragment-url') + selected_user_id + '/';
$.ajax({url: fragment_url,
method: 'GET',
data: {source_type: source_type, source_pk: source_pk},
dataType: 'html',
success: function(data) {
$('.contacts .contact').replaceWith(data);
$('.contacts').addClass('has-contact-displayed');
},
error: function(error) { console.log(':(', error); }
});
},
error: function(error) { console.log(':/', error); }
});
});
$('#postit').on('click', window.displayPopup);

View File

@ -56,6 +56,8 @@ def sign_string(s, key, algo='sha256', timedelta=30):
hash = hmac.HMAC(str(key), digestmod=digestmod, msg=s)
return hash.digest()
def get_authentic_url():
return settings.KNOWN_SERVICES.get('authentic').items()[0][1]['url']
def get_wcs_services():
return settings.KNOWN_SERVICES.get('wcs')