def handle(self, *args, **options): current_link_count = Redirect.objects.count() from django.conf import settings # If there are too many link objects, delete the extras if current_link_count > settings.LINK_COUNT: extra = Redirect.objects.all()[:current_link_count-settings.LINK_COUNT] for redirect in extra: redirect.delete() # Otherwise, add links until the appropriate number is stored elif current_link_count < settings.LINK_COUNT: untagged = api.get('/references/untagged/') while current_link_count < settings.LINK_COUNT: # Get the next link. If out of links, raise RuntimeError try: link = untagged.pop() except IndexError: raise RuntimeError( 'Out of available/relevent links. Consider ' 'Adding more eligible links in the Axiologue ' 'servier or changing the `LINK_COUNT` in settings' ) # skip any reference that already has a redirect link if Redirect.objects.filter(reference_id=link['id']): continue # Create QR code and associated Redirect object Redirect.new_from_link_id(link['id']) current_link_count += 1
def make_redirect(job, business_unit): """Given a job dictionary, make a redirect record Input: :job: A dictionary describing a job. :return: a redirect""" location = "%s-%s" % (job['state_short'], job['city_slab_exact']) # Get or create doesn't support not saving, and Redirects are not valid to # save until new_date is set. guid = '{%s}' % str(uuid.UUID(job['guid'])).upper() try: redirect = Redirect.objects.get(guid=guid) redirect.url = job['link'] redirect.save() return redirect except Redirect.DoesNotExist: logger.debug("Creating new redirect for guid %s", guid) redirect = Redirect(guid=guid, buid=business_unit.id, uid=None, url=job['link'], new_date=job['date_new'], expired_date=None, job_location=location, job_title=job['title_exact'], company_name=job['company']) redirect.save() return redirect
def test_url(settings): settings.REDIRECT_HOST = 'www.foo.com' settings.REDIRECT_SECURE = True redirect = Redirect(path='/test', location='https://www.example.com') assert redirect.url == 'https://www.foo.com/test' settings.REDIRECT_HOST = 'www.foo.com' settings.REDIRECT_SECURE = False redirect = Redirect(path='/test', location='https://www.example.com') assert redirect.url == 'http://www.foo.com/test'
def test_path(): redirect = Redirect.objects.create(path='test', location='https://www.example.com') assert redirect.path == '/test' redirect = Redirect.objects.create(path='/test2', location='https://www.example.com') assert redirect.path == '/test2' with pytest.raises(ValidationError): redirect = Redirect(path='test3?foo=bar', location='https://www.example.com') redirect.full_clean()
def test_redirect_request_partial_gone(self): # Create a redirect request = self.factory.get('/test/123/') redirect = Redirect(from_url='/test/', to_url='', is_partial=True, site=get_current_site(request), http_status=301) redirect.save() new_response = self.run_redirect(request) self.assertEqual(new_response.status_code, 410)
def test_redirect_request_gone(self): # Create a redirect request = self.factory.get('/test/123/') redirect = Redirect(from_url='/test/(?P<pk>\d+)/', to_url='', site=get_current_site(request)) redirect.save() new_response = self.run_redirect(request) self.assertEqual(new_response.status_code, 410)
def test_redirect_request_partial_permanent(self): # Create a redirect request = self.factory.get('/test/123/') redirect = Redirect(from_url='/test/', to_url='/partialtest/', is_partial=True, site=get_current_site(request), http_status=301) redirect.save() new_response = self.run_redirect(request) self.assertEqual(new_response.status_code, 301) assert 'partialtest' in new_response.serialize_headers()
def test_redirect_request_temporary(self): # Create a redirect request = self.factory.get('/test/123/') redirect = Redirect(from_url='/test/(?P<pk>\d+)/', to_url='/somethingelse/(?P<pk>\d+)/', site=get_current_site(request), http_status=302) redirect.save() new_response = self.run_redirect(request) self.assertEqual(new_response.status_code, 302) assert 'somethingelse' in new_response.serialize_headers()
def add(request): if 'url' in request.POST: redirect = Redirect(url=request.POST['url']) redirect.save() short_key = encode_short_key(redirect.id) redirect.short_key = short_key long_key = encode_long_key(redirect.id) redirect.long_key = long_key redirect.save() return HttpResponseRedirect('/r/_d/' + redirect.short_key) else: return render_to_response( 'add.html', {}, context_instance=RequestContext(request))