From 477394887e6dfd19ae7e8e0f6d6e5718fdda681e Mon Sep 17 00:00:00 2001 From: Patrick Huck Date: Wed, 10 Apr 2024 18:18:55 -0700 Subject: [PATCH] linting, increase description limit --- .../mpcontribs/api/projects/document.py | 111 ++++++++++++------ .../mpcontribs/client/__init__.py | 2 +- 2 files changed, 74 insertions(+), 39 deletions(-) diff --git a/mpcontribs-api/mpcontribs/api/projects/document.py b/mpcontribs-api/mpcontribs/api/projects/document.py index f9e5d84a4..868574723 100644 --- a/mpcontribs-api/mpcontribs/api/projects/document.py +++ b/mpcontribs-api/mpcontribs/api/projects/document.py @@ -17,8 +17,16 @@ from mongoengine import EmbeddedDocument, signals from mongoengine.queryset.manager import queryset_manager from mongoengine.fields import ( - StringField, BooleanField, DictField, URLField, EmailField, DecimalField, - FloatField, IntField, EmbeddedDocumentListField, EmbeddedDocumentField + StringField, + BooleanField, + DictField, + URLField, + EmailField, + DecimalField, + FloatField, + IntField, + EmbeddedDocumentListField, + EmbeddedDocumentField, ) from mpcontribs.api import send_email, valid_key, valid_dict, delimiter, enter @@ -28,6 +36,7 @@ def visit(path, key, value): from mpcontribs.api.contributions.document import quantity_keys + # pull out units if isinstance(value, dict) and "unit" in value: return key, value["unit"] @@ -47,9 +56,7 @@ def validate(self, value): provider, email = value.split(":", 1) if provider not in PROVIDERS: - self.error( - "{} {}".format(self.error_msg % value, "(invalid provider)") - ) + self.error("{} {}".format(self.error_msg % value, "(invalid provider)")) super().validate(email) @@ -139,12 +146,12 @@ class Projects(Document): ) authors = StringField( required=True, - help_text="comma-separated list of authors" + help_text="comma-separated list of authors", # TODO change to EmbeddedDocumentListField ) description = StringField( min_length=5, - max_length=1500, + max_length=2000, required=True, help_text="brief description of the project", ) @@ -156,8 +163,10 @@ class Projects(Document): help_text="list of references", ) license = StringField( - choices=["CCA4", "CCPD"], default="CCA4", required=True, - help_text="license (see https://materialsproject.org/about/terms)" + choices=["CCA4", "CCPD"], + default="CCA4", + required=True, + help_text="license (see https://materialsproject.org/about/terms)", ) other = DictField(validation=valid_dict, null=True, help_text="other information") owner = ProviderEmailField( @@ -197,8 +206,12 @@ def post_save(cls, sender, document, **kwargs): ts = current_app.config["USTS"] email_project = [document.owner, document.name] token = ts.dumps(email_project) - link = url_for("projects.applications", token=token, _scheme=scheme, _external=True) - url = url_for("projectsFetch", pk=document.name, _scheme=scheme, _external=True) + link = url_for( + "projects.applications", token=token, _scheme=scheme, _external=True + ) + url = url_for( + "projectsFetch", pk=document.name, _scheme=scheme, _external=True + ) url += "?_fields=_all" html = render_template("admin_email.html", url=url, link=link) send_email(admin_email, f'New project "{document.name}"', html) @@ -214,15 +227,20 @@ def post_save(cls, sender, document, **kwargs): approved=True, admin_email=admin_email, host=portal, - project=document.name + project=document.name, ) owner_email = document.owner.split(":", 1)[1] send_email(owner_email, subject, html) - if "columns" in delta_set or "columns" in delta_unset or ( - not delta_set and not delta_unset + if ( + "columns" in delta_set + or "columns" in delta_unset + or (not delta_set and not delta_unset) ): - from mpcontribs.api.contributions.document import Contributions, COMPONENTS + from mpcontribs.api.contributions.document import ( + Contributions, + COMPONENTS, + ) columns = {} ncontribs = Contributions.objects(project=document.id).count() @@ -237,11 +255,13 @@ def post_save(cls, sender, document, **kwargs): pipeline = [ {"$match": {"project": document.id}}, {"$sample": {"size": 1000}}, - {"$project": {"data": 1}} + {"$project": {"data": 1}}, ] result = Contributions.objects.aggregate(pipeline) merged = ChainMap(*result) - flat = flatten(remap(merged, visit=visit, enter=enter), reducer="dot") + flat = flatten( + remap(merged, visit=visit, enter=enter), reducer="dot" + ) for k, v in flat.items(): if k.startswith("data."): @@ -256,23 +276,27 @@ def post_save(cls, sender, document, **kwargs): # NOTE also includes dynamic document fields for component in COMPONENTS.keys(): pipeline.append( - {"$lookup": { - "from": component, - "localField": component, - "foreignField": "_id", - "as": component - }} + { + "$lookup": { + "from": component, + "localField": component, + "foreignField": "_id", + "as": component, + } + } ) # document size and attachment content size project_stage = { "_id": 0, "size": {"$bsonSize": "$$ROOT"}, - "contents": {"$map": { # attachment sizes - "input": "$attachments", - "as": "attm", - "in": {"$toInt": "$$attm.content"} - }} + "contents": { + "$map": { # attachment sizes + "input": "$attachments", + "as": "attm", + "in": {"$toInt": "$$attm.content"}, + } + }, } # number of components @@ -280,14 +304,18 @@ def post_save(cls, sender, document, **kwargs): project_stage[component] = {"$size": f"${component}"} # filter/forward number columns - min_max_paths = [path for path, col in columns.items() if col["unit"] != "NaN"] + min_max_paths = [ + path for path, col in columns.items() if col["unit"] != "NaN" + ] for path in min_max_paths: field = f"{path}{delimiter}value" - project_stage[field] = {"$cond": { - "if": {"$isNumber": f"${field}"}, - "then": f"${field}", - "else": "$$REMOVE" - }} + project_stage[field] = { + "$cond": { + "if": {"$isNumber": f"${field}"}, + "then": f"${field}", + "else": "$$REMOVE", + } + } # add project stage to pipeline pipeline.append({"$project": project_stage}) @@ -298,7 +326,10 @@ def post_save(cls, sender, document, **kwargs): pipeline.append({"$project": project_stage_2}) # total size and total number of components - group_stage = {"_id": None, "size": {"$sum": {"$add": ["$size", "$contents"]}}} + group_stage = { + "_id": None, + "size": {"$sum": {"$add": ["$size", "$contents"]}}, + } for component in COMPONENTS.keys(): group_stage[component] = {"$sum": f"${component}"} @@ -340,14 +371,18 @@ def post_delete(cls, sender, document, **kwargs): admin_email = current_app.config["MAIL_DEFAULT_SENDER"] subject = f'Your project "{document.name}" has been deleted' html = render_template( - "owner_email.html", approved=False, - admin_email=admin_email, project=document.name + "owner_email.html", + approved=False, + admin_email=admin_email, + project=document.name, ) owner_email = document.owner.split(":", 1)[1] send_email(owner_email, subject, html) -register_field(ProviderEmailField, ProviderEmail, available_params=(params.LengthParam,)) +register_field( + ProviderEmailField, ProviderEmail, available_params=(params.LengthParam,) +) signals.post_save.connect(Projects.post_save, sender=Projects) signals.post_delete.connect(Projects.post_delete, sender=Projects) Projects.atlas.index._set_indexed_fields({"type": "document", "dynamic": True}) diff --git a/mpcontribs-client/mpcontribs/client/__init__.py b/mpcontribs-client/mpcontribs/client/__init__.py index 8a78fbb0e..70d895f9a 100644 --- a/mpcontribs-client/mpcontribs/client/__init__.py +++ b/mpcontribs-client/mpcontribs/client/__init__.py @@ -1174,7 +1174,7 @@ def create_project( name (str): unique name matching `^[a-zA-Z0-9_]{3,31}$` title (str): unique title with 5-30 characters authors (str): comma-separated list of authors - description (str): brief description (max 1500 characters) + description (str): brief description (max 2000 characters) url (str): URL for primary reference (paper/website/...) """ queries = [{"name": name}, {"title": title}]