forked from enviPath/enviPy
wip
This commit is contained in:
@ -1,11 +1,15 @@
|
||||
from typing import List
|
||||
|
||||
import urllib.parse
|
||||
import nh3
|
||||
from django.conf import settings as s
|
||||
from django.db import models
|
||||
from django.db import models, transaction
|
||||
from django.db.models import QuerySet
|
||||
from django.urls import reverse
|
||||
|
||||
from epdb.models import (
|
||||
EnviPathModel,
|
||||
Compound,
|
||||
CompoundStructure,
|
||||
ParallelRule,
|
||||
SequentialRule,
|
||||
SimpleAmbitRule,
|
||||
@ -95,4 +99,70 @@ class Package(EnviPathModel):
|
||||
return rules
|
||||
|
||||
class Meta:
|
||||
db_table = "epdb_package"
|
||||
db_table = "epdb_package"
|
||||
|
||||
|
||||
class PESCompound(Compound):
|
||||
|
||||
@staticmethod
|
||||
@transaction.atomic
|
||||
def create(
|
||||
package: "Package", pes_data: dict, name: str = None, description: str = None, *args, **kwargs
|
||||
) -> "Compound":
|
||||
|
||||
pes_url = pes_data["pes_url"]
|
||||
|
||||
# Check if we find a direct match for a given pes_link
|
||||
if PESStructure.objects.filter(pes_link=pes_url, compound__package=package).exists():
|
||||
return PESStructure.objects.get(pes_link=pes_url, compound__package=package).compound
|
||||
|
||||
# Generate Compound
|
||||
c = PESCompound()
|
||||
c.package = package
|
||||
|
||||
if name is not None:
|
||||
# Clean for potential XSS
|
||||
name = nh3.clean(name, tags=s.ALLOWED_HTML_TAGS).strip()
|
||||
|
||||
if name is None or name == "":
|
||||
name = f"Compound {Compound.objects.filter(package=package).count() + 1}"
|
||||
|
||||
c.name = name
|
||||
|
||||
# We have a default here only set the value if it carries some payload
|
||||
if description is not None and description.strip() != "":
|
||||
c.description = nh3.clean(description, tags=s.ALLOWED_HTML_TAGS).strip()
|
||||
|
||||
c.save()
|
||||
|
||||
is_standardized = standardized_smiles == smiles
|
||||
|
||||
if not is_standardized:
|
||||
_ = CompoundStructure.create(
|
||||
c,
|
||||
standardized_smiles,
|
||||
name="Normalized structure of {}".format(name),
|
||||
description="{} (in its normalized form)".format(description),
|
||||
normalized_structure=True,
|
||||
)
|
||||
|
||||
cs = CompoundStructure.create(
|
||||
c, smiles, name=name, description=description, normalized_structure=is_standardized
|
||||
)
|
||||
|
||||
c.default_structure = cs
|
||||
c.save()
|
||||
|
||||
return c
|
||||
|
||||
|
||||
class PESStructure(CompoundStructure):
|
||||
pes_link = models.URLField(blank=False, null=False, verbose_name="PES Link")
|
||||
|
||||
def d3_json(self):
|
||||
return {
|
||||
"is_pes": True,
|
||||
"pes_link": self.pes_link,
|
||||
# Will overwrite image from Node
|
||||
"image": f"{reverse("depict_pes")}?pesLink={urllib.parse.quote(self.pes_link)}"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user