def ws_prs(self, url, cancer='breast'): ''' Test affect of including the PRS. ''' data = { 'mut_freq': 'UK', 'cancer_rates': 'UK', 'pedigree_data': self.pedigree_data, 'user_id': 'test_XXX' } self.client.credentials(HTTP_AUTHORIZATION='Token ' + WSRiskFactors.token.key) response = self.client.post(url, data, format='multipart', HTTP_ACCEPT="application/json") self.assertEqual(response.status_code, status.HTTP_200_OK) cancer_risks1 = json.loads(force_text( response.content))['pedigree_result'][0]['cancer_risks'] # add PRS data['prs'] = json.dumps({'alpha': 0.45, 'zscore': 2.652}) response = self.client.post(url, data, format='multipart', HTTP_ACCEPT="application/json") self.assertEqual(response.status_code, status.HTTP_200_OK) cancer_risks2 = json.loads(force_text( response.content))['pedigree_result'][0]['cancer_risks'] self.assertLess(cancer_risks1[0][f'{cancer} cancer risk']['decimal'], cancer_risks2[0][f'{cancer} cancer risk']['decimal'])
def ws_risk_factor(self, url, bmi, cancer='breast'): ''' Test affect of including the risk factors. ''' data = { 'mut_freq': 'UK', 'cancer_rates': 'UK', 'pedigree_data': self.pedigree_data, 'user_id': 'test_XXX' } self.client.credentials(HTTP_AUTHORIZATION='Token ' + WSRiskFactors.token.key) response = self.client.post(url, data, format='multipart', HTTP_ACCEPT="application/json") self.assertEqual(response.status_code, status.HTTP_200_OK) cancer_risks1 = json.loads(force_text( response.content))['pedigree_result'][0]['cancer_risks'] # add BMI data['pedigree_data'] = self.pedigree_data.replace( "##CanRisk 2.0", f"##CanRisk 2.0\n##BMI={bmi}") response = self.client.post(url, data, format='multipart', HTTP_ACCEPT="application/json") self.assertEqual(response.status_code, status.HTTP_200_OK) cancer_risks2 = json.loads(force_text( response.content))['pedigree_result'][0]['cancer_risks'] self.assertLess(cancer_risks1[0][f'{cancer} cancer risk']['decimal'], cancer_risks2[0][f'{cancer} cancer risk']['decimal'])
def fallback_schema_from_field(self, field): """ Fallback schema for field that isn't inspected properly by DRF and probably won't land in upstream canon due to its hacky nature only for doc purposes """ title = force_text(field.label) if field.label else '' description = force_text(field.help_text) if field.help_text else '' # since we can't really inspect dictfield and jsonfield, at least display object as type # instead of string if isinstance(field, (serializers.DictField, serializers.JSONField)): return coreschema.Object(properties={}, title=title, description=description)
def get_path_fields(self, path, method, view): """ Return a list of `coreapi.Field` instances corresponding to any templated path variables. """ model = getattr(getattr(view, 'queryset', None), 'model', None) fields = [] for variable in uritemplate.variables(path): if variable == 'version': continue title = '' description = '' schema_cls = coreschema.String kwargs = {} if model is not None: # Attempt to infer a field description if possible. try: model_field = model._meta.get_field(variable) except: model_field = None if model_field is not None and model_field.verbose_name: title = force_text(model_field.verbose_name) if model_field is not None and model_field.help_text: description = force_text(model_field.help_text) elif model_field is not None and model_field.primary_key: description = get_pk_description(model, model_field) if hasattr(view, 'lookup_value_regex' ) and view.lookup_field == variable: kwargs['pattern'] = view.lookup_value_regex elif isinstance(model_field, models.AutoField): schema_cls = coreschema.Integer field = Field(name=variable, location='path', required=True, schema=schema_cls(title=title, description=description, **kwargs)) fields.append(field) return fields
def activate(request, uidb64, token): account_activation_token = AccountActivationTokenGenerator() try: uid = force_text(urlsafe_base64_decode(uidb64)) user = User.objects.get(pk=uid) except (TypeError, ValueError, OverflowError, User.DoesNotExist): user = None if user is not None and account_activation_token.check_token(user, token): user.is_active = True user.save() return Response( {"message": "You have activated account, now you can Log In!"})
def get_path_parameters(self, path, view_cls): """Return a list of Parameter instances corresponding to any templated path variables. :param str path: templated request path :param type view_cls: the view class associated with the path :return: path parameters :rtype: list[openapi.Parameter] """ parameters = [] model = getattr(getattr(view_cls, 'queryset', None), 'model', None) for variable in uritemplate.variables(path): pattern = None type = openapi.TYPE_STRING description = None if model is not None: # Attempt to infer a field description if possible. try: model_field = model._meta.get_field(variable) except Exception: # pragma: no cover model_field = None if model_field is not None and model_field.help_text: description = force_text(model_field.help_text) elif model_field is not None and model_field.primary_key: description = get_pk_description(model, model_field) if hasattr(view_cls, 'lookup_value_regex') and getattr( view_cls, 'lookup_field', None) == variable: pattern = view_cls.lookup_value_regex elif isinstance(model_field, django.db.models.AutoField): type = openapi.TYPE_INTEGER field = openapi.Parameter( name=variable, required=True, in_=openapi.IN_PATH, type=type, pattern=pattern, description=description, ) parameters.append(field) return parameters
def mockreturn(self, request): codec = CoreJSONCodec() body = force_text(request.body) content = codec.encode(Document(content={'data': json.loads(body)})) return MockResponse(content)