# 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): manapattern = r'X?\d*(\((\d+|[{0}])/(\d+|[{0}])\))*[{0}]*'.format(''.join(Mana.types.keys())) pattern = r'{%s}' % manapattern 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})