mtg-web/cards/views.py

23 lines
884 B
Python
Executable file

# Create your views here.
import re
from django.shortcuts import render_to_response
from django.template.loader import render_to_string
from mtgweb.analyzer.models import Card
from mtgweb.lib.mtg.mtg import Mana, ManaCost
import mtgweb.lib.mtg.database as database
def prettify_mana(text):
pattern = r'{%s}' % ManaCost.symbolPattern
while True:
match = re.search(pattern, text)
if not match:
break
text = text[:match.start()] + render_to_string('cards/manacost.html', {'cost': ManaCost(str(match.group(0)))}) + text[match.end():]
return text
def display(request, name):
card = Card.objects.get(name=name)
cost = ManaCost(str(card.cost))
abilities = [prettify_mana(ability) for ability in card.text.split("\n") if ability]
return render_to_response('cards/view.html', {'card': card, 'cost': cost, 'abilities': abilities})