def remove(request, link_id): ''' Remove a specific link with ID ``link_id`` ''' from django.forms import IntegerField from django.forms import ValidationError field = IntegerField(min_value=1) try: value = field.clean(link_id) except ValidationError, err: jd = json.dumps({'status': 1, 'messages': '\n'.join(err.messages)}) return HttpJSONResponseBadRequest(content=jd)
def add(request): '''Add new link to a specific target The target should be a valid model within TCMS, which are documented in ``LINKREF_TARGET``. Incoming request should be a POST request, and contains following arguments: - target: To which the new link will link to. The avialable target names are documented in the ``LINKREF_TARGET``. - target_id: the ID used to construct the concrete target instance, to which the new link will be linked. - name: a short description to this new link, and accept 64 characters at most. - url: the actual URL. ''' form = AddLinkReferenceForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] url = form.cleaned_data['url'] target_id = form.cleaned_data['target_id'] model_class = form.cleaned_data['target'] model_instance = model_class.objects.get(pk=target_id) create_link(name=name, url=url, link_to=model_instance) jd = json.dumps({ 'status': 0, 'message': '', 'data': { 'name': name, 'url': url } }) return HttpJSONResponse(content=jd) else: jd = json.dumps({'status': 1, 'message': form.errors.as_text()}) return HttpJSONResponseBadRequest(content=jd)
model_instance = model_class.objects.get(pk=target_id) links = LinkReference.get_from(model_instance) except Exception, err: jd = json.dumps({'status': 1, 'message': str(err)}) return HttpJSONResponseServerError(content=jd) jd = [] for link in links: jd.append({'name': link.name, 'url': link.url}) jd = json.dumps(jd) return HttpJSONResponse(content=jd) else: jd = json.dumps({'status': 1, 'message': form.errors.as_text()}) return HttpJSONResponseBadRequest(content=jd) @user_passes_test(lambda u: u.has_perm('testruns.change_testcaserun')) @require_GET def remove(request, link_id): ''' Remove a specific link with ID ``link_id`` ''' from django.forms import IntegerField from django.forms import ValidationError field = IntegerField(min_value=1) try: value = field.clean(link_id) except ValidationError, err: jd = json.dumps({'status': 1, 'messages': '\n'.join(err.messages)})
def test_HttpJSONResponseBadRequest(self): resp = HttpJSONResponseBadRequest(content='{}') self.assert_(isinstance(resp, HttpResponse)) self.assertEqual(resp['content-type'], 'application/json') self.assertEqual(resp.status_code, 400)