def signup_view(cls): data = cls.validate(cls._get_json_from_request()) chimp = Chimp.get_where({'shop': data['shop']}) client = MailChimp(chimp['username'], chimp['access_token']) try: response = client.lists.members.create( chimp['list_id'], { 'email_address': data['email'], 'status': 'subscribed' }) return cls._format_response({ 'email': data['email'], 'list_id': chimp['list_id'], }) except Exception as e: return to_json( { 'email': data['email'], 'list_id': chimp['list_id'], 'error': e.response.json()['title'] }, 400)
def _format_response(cls, response): try: if request.headers['Accept'] == 'application/json': return to_json(response) else: cached_template = app.caches[cls.endpoint].get(request.path) if cached_template is None or request.current_session_is_admin or app.config['DEBUG']: for template in cls.templates: if template['view_function'] == request.url_rule.route['view_function']: try: response = getattr(cls, template['prerender_process'])(response) except KeyError: pass response = { template['response_key']: response.copy(), 'pieces': Piece._values(), 'root': request.host_url, 'debugging': app.config['DEBUG'] } response['pieces_json'] = json.dumps(response['pieces'], sort_keys=False, default=json_formater) try: template['template'] = template['template'].replace('<route>', request.view_args['_id']) except KeyError: pass try: template['template'] = template['template'].replace('<parent_route>', request.view_args['parent_id']) except KeyError: pass render = render_template(template['template'], **response) if not request.current_session_is_admin: app.caches[cls.endpoint].set(request.path, render, timeout=0) return render else: return cached_template except KeyError: return to_json(response)
def raise_error(category, key, code=500, fields=None, no_abort=False): errors = {} document = {'category': category, 'key': key, 'code': code} if fields is not None: document['fields'] = fields try: document['message'] = errors[category][key] except KeyError: pass if no_abort: return to_json(document, code) else: abort(to_json(document, code))
def _format_response(cls, response): try: if 'application/json' in request.headers['Accept']: return to_json(response) else: cached_template = app.caches[cls.endpoint].get( request.path) if cached_template is None or request.current_session_is_admin or app.config[ 'DEBUG']: for template in cls.templates: if template[ 'view_function'] == request.url_rule.route[ 'view_function']: try: response = getattr( cls, template['prerender_process'])( response) except KeyError: pass response = { template['response_key']: response.copy(), 'pieces': {}, 'debugging': app.config['DEBUG'], 'current_path': request.path, 'root': request.host_url } response['pieces_json'] = json.dumps( response['pieces'], sort_keys=False, default=json_formater) template_name = template['template'] try: template_name = template_name.replace( '<route>', str(request.view_args['_id'])) except KeyError: pass try: template_name = template_name.replace( '<parent_route>', request.view_args['parent_id']) except KeyError: pass render = render_template( template_name, **response) if not request.current_session_is_admin: app.caches[cls.endpoint].set(request.path, render, timeout=0) return render else: return cached_template except KeyError: return to_json(response)
def _format_response(cls, data): return to_json(data)
def _format_response(cls, response): try: if 'application/json' in request.headers['Accept']: return to_json(response) else: # cached_template = app.caches[cls.endpoint].get(request.path) # if cached_template is None or app.config['DEBUG']: for template in cls.templates: if template['view_function'] == request.url_rule.route[ 'view_function']: try: response = getattr( cls, template['prerender_process'])(response) except KeyError: pass from core.models.cms.interface import Interface from core.models.cms.page import Page from core.models.hotels.hotel import Hotel response = { template['response_key']: response.copy(), 'interface': Interface.get(lang=request.url_rule.lang), 'pages': Page.list(lang=request.url_rule.lang), 'hotels': Hotel.list(lang=request.url_rule.lang), 'debugging': app.config['DEBUG'], # 'stripe_key': app.config['STRIPE_PUBLISHABLE_KEY'] } response['interface_json'] = json.dumps( response['interface'], sort_keys=False, default=json_formater) if request.url_rule.lang is None: response['lang_route'] = '/' response['current_path'] = request.path else: response['lang'] = request.url_rule.lang response[ 'lang_route'] = '/' + request.url_rule.lang + '/' response[ 'current_path'] = request.path.replace( response['lang_route'], '/') template_name = template['template'] try: template_name = template_name.replace( '<route>', str(request.view_args['_id'])) except KeyError: pass try: template_name = template_name.replace( '<parent_route>', request.view_args['parent_id']) except KeyError: pass render = render_template(template_name, **response) # app.caches[cls.endpoint].set(request.path, render, timeout=0) return render # else: # return cached_template except KeyError: return to_json(response)
def _format_response(cls, response): return to_json(response)