Skip to content

Commit

Permalink
Merge pull request #44 from jardiacaj/html_format_#43
Browse files Browse the repository at this point in the history
Use format_html for HTML-generating functions #43
  • Loading branch information
jardiacaj committed Nov 30, 2017
2 parents 6fc4875 + 5e55ebe commit f1199b1
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .idea/finem_imperii.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 11 additions & 6 deletions battle/battle_renderer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from battle.models import Battle, BattleSoldierInTurn
from django.utils.html import conditional_escape

from battle.models import Battle
from finem_imperii.app_settings import BATTlE_TICKS_PER_TURN


Expand All @@ -11,13 +13,15 @@ def render_battle_for_view(battle: Battle):
for battle_organization in battle_side.battleorganization_set.all():
organizations[battle_organization.id] = {
'z': battle_side.z,
'name': battle_organization.organization.name,
'name': conditional_escape(
battle_organization.organization.name),
'color': battle_organization.organization.color,
}
for battle_character in battle_organization.battlecharacter_set.all():
characters[battle_character.id] = {
'organization_id': battle_organization.id,
'name': battle_character.character.name,
'name': conditional_escape(
battle_character.character.name),
'in_turn': {}
}
for battle_character_in_turn in battle_character.battlecharacterinturn_set.all():
Expand All @@ -27,8 +31,8 @@ def render_battle_for_view(battle: Battle):
units[battle_unit.id] = {
'character_id': battle_unit.owner_id,
'organization_id': battle_unit.battle_organization_id,
'name': battle_unit.name,
'type': battle_unit.type,
'name': conditional_escape(battle_unit.name),
'type': conditional_escape(battle_unit.type),
'in_turn': {}
}
for battle_unit_in_turn in battle_unit.battleunitinturn_set.all():
Expand All @@ -48,7 +52,8 @@ def render_battle_for_view(battle: Battle):
'x_pos': contubernium_in_turn.x_pos,
'z_pos': contubernium_in_turn.z_pos,
'ammo_remaining': contubernium_in_turn.ammo_remaining,
'attack_type': contubernium_in_turn.attack_type_this_turn,
'attack_type': conditional_escape(
contubernium_in_turn.attack_type_this_turn),
'attack_target':
contubernium_in_turn.contubernium_attacked_this_turn.battle_contubernium_id
if contubernium_in_turn.contubernium_attacked_this_turn
Expand Down
11 changes: 8 additions & 3 deletions character/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from django.urls import reverse
from django.utils import timezone
from django.utils.html import format_html
from django.utils.safestring import mark_safe

import organization.models.capability
import organization.models.organization
Expand Down Expand Up @@ -227,11 +229,14 @@ def get_html_name(self):
return result

def get_html_link(self):
return '<a href="{}">{}</a>'.format(
self.get_absolute_url(), self.get_html_name()
return format_html(
'<a href="{url}">{name}</a>',
url=mark_safe(self.get_absolute_url()),
name=mark_safe(self.get_html_name())
)

def total_carrying_capacity(self):
@staticmethod
def total_carrying_capacity():
return 100

def remaining_carrying_capacity(self):
Expand Down
2 changes: 1 addition & 1 deletion character/templates/character/travel_map_iframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<script>
$(function () {
var map = new MapRenderer({{ regions|safe }});
var map = new MapRenderer({{ world_data|safe }});
map.focus_to_region({{ request.hero.location.tile_id }});
map.enable_region_tags();
map.enable_settlement_tags();
Expand Down
2 changes: 1 addition & 1 deletion character/templates/character/view_character_iframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<script>
$(function () {
var map = new MapRenderer({{ regions|safe }});
var map = new MapRenderer({{ world_data|safe }});
map.focus_to_region({{ character.location.tile.id }});
map.highlight_settlement({{ character.location_id }});
map.enable_region_tags();
Expand Down
2 changes: 1 addition & 1 deletion character/views/travel.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def travel_view_iframe(request, settlement_id=None):
world = request.hero.world
context = {
'world': world,
'regions': render_world_for_view(world),
'world_data': render_world_for_view(world),
'target_settlement': target_settlement,
}
return render(request, 'character/travel_map_iframe.html', context)
2 changes: 1 addition & 1 deletion character/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def character_view_iframe(request, character_id):
)
context = {
'character': character,
'regions': render_world_for_view(character.world),
'world_data': render_world_for_view(character.world),
}
return render(request,
'character/view_character_iframe.html', context)
4 changes: 4 additions & 0 deletions messaging/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db import models
from django.urls.base import reverse
from django.utils.html import format_html


class ServerMOTD(models.Model):
Expand All @@ -11,6 +12,9 @@ class Meta:
display_order = models.SmallIntegerField()
draft = models.BooleanField(default=True)

def get_html_content(self):
return format_html(self.html_content)

def __str__(self):
return self.title

Expand Down
9 changes: 5 additions & 4 deletions organization/models/document.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db import models
from django.urls import reverse
from django.utils.html import format_html


class PolicyDocument(models.Model):
Expand All @@ -19,7 +20,7 @@ def get_absolute_url(self):
'organization:document', kwargs={'document_id': self.id})

def get_html_link(self):
return '<a href="{}">{}</a>'.format(
self.get_absolute_url(),
self.title,
)
return format_html('<a href="{url}">{name}</a>',
url=self.get_absolute_url(),
name=self.title,
)
27 changes: 15 additions & 12 deletions organization/models/organization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import models, transaction
from django.urls import reverse
from django.utils.html import escape
from django.utils.html import escape, format_html
from django.utils.safestring import mark_safe

import organization.models.capability
import organization.models.document
Expand Down Expand Up @@ -313,10 +314,11 @@ def get_html_name(self):
else:
suffix = ''

return template.format(
name=escape(self.name),
icon=icon,
suffix=suffix
return format_html(
template,
name=self.name,
icon=mark_safe(icon),
suffix=mark_safe(suffix)
)

def get_bootstrap_icon(self):
Expand All @@ -337,15 +339,16 @@ def get_bootstrap_icon(self):
icon = "triangle-top"
else:
icon = "option-vertical"
return template.format(
icon=icon,
color=escape(self.color),
)
return format_html(template,
icon=icon,
color=escape(self.color),
)

def get_html_link(self):
return '<a href="{}">{}</a>'.format(
self.get_absolute_url(),
self.get_html_name()
return format_html(
'<a href="{url}">{name}</a>',
url=mark_safe(self.get_absolute_url()),
name=mark_safe(self.get_html_name())
)

def current_elections_can_vote_in(self):
Expand Down
14 changes: 11 additions & 3 deletions organization/models/relationship.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models, transaction
from django.utils.html import format_html


class OrganizationRelationship(models.Model):
Expand Down Expand Up @@ -65,7 +66,10 @@ def _format_relationship(relationship, relationship_name):
)

def get_relationship_html(self):
return OrganizationRelationship._format_relationship(self.relationship, self.get_relationship_display())
return OrganizationRelationship._format_relationship(
self.relationship,
self.get_relationship_display()
)

def get_desired_relationship_html(self):
if self.desired_relationship is None:
Expand Down Expand Up @@ -122,7 +126,10 @@ def default_military_stance(self):
return MilitaryStance.AVOID_BATTLE

def __str__(self):
return "Relationship {} to {}".format(self.from_organization, self.to_organization)
return "Relationship {} to {}".format(
self.from_organization,
self.to_organization
)


class MilitaryStance(models.Model):
Expand Down Expand Up @@ -172,7 +179,8 @@ def get_html_stance(self):
else:
bootstrap_type = 'default'

return '<span class="label label-{bootstrap_type}">{stance}</span>'.format(
return format_html(
'<span class="label label-{bootstrap_type}">{stance}</span>',
bootstrap_type=bootstrap_type,
stance=stance
)
29 changes: 16 additions & 13 deletions unit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from django.db.models import Avg, F
from django.urls import reverse
from django.utils.html import format_html

import character.models
import world.models
from battle.models import BattleUnit


Expand Down Expand Up @@ -79,10 +79,14 @@ class WorldUnit(models.Model):
(5, "flanking far right"),
)

owner_character = models.ForeignKey('character.Character', blank=True, null=True)
owner_character = models.ForeignKey(
'character.Character',
blank=True, null=True)
world = models.ForeignKey('world.World', blank=True, null=True)
location = models.ForeignKey('world.Settlement', blank=True, null=True)
origin = models.ForeignKey('world.Settlement', related_name='units_originating')
origin = models.ForeignKey(
'world.Settlement',
related_name='units_originating')
name = models.CharField(max_length=100)
recruitment_type = models.CharField(
max_length=30, choices=RECRUITMENT_CHOICES
Expand Down Expand Up @@ -127,27 +131,26 @@ def __str__(self):
return self.name

def get_short_html_descriptor(self):
return '{} ' \
'<span class="unit-icon-{}" aria-hidden="true"></span>'.format(
return format_html(
'{} <span class="unit-icon-{}" aria-hidden="true"></span>',
self.soldier.count(),
self.type
)

def get_long_html_descriptor(self):
return '{} ' \
'<span class="unit-icon-{}" aria-hidden="true"></span> ' \
'{}'.format(
return format_html(
'{} <span class="unit-icon-{}" aria-hidden="true"></span> {}',
self.soldier.count(),
self.type,
self.name
)

def get_html_link(self):
return '{} <a href="{}">{}</a>'.format(
self.get_short_html_descriptor(),
self.get_absolute_url(),
self.name
)
return format_html('{descriptor} <a href="{url}">{name}</a>',
descriptor=self.get_short_html_descriptor(),
url=self.get_absolute_url(),
name=self.name
)

def get_absolute_url(self):
return reverse('unit:unit', kwargs={'unit_id': self.id})
Expand Down
16 changes: 9 additions & 7 deletions world/models/geography.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from django.db import models
from django.urls import reverse
from django.utils.html import format_html

import organization.models.organization
import unit.models
Expand Down Expand Up @@ -69,10 +70,10 @@ def get_current_date(self):
return turn_to_date(self.current_turn)

def get_html_link(self):
return '<a href="{}">{}</a>'.format(
self.get_absolute_url(),
self.name
)
return format_html('<a href="{url}">{name}</a>',
url=self.get_absolute_url(),
name=self.name
)

def get_absolute_url(self):
return reverse('world:world', kwargs={'world_id': self.id})
Expand Down Expand Up @@ -126,9 +127,10 @@ def get_absolute_url(self):
return reverse('world:tile', kwargs={'tile_id': self.id})

def get_html_link(self):
return '<a href="{url}">{name}</a>'.format(
url=self.get_absolute_url(), name=self.name
)
return format_html('<a href="{url}">{name}</a>',
url=self.get_absolute_url(),
name=self.name
)

def get_absolute_coords(self):
return Point(x=self.x_pos, z=self.z_pos)
Expand Down
Loading

0 comments on commit f1199b1

Please sign in to comment.