Repository: OpenEnergyPlatform/oeplatform

Issue:
Adding new Tags raises error if metadata is empty 
## Description of the issue

If a tag is added to a table without metadata, omi triggers an error. 

## Steps to Reproduce
1. Open table detail page
2. Add Tag and submit
3. Tag is added but the page is redirectet to interal server error page.

## Ideas of solution

1. Check if metadata on table in available, otherwise skip adding tags to metadata keywords.

## Context and Environment
* Version used: 
* Operating system: 
* Environment setup and (python) version: 

## Workflow checklist
- [ ] I am aware of the workflow in [CONTRIBUTING.md](https://github.com/OpenEnergyPlatform/oeplatform/blob/develop/CONTRIBUTING.md)



Discussion:

Resolving patch:
diff --git a/api/actions.py b/api/actions.py
--- a/api/actions.py
+++ b/api/actions.py
@@ -136,6 +136,16 @@ def assert_add_tag_permission(user, table, permission, schema):
         raise PermissionDenied
 
 
+def assert_has_metadata(table, schema):
+    table = DBTable.load(schema, table)
+    if table.oemetadata is None:
+        result = False
+    else:
+        result = True
+
+    return result
+
+
 def _translate_fetched_cell(cell):
     if isinstance(cell, geoalchemy2.WKBElement):
         return _get_engine().execute(cell.ST_AsText()).scalar()
diff --git a/dataedit/views.py b/dataedit/views.py
--- a/dataedit/views.py
+++ b/dataedit/views.py
@@ -24,11 +24,12 @@
 from django.utils.encoding import smart_str
 from django.views.generic import View
 from metadata.v160.schema import OEMETADATA_V160_SCHEMA
+from metadata.v160.template import OEMETADATA_V160_TEMPLATE
 from sqlalchemy.dialects.postgresql import array_agg
 from sqlalchemy.orm import sessionmaker
 
 import api.parser
-from api.actions import describe_columns
+from api.actions import assert_has_metadata, describe_columns
 
 try:
     import oeplatform.securitysettings as sec
@@ -1571,13 +1572,18 @@ def update_table_tags(request):
         int(field[len("tag_") :]) for field in request.POST if field.startswith("tag_")
     }
 
-    # update tags in db and harmonize metadata
-    metadata = get_tag_keywords_synchronized_metadata(
-        table=table, schema=schema, tag_ids_new=ids
-    )
-
     with _get_engine().connect() as con:
         with con.begin():
+            if not actions.assert_has_metadata(table=table, schema=schema):
+                actions.set_table_metadata(
+                    table=table, schema=schema, metadata=OEMETADATA_V160_TEMPLATE, cursor=con
+                )
+                # update tags in db and harmonize metadata
+                
+            metadata = get_tag_keywords_synchronized_metadata(
+                table=table, schema=schema, tag_ids_new=ids
+            )
+
             # TODO Add metadata to table (JSONB field) somewhere here
             actions.set_table_metadata(
                 table=table, schema=schema, metadata=metadata, cursor=con
@@ -1587,6 +1593,7 @@ def update_table_tags(request):
         request,
         'Please note that OEMetadata keywords and table tags are synchronized. When submitting new tags, you may notice automatic changes to the table tags on the OEP and/or the "Keywords" field in the metadata.',  # noqa
     )
+
     return render(
         request,
         "dataedit/dataview.html",


Test patch: