def upload_template(request, id=None): template = get_template_from_session(request, id) if template: form = UploadTemplateForm(instance=template) else: form = UploadTemplateForm(instance=None) errors = [] if request.method == "POST": if template: form = UploadTemplateForm(request.POST, request.FILES, instance=template) else: form = UploadTemplateForm(request.POST, request.FILES) session = utils.get_session_obj(request) if form.is_valid(): form.save() session['template_id'] = form.instance.id # add keywords add_keywords(request, form.instance) if id: return HttpResponseRedirect('/admin/document/template-details/%s/' % id) else: return HttpResponseRedirect('/admin/document/template-details/') else: for er in form.errors: errors.append(form.errors[er]) ctxt = { 'form': form, 'errors': errors, 'id': id, 'template': template, } return render_to_response('riba-admin/document/upload_template.html', ctxt, context_instance=RequestContext(request))
def finalize_template(request): template = get_template_from_session(request) if not template: raise Http404 form = FinalTemplateForm(instance=template) inline_formset = inlineformset_factory(Template, Questionnaire, form = FinalQuestionnaireForm, extra=0) formset = inline_formset(instance = template) if request.method == "POST": form = FinalTemplateForm(request.POST, request.FILES, instance = template) formset = inline_formset(request.POST, request.FILES, instance = template) if form.is_valid(): qn = form.save() for f in formset: if f.is_valid() and f.cleaned_data.get('question'): f.save() else: print formset.errors[f] template.state = 'submitted' template.save() session = utils.get_session_obj(request) template_id = session.get('template_id', None) if template_id: session['template_id'] = None return HttpResponseRedirect('/document/') ctxt = { 'template': template, 'form': form, 'formset': formset, } return render_to_response('riba-admin/document/finalize_template.html', ctxt, context_instance=RequestContext(request))
def get_template_from_session(request, id=None): session = utils.get_session_obj(request) template_id = session.get('template_id', None) if id: session['template_id'] = id template_id = id template = '' if template_id: try: template = Template.objects.get(pk=template_id) except: pass return template
def finalize_template(request): template = get_template_from_session(request) if not template: raise Http404 form = FinalTemplateForm(instance=template) inline_formset = inlineformset_factory(Template, Questionnaire, form=FinalQuestionnaireForm, extra=0) formset = inline_formset(instance=template) if request.method == "POST": form = FinalTemplateForm(request.POST, request.FILES, instance=template) formset = inline_formset(request.POST, request.FILES, instance=template) if form.is_valid(): qn = form.save() for f in formset: if f.is_valid() and f.cleaned_data.get('question'): f.save() else: print formset.errors[f] template.state = 'submitted' template.save() session = utils.get_session_obj(request) template_id = session.get('template_id', None) if template_id: session['template_id'] = None return HttpResponseRedirect('/document/') ctxt = { 'template': template, 'form': form, 'formset': formset, } return render_to_response('riba-admin/document/finalize_template.html', ctxt, context_instance=RequestContext(request))
def upload_template(request, id=None): template = get_template_from_session(request, id) if template: form = UploadTemplateForm(instance=template) else: form = UploadTemplateForm(instance=None) errors = [] if request.method == "POST": if template: form = UploadTemplateForm(request.POST, request.FILES, instance=template) else: form = UploadTemplateForm(request.POST, request.FILES) session = utils.get_session_obj(request) if form.is_valid(): form.save() session['template_id'] = form.instance.id # add keywords add_keywords(request, form.instance) if id: return HttpResponseRedirect( '/admin/document/template-details/%s/' % id) else: return HttpResponseRedirect( '/admin/document/template-details/') else: for er in form.errors: errors.append(form.errors[er]) ctxt = { 'form': form, 'errors': errors, 'id': id, 'template': template, } return render_to_response('riba-admin/document/upload_template.html', ctxt, context_instance=RequestContext(request))
def get_api_response(request, agent,path, cookie_file, method='get', params={}, post_data=None): import socket socket.setdefaulttimeout(600) URL = '%s:%s' % (FB_API_URL, FB_API_PORT) if request.client.domain in FB_API_OTHER_URLS: URL = 'http://%s:%s' % (request.client.domain, FB_API_PORT) url = '%s/api/%s' % (URL, path) cj = StringCookieJar(cookie_file) session_id = get_session_id(cj) proxy_support = urllib2.ProxyHandler({}) opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj),proxy_support) sha1 = hashlib.sha1() sha1.update(str(random.random())) data = sha1.hexdigest() #api_response = APIResponse() #api_response.client = 'FB' #api_response.save() api_key = 'AAAAB3NzaC1yc2EAAAABIwAAAQEAmj71mIXq6+cUMZ+s/2HPyAHFJh5cpflE+J2h+WBRJlLGJtb/ArvAKszaMO4q/aQpD0lB5s1yhGVNS6Hoq1GHaCSiuF71sV34I4txVfOcvLsXmvW3+8Y5hcAJoWfXPj+oeCLZAhkOhUSANHUc54syk4N8rH8nEP+ank78jvttBoTyIn54z0msO/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CpFdDI2cXci6czYB2i/phu2ByzZNrAeCyF81KAZlmto+f24jq3HolJgGaUvA/kWGrhHNJ10XY+LtZiLTBsRyxF0tN7W8hUmV8RnCnJkQ3gZbfbyC+Wa36Lk19sBeGKUsJrLMvRYr1C/HTOKzSUZeqD2CnGPlfPfAoGANNBTygQQwbjrrwx6zbb76C5kB1uq4AjdXGsEYojlf5DfhNrHS35JWTQepi8K/XJ59OaryzzYfZdHeupSs8LulR3D8Wbtu+SQkw6EEHd/Aa14YrhweCUnTy2vNfkBV2TRKBs03kfJziZ+itvrN9WTO7/qEwTVf7lGfDyGZz7unfUCgYAxb/epQSGxsqRFYlEUOeKpm0NvMDxrZHsrsnbfpZG4Qj1gYe2IQRgM/bygVAdug8qnd8wUiG68ZMUK0Hs+bmEzilNEe5c6KSWWxj0jW9fZjYRIckSD0KOiijI4L5yowWy028J6JPMSCPo2bsP1sGeYa6hsVuRLXlK7rPtfB6o3ZQKBgAivtq1I3hoSC+ToZroG3HmoN2PRB3syXjdJs9xnm7243Rg9YH2Qr7Knj8xhTr3nUN8q2Vk4LzNn5uW5zapZz+iiT0vbqdni18DgLWNd6NgjFeNrMyTUFk8cfAmLNAcMyYO2HQMTQTYnn5AGoC5eoQyzTFOaDtEs0TnBsDrOAsgI' sha2 = hashlib.sha1() sha2.update('%s%s%s' % (data, api_key, private_key)) signature = sha2.hexdigest() tinla_order_ref = '' try: if 'cart_id' in request.session: tinla_order_ref = request.session['cart_id'] from utils import utils cs = utils.get_session_obj(request) fbapiobj = cs.get('fbapiobj', None) if fbapiobj: if fbapiobj.add_to_cart_response: tinla_order_ref = fbapiobj.add_to_cart_response['items'][0]['orderId'] except: pass if method == 'get' and params: #params.update({'requestId':api_response.id}) headers = { 'X-FBAPI-KEY': api_key, 'X-FBAPI-DATA': data, 'X-FBAPI-SIGNATURE': signature, 'X-FBAPI-AGENT': agent, 'X-FBAPI-ID': '0', #str(api_response.id), 'tinlaHostIPAddr': socket.gethostname(), 'tinlaSessionId': session_id, 'tinlaOrderId': str(tinla_order_ref), } url = '%s?%s' % (url, urllib.urlencode(params)) req = urllib2.Request(url, None, headers) if method == 'post' and post_data: #post_data.update({'requestId':api_response.id}) log.info('Agent: %s, POST DATA: %s' % (agent,post_data)) headers = { 'Content-Type':'application/json; charset=UTF-8', 'X-FBAPI-KEY': api_key, 'X-FBAPI-DATA': data, 'X-FBAPI-SIGNATURE': signature, 'X-FBAPI-AGENT': agent, 'X-FBAPI-ID': '0', #str(api_response.id), 'tinlaHostIPAddr': socket.gethostname(), 'tinlaSessionId': session_id, 'tinlaOrderId': str(tinla_order_ref) } encoder = simplejson.JSONEncoder() data = encoder.encode(post_data) req = urllib2.Request(url, data, headers) print datetime.now() log.info('API REQUEST %d %s: %s: DATA: %s' % (0, session_id, url, post_data)) try: try: response = opener.open(req) info = response.info() if info.getheaders('Set-Cookie'): log.info('Got new cookie for %s - %s' % (session_id, info.getheaders('Set-Cookie'))) except urllib2.URLError, e: log.exception('Error doing api request: %s' % repr(e)) import time time.sleep(0.5) try: response = opener.open(req) info = response.info() if info.getheaders('Set-Cookie'): log.info('Got new cookie for %s - %s' % (session_id, info.getheaders('Set-Cookie'))) except urllib2.HTTPError, error: raise APIError('Error reading response %s: %s' % (error.read(), repr(error)))