Commit 590d4941 authored by Bryan  BRANCOTTE's avatar Bryan BRANCOTTE
Browse files

Validate id_source to prevent error in autofill

parent d3f842c8
......@@ -3,21 +3,23 @@ Models used in iPPI-DB
"""
from __future__ import unicode_literals
import operator
import re
from django.db import models
from django.conf import settings
from django.db.models import Max, Min, Count, F, Q, Case, When, Sum
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import FloatField, IntegerField, BooleanField
from django.db.models import Max, Count, F, Q, Case, When
from django.db.models.functions import Cast
from django.db.models import FloatField, IntegerField, BooleanField
from django.utils.translation import ugettext_lazy as _
from .utils import FingerPrinter, smi2inchi, smi2inchikey
from .ws import get_pubmed_info, get_google_patent_info, get_uniprot_info, get_taxonomy_info, get_go_info, get_pfam_info
class AutoFillableModel(models.Model):
"""
AutoFillableModel makes it possible to automatically fill model fields from
external sources in the autofill() method
......@@ -46,6 +48,11 @@ class Bibliography(AutoFillableModel):
('PT', 'Patent'),
('DO', 'DOI ID')
)
id_source_validators = dict(
PM=re.compile("^[0-9]+$"),
PT=re.compile("^.*$"),
DO=re.compile("^.*$"),
)
source = models.CharField(
'Bibliographic type', max_length=2, choices=SOURCES, default='PM')
id_source = models.CharField('Bibliographic ID', max_length=25)
......@@ -76,6 +83,16 @@ class Bibliography(AutoFillableModel):
self.authors_list = info['authors_list']
self.biblio_year = info['biblio_year']
def clean(self):
super().clean()
id_source_validator = self.id_source_validators[self.source]
if not id_source_validator.match(self.id_source):
raise ValidationError(
dict(
id_source=_("Must match pattern %s for this selected source" % id_source_validator.pattern)
)
)
class Meta:
verbose_name_plural = "bibliographies"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment