def test_check_in_use_failure(self): with pytest.raises( Exception ) as exc: # Wide catch, scope narrowed for preventing nested Exception override in_use = Redirect.check_in_use(shortcode='xxx') assert isinstance(exc.type, ShortcodeNotFound.__class__) # Sanity check
def get_url(shortcode): """ This endpoint method handles the redirect requests, routed to the shortcode specific routing url and being a GET request. The Redirect database model specific methods will handle the logic. :param shortcode: The provided shortcode as url parameter. :type shortcode: str :return: The response with the corresponding url for the provided shortcode added to the Location header. :rtype: flask.Response .. note:: The Redirect database model specific methods handle the logic and exception handling for this endpoint, as the endpoint relies heavily on database specific logic. .. seealso:: See for database model related methods: src/models.py See for exception related exceptions: src/exceptions.py """ redirect_url = Redirect.redirect(shortcode=shortcode) response = jsonify() response.status_code = 302 response.headers['Location'] = redirect_url return response
def get_object(self): source = slugify(self.kwargs.get('slug')) redirect = Redirect.objects.filter(source=source, region=self.get_region()) if redirect: return redirect[0] return Redirect(source=source, region=self.get_region())
def get(self, *args, **kwargs): try: if not self.request.path: return self.redirect('http://justcramer.com', permanent=True); try: result = Redirect.all().filter('origin =', self.request.path).fetch(1)[0] except IndexError: return self.redirect('http://justcramer.com', permanent=True); return self.redirect('http://justcramer.com%s' % result.dest, permanent=True) except Exception, e: logging.error(e) return self.redirect('http://justcramer.com', permanent=True);
def create(self, request): # set everything up key = request.POST.get('key') or request.GET.get('key') url = request.POST.get('url') or request.GET.get('url') # set up response response = HttpResponse(mimetype='json/application') # post needs to have a key and url, fail if not both if not bool(url and key): response.content = json.dumps({'code': 101, 'url' : 'http://%s%s' % (Site.objects.get_current(), reverse('faq:view', args=[101]) ) } ) response.status_code = 400 return response # make sure key is correct try: user = User.objects.get(userprofile__key=key) except User.DoesNotExist: response.content = json.dumps({'code': 102, 'url' : 'http://%s%s' % (Site.objects.get_current(), reverse('faq:view', args=[102]) ) } ) response.status_code = 400 return response # either 200 or 201 # # try: r = Redirect.objects.get(url=url, user=user) response.status_code = 200 except Redirect.DoesNotExist: r = Redirect() r.url = url r.user = user r.save() response.status_code = 200 # return the response #response.content = serializers.serialize('json', [r]) response.content = json.dumps({'shortUrl' : r.shorturl}) response['Location'] = r.get_absolute_url() return response
def get(self, *args, **kwargs): if constants.WARM_UP_PATH in self.request.url: return self.response.out.write('Warming Up...') # TODO: Re construct the original URL _, netloc, _, _, _ = urlparse.urlsplit(self.request.url) # Discard any port number from the hostname netloc = netloc.split(':', 1)[0] redirect = Redirect.get_by_source(netloc) if not redirect: logging.error('Unable to redirect this url [%s]', self.request.url) self.abort(404) logging.info('Redirect successful from [%s] to [%s] permanent? [%s]', self.request.url, redirect.destination, redirect.permanent) # TODO: Store permanent in Redirect model return self.redirect(str(redirect.destination), redirect.permanent)
def post(self): self.parser.add_argument('short_path', required=True, type=str,help='short link must be a string') self.parser.add_argument('referrer', required=True, type=str, help='referrer is required'); args = self.parser.parse_args() url = Url.query.filter((Url.short_path == args['short_path'])).first() if url is None: return {'message':'Short link does not exist'}, 404 if url.group_id is None: group_id = None else: group_id = url.group_id if current_app.config['CLIENT_URL'] not in args['referrer']: redirect = Redirect(user_id=url.user.id, url_id=url.id, group_id=group_id) db.session.add(redirect) db.session.commit() return marshal(url, self.url_field, envelope='data'), 201
def test_redirect_to_self(self): p = Page(name="foobar", content="<p>foobar</p>") p.save() r = Redirect(source='foobar', destination=p) self.assertRaises(exceptions.RedirectToSelf, r.save)
def test_check_in_use_true(self): assert Redirect.check_in_use(shortcode='090909')
def test_redirect_in_use_success(self): url = Redirect.redirect(shortcode='090909') assert url == 'scenario8.com'
def test_check_in_use_false(self): assert Redirect.check_in_use(shortcode='090909') is False
def create_object(self): return Redirect(source=slugify(self.kwargs['slug']))
def get_object(self): return Redirect(source=slugify(self.kwargs.get('slug')))
def get_object(self): return Redirect(source=slugify(self.kwargs.get('slug')), region=self.get_region())