def __init__(self, environ, populate_request=True, shallow=False): ResponseBase.__init__(self) RequestBase.__init__(self, environ, populate_request, shallow) self.href = Href(self.script_root or '/', self.charset) self.abs_href = Href(self.url_root, self.charset) self.headers = Headers([('Content-Type', 'text/html')]) self.response = [] self.status_code = 200
def make_link(self, page): if self.link is None: if self.page == 1: href = Href() else: href = Href(u'../../') else: href = Href(self.link) if page == 1: return href(**self.args) return href(u'page/%d/' % page, **self.args)
def edit_deal(deal_id): ''' This function is used to allow a user to edit the description of a deal that he or she has submitted ''' deal = Deal.objects(id=deal_id).first() if deal is None: return abort(404) user = get_current_user() if user is None or str(get_current_user().id) != deal.author_id: return abort(404) msg = {} form = Edit_Form(csrf_enabled=False) if request.method == 'POST' and form.validate_on_submit(): print request.form description = request.form.get('description', None) if description: deal.description = description deal.edited = datetime.now() deal.save() store_deal(deal, overwrite=True) next = Href('/') next = next('deals', deal.sequence_num, deal.short_title) msg['status'] = 'success' else: msg['status'] = 'error' msg['description_error'] = form.description.errors[0] return jsonify(msg)
def make_template(self): if self.link is None: if self.page == 1: base = u'page/!/' else: base = u'../!/' else: base = u'%spage/!/' % self.link return Href(base)(**self.args)
def test_href_past_root(): """Href() over root does not break the URL.""" raise SkipTest('currently not implemented, stdlib bug?') base_href = Href('http://www.blagga.com/1/2/3') assert base_href('../foo') == 'http://www.blagga.com/1/2/foo' assert base_href('../../foo') == 'http://www.blagga.com/1/foo' assert base_href('../../../foo') == 'http://www.blagga.com/foo' assert base_href('../../../../foo') == 'http://www.blagga.com/foo' assert base_href('../../../../../foo') == 'http://www.blagga.com/foo' assert base_href('../../../../../../foo') == 'http://www.blagga.com/foo'
def post_deal(): form = Deal_Form() # if current_user is None: # msg = {"status": "error", "message": "user not logged in"} # return msg if request.method == 'GET': return render_template('post_deal.html', form=form) elif request.method == 'POST': if form.validate_on_submit(): title = request.form.get('title') title = string.strip(title) short_title = title[0:short_title_length - 1] short_title = string_to_url_fix(short_title) category = request.form.get('categories') category = string.lower(category) location = request.form.get('location', None) if location == "": location = None if location and is_amazon_url(location): location = gen_amazon_affiliate_url(location) description = request.form.get('description', None) user = get_current_user() ip = request.remote_addr new_deal = Deal(title=title, short_title=short_title, location=location, category=category, description=description, author_id=str(user.id), num_votes=1, ip=ip) new_deal.save() new_deal_id = new_deal.id # updating redis cache store_deal(new_deal) insert_new_deal_into_list(category, new_deal.sequence_num) set_user_action_as_completed('vote', new_deal_id, user.sequence_num) for sort in sorts: r.delete("".join([user.name, '_', 'shared', '_', sort])) # updating mongodb or datastore user.deals_voted.append(str(new_deal_id)) user.deals_submitted.append(str(new_deal_id)) user.save() celery_tasks.upvote.delay(new_deal_id, user.id, request.remote_addr) #building this deal's url so to redirect the user next = Href('/') next = next('deals', new_deal.sequence_num, new_deal.short_title) msg = {'status': 'success', 'redirect': next} return jsonify(msg) else: #if form returns errors, return the errors to the users via js msg = {"status": "error"} if form.title.errors: msg["title_error"] = form.title.errors[0] if form.location.errors: msg["location_error"] = form.location.errors[0] if form.categories.errors: msg["category_error"] = form.categories.errors[0] if form.description.errors: msg["description_error"] = form.description.errors[0] return jsonify(msg) else: abort(404)
def test_href(): """Test the Href class""" x = Href('http://www.example.com/') assert x('foo') == 'http://www.example.com/foo' assert x.foo('bar') == 'http://www.example.com/foo/bar' assert x.foo('bar', x=42) == 'http://www.example.com/foo/bar?x=42' assert x.foo('bar', class_=42) == 'http://www.example.com/foo/bar?class=42' assert x.foo('bar', {'class': 42}) == 'http://www.example.com/foo/bar?class=42' assert_raises(AttributeError, lambda: x.__blah__) x = Href('blah') assert x.foo('bar') == 'blah/foo/bar' assert_raises(TypeError, x.foo, {"foo": 23}, x=42) x = Href('') assert x('foo') == 'foo'