Skip to content

Commit

Permalink
Refactored and improved templates
Browse files Browse the repository at this point in the history
  • Loading branch information
tholzheim committed Apr 27, 2021
1 parent 9dc2874 commit dad7da8
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 75 deletions.
4 changes: 3 additions & 1 deletion templates/category_page.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ __NOTOC__
== Properties ==
{{ SMW.render_as_list(map(Property.get_page_link, Property.get_entity_properties(topic.name, properties))) }}
</div>
</div>
</div>

{{ SMW.parser_function("set", oneliner=oneliner, **{"Has property description":topic.__dict__.get("documentation")+"@en"}) }}
5 changes: 1 addition & 4 deletions templates/concept_page.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ Input:
properties: all properties of the wiki.
Output: Generates the page Concept:<topic.name>
#}
{#---------------------------------Import-------------------------------------#}
{% from 'macros/utils.jinja' import render_entity %}
{% from 'macros/form.jinja' import forminput %}
{#-------------------------------Template-------------------------------------#}
__NOCACHE__
{{ topic.render_entity(oneliner=False) }}
Expand All @@ -29,4 +26,4 @@ __NOCACHE__
{# Add all topics that are related to this topic as list of page links -#}
{{ SMW.render_as_list(map(Topic.get_page_link, topic.get_related_topics(properties))) }}
{{ '}}' }}
{# [[:Category:{{ topic.name }}]] #}
[[:Category:{{ topic.name }}]]
5 changes: 1 addition & 4 deletions templates/form_page.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
Generate the form of page for a entity.
Input:
topic: topic entity
properties: all properties of the wiki.
regexps: List of a regular expressions
Output: Generates the page Category:<entity_name>
#}
{#----------------------------Global Variables--------------------------------#}
{% set topic_properties = Property.get_entity_properties(topic.name, properties) %}
{#-------------------------------Template-------------------------------------#}
<noinclude>
{{ Form.forminput(form=topic.name, button_text='add ' + topic.name) }}
Expand All @@ -19,7 +16,7 @@ Output: Generates the page Category:<entity_name>
{# Form control buttons #}
{{ Form.standard_input_tag(["save","preview","changes","cancel"]) }}
{{ Form.page_form_function("for template", **{topic.name:True }) }}
{{ Form.form_table(topic.name, topic_properties) }}
{{ Form.form_table(topic.name, topic.properties) }}
{{ Form.page_form_function("end template") }}
{# Form control buttons -#}
{{ Form.standard_input_tag(["save","preview","changes","cancel"]) }}
Expand Down
11 changes: 4 additions & 7 deletions templates/list_of_page.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@ Input:
properties: all properties of the wiki.
Output: Generates the page List of<entity_plural_name>
#}
{#---------------------------------Import-------------------------------------#}
{% from 'macros/utils.jinja' import render_entity %}
{% from 'macros/form.jinja' import forminput %}
{#-------------------------------Template-------------------------------------#}
__NOCACHE__
{{ topic.query_topic_overview() }}

== {{ topic.plural_name }} ==
== {{ topic.pluralName }} ==
{{ Form.forminput(form=topic.name, button_text='add ' + topic.name) }}
{{ topic.query_number_entities() }} {{ topic.plural_name }}:
{{ topic.query_number_entities() }} {{ topic.pluralName }}:
{{ topic.query_entities() }}
== See also ==
{{ SMWPart.getAllAsPageLink(topic) }}
<div class="toccolours mw-collapsible mw-collapsed" style="width:1024px">
=== Example rendered with the corresponding Template ===
<div class="mw-collapsible-content">
{{ topic.query_entities(format="template", limit=1, template=topic.name) }}
{{ topic.query_entities(format="template", limit=0, template=topic.name, searchlabel="Show examples") }}
</div>
</div>
{# [[:Category:{{ topic.name }}]] #}
[[:Category:{{ topic.name }}]]
10 changes: 4 additions & 6 deletions templates/template_page.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ Input:
properties: all properties of the wiki.
Output: Generates the page Template:<topic.name>
#}
{#----------------------------Global Variables--------------------------------#}
{% set topic_properties = Property.get_entity_properties(topic.name, properties) %}
{#-------------------------------Template-------------------------------------#}
<noinclude>
This is the template {{ '{{' }}Link|target=Template:{{ topic.name }}|title={{ '}}' }}.
Expand All @@ -17,16 +15,16 @@ This is the template {{ '{{' }}Link|target=Template:{{ topic.name }}|title={{ '}

== Usage ==
<pre>
{{ SMW.render_sample_entity_with_properties(topic, topic_properties, oneliner=False) }}
{{ SMW.render_sample_entity_with_properties(topic, topic.properties, oneliner=False) }}
</pre>
{{ SMW.render_sample_entity_with_properties(topic, topic_properties, oneliner=False) }}
{{ SMW.render_sample_entity_with_properties(topic, topic.properties, oneliner=False) }}

[[Category:Template]]
</noinclude>
<includeonly>
{{ SMW.set_entity_parameter(topic, topic_properties, oneliner=False) }}
{{ SMW.set_entity_parameter(topic, topic.properties, oneliner=False) }}

{{ Template.table_of_arguments(topic, topic_properties) }}
{{ Template.table_of_arguments(topic, topic.properties) }}

[[Category:{{ topic.name }}]]{{ SMW.parser_function("default_form",presence_is_true=True, **{topic.name:True}) }}
</includeonly>
6 changes: 3 additions & 3 deletions templates/uml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ note as {{ topic.name }}Note
{{ topic.documentation }}
end note
class {{ topic.name }} {
{% for property in Property.get_primitive_properties(topic.name,properties) -%}
{% for property in Property.get_primitive_properties(topic.name,topic.properties) -%}
{{ property.type.replace("Special:Types/","") }}: {{ property.label }}
{% endfor -%}
}
{{ topic.name }}Note .. {{ topic.name }}
{# Set outgoing edges #}
{%- for edge in UML.get_outgoing_edges(topic.name,properties) %}
{%- for edge in UML.get_outgoing_edges(topic.name,topic.properties) %}
"{{ edge.source }}" "{{ edge.source_cardinality }}" --> "{{ edge.target_cardinality }}" "{{ edge.target }}" : "{{ edge.property.name }}"
{%- endfor -%}
{# Set incoming edges #}
{% for edge in UML.get_incoming_edges(topic.name,properties) %}
{% for edge in UML.get_incoming_edges(topic.name,topic.properties) %}
"{{ edge.target }}" "{{ edge.target_cardinality }}" <-- "{{ edge.source_cardinality }}" "{{ edge.source }}" : "{{ edge.property.name }}"
{%- endfor %}

Expand Down
42 changes: 21 additions & 21 deletions tests/test_metamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ def test_get_related_topics(self):

def test_get_page_link(self):
topic = Topic({"name": "Topic"})
self.assertEqual("[[Concept:Topic]]", topic.get_page_link())
self.assertEqual("[[Concept:Topic|Topic]]", topic.get_page_link())
topic.__dict__.update(pageTitle="Concept:Topic")
self.assertEqual("[[Concept:Topic]]", topic.get_page_link())
self.assertEqual("[[Concept:Topic|Topic]]", topic.get_page_link())

def test_render_entity(self):
data = {"name": "Topic",
Expand Down Expand Up @@ -327,25 +327,25 @@ def test_query_examples(self):

def test_query_entity_properties(self):
topic = Topic({"name":"Test Title"})
expected_query = """{{#ask:[[Concept:Property]][[Used for::Concept:Test Title]]
|?name=name
|?label=label
|?type=type
|?index=index
|?sortPos=sortPos
|?primaryKey=primary key
|?mandatory=mandatory
|?namespace=namespace
|?size=size
|?uploadable=uploadable
|?defaultValue=default
|?inputType=inputType
|?allowedValues=allowedValues
|?documentation=documentation
|?values_from=values from
|?showInGrid=showInGrid
|?isLink=isLink
|?nullable=allow nulls?
expected_query = """{{#ask:[[Concept:Property]][[Property topic::Concept:Test Title]]
|?Property name=name
|?Property label=label
|?Property type=type
|?Property index=index
|?Property sortPos=sortPos
|?Property primaryKey=primary key
|?Property mandatory=mandatory
|?Property namespace=namespace
|?Property size=size
|?Property uploadable=uploadable
|?Property defaultValue=default
|?Property inputType=inputType
|?Property allowedValues=allowedValues
|?Property documentation=documentation
|?Property values_from=values from
|?Property showInGrid=showInGrid
|?Property isLink=isLink
|?Property nullable=allow nulls?
|format=wikitable
}}"""
actual_query = topic.query_entity_properties()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_smw.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_field(self):
self.assertEqual(test_field, Form.field(property))

def test_form_table(self):
exp_form_table = """{| class="formtable InfoBox mw-collapsible" style="width:100%"
exp_form_table = """{| class="wikitable mw-collapsible" style="width:100%"
|-
|colspan="2" |Basic Information
|-
Expand Down
67 changes: 51 additions & 16 deletions wikifile/metamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,28 @@ def get_prop_list_from_samples(samples: list):
prop_list.append(key)
return prop_list

def get_pageTitle(self, prefix:str, withNamespace=True):
"""
Returns page name of this metamodel element. If pageTitle is not defined, it is assumed that name corresponds to the pageTitle.
Args:
prefix: MetaModelElement prefix e.g. properties usually have the prefix Property
withNamespace: If true also the namespace of this property is returned. Otherwise, without namespace.
Returns:
Returns page name of this metamodel element.
"""
if self.pageTitle:
if withNamespace:
return self.pageTitle
else:
return self.pageTitle.replace(f"{prefix}:", "")
elif self.name:
if withNamespace:
return f"{prefix}:{self.name}"
else:
return self.name
else:
return None


class Context(MetaModelElement):
"""
Expand Down Expand Up @@ -159,6 +181,16 @@ def __init__(self, topic_properties: dict, properties: list=None):
super(Topic, self).__init__(Topic.propList, properties=topic_properties)
self.properties = properties

def get_pageTitle(self, withNamespace=True):
"""
Returns page name of this topic. If pageTitle is not defined, it is assumed that name corresponds to the pageTitle.
Args:
withNamespace: If true also the namespace of this property is returned. Otherwise, without namespace.
Returns:
Returns page name of this topic.
"""
return super(Topic, self).get_pageTitle("Concept", withNamespace)


def get_related_topics(self, properties):
"""
Expand Down Expand Up @@ -187,12 +219,15 @@ def render_entity(self, oneliner=True):
res += SMW.render_parameters(oneliner, **MetaModelElement.get_parameters(self.__dict__, self.propList))
return res + "}}"

def get_page_link(self):
def get_page_link(self, withLabel=True, withDescription=False):
"""Returns a link to this page"""
if self.pageTitle:
return f"[[{self.pageTitle}]]"
pageTitle = self.get_pageTitle()
if withDescription:
pageTitle += "::@@@"
if withLabel:
return f"[[{pageTitle}|{self.name}]]"
else:
return f"[[Concept:{self.name}]]"
return f"[[Concept:{pageTitle}]]"

@classmethod
def from_wiki_json(cls, topic_json, prop_json=None):
Expand Down Expand Up @@ -242,7 +277,7 @@ def query_entity_properties(self, format="wikitable", oneliner=False):
"""Returns a SMW query querying all properties of this entity"""
query = Query(format=format)
query.select("Concept:Property")
query.select("Used for::Concept:" + self.name)
query.select("Property topic::Concept:" + self.name)
query.printout_list_of_properties(Property.get_property_properties())
return query.render(oneliner=oneliner)

Expand Down Expand Up @@ -315,27 +350,27 @@ def is_used_for(self):
res.append(self.usedFor)
return res

def get_pageName(self, withNamespace=True):
def get_pageTitle(self, withNamespace=True):
"""
Returns page name of this property. If pageTitle is not defined, it is assumed that name corresponds to the pageTitle.
Args:
withNamespace: If true also the namespace of this property is returned. Otherwise, without namespace.
Returns:
Returns page name of this property.
"""
if self.pageTitle:
return self.pageTitle if withNamespace else self.pageTitle.replace("Property:", "")
elif self.name:
# ToDo if the properties from meta model are taken they do not have a page title name current assumption is that property.name is then the pageTitle (without namespace)
return f"Property:{self.name}" if withNamespace else self.name
else:
return None
return super(Property, self).get_pageTitle(self.__class__.__name__, withNamespace)


@staticmethod
def get_property_properties():
"""Returns the properties that describe properties"""
properties_json = json.loads(pkgutil.get_data("wikifile", "/resources/metamodel/Property_properties.json"))
return [Property(x) for x in properties_json]
properties = [Property(x) for x in properties_json]
# Add pageTitle
for property in properties:
property.__dict__["pageTitle"] = f"Property:Property {property.name}"
print([p.pageTitle for p in properties])
return properties

@staticmethod
def get_entity_properties(entity_name: str, properties: list):
Expand Down Expand Up @@ -401,7 +436,7 @@ def get_page_link(self, name=None):
SMW page link to this property
"""
if name is None:
name = self.get_pageName(withNamespace=True)
name = self.get_pageTitle(withNamespace=True)
if self.label:
return f"[[{name}|{self.label}]]"
else:
Expand All @@ -415,7 +450,7 @@ def get_description_page_link(self):
Returns:
SMW page link to this property
"""
name = f"{self.get_pageName(withNamespace=False)}::@@@"
name = f"{self.get_pageTitle(withNamespace=False)}::@@@"
return self.get_page_link(name)


Expand Down
Loading

0 comments on commit dad7da8

Please sign in to comment.