mtg-web/cards/views.py

24 lines
884 B
Python
Raw Normal View History

2010-08-26 03:43:19 +00:00
# Create your views here.
2010-09-22 00:14:10 +00:00
import re
2010-08-26 03:43:19 +00:00
from django.shortcuts import render_to_response
2010-09-22 00:14:10 +00:00
from django.template.loader import render_to_string
2010-09-17 21:44:54 +00:00
from mtgweb.analyzer.models import Card
2010-09-22 00:14:10 +00:00
from mtgweb.lib.mtg.mtg import Mana, ManaCost
2010-08-26 03:43:19 +00:00
import mtgweb.lib.mtg.database as database
2010-09-22 00:14:10 +00:00
def prettify_mana(text):
pattern = r'{%s}' % ManaCost.symbolPattern
2010-09-22 00:14:10 +00:00
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
2010-08-26 03:43:19 +00:00
def display(request, name):
2010-09-17 21:44:54 +00:00
card = Card.objects.get(name=name)
cost = ManaCost(str(card.cost))
2010-09-22 00:14:10 +00:00
abilities = [prettify_mana(ability) for ability in card.text.split("\n") if ability]
2010-09-17 21:44:54 +00:00
return render_to_response('cards/view.html', {'card': card, 'cost': cost, 'abilities': abilities})