diff --git a/analyzer/models.py b/analyzer/models.py index 36178a4..3388fd5 100755 --- a/analyzer/models.py +++ b/analyzer/models.py @@ -24,4 +24,12 @@ class Card(models.Model, mtg.Card): return self.name class Deck(models.Model): name = models.CharField(max_length=80) - cards = models.ManyToManyField(Card) + cards = models.ManyToManyField(Card, through='Included') + def __unicode__(self): + return self.name +class Included(models.Model): + card = models.ForeignKey(Card) + deck = models.ForeignKey(Deck) + count = models.IntegerField(default=0) + def __unicode__(self): + return '{0}x {1}'.format(self.count, self.card) diff --git a/analyzer/templatetags/__init__.py b/analyzer/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/analyzer/templatetags/get_range.py b/analyzer/templatetags/get_range.py new file mode 100644 index 0000000..cc9dfd2 --- /dev/null +++ b/analyzer/templatetags/get_range.py @@ -0,0 +1,24 @@ +from django.template import Library + +register = Library() + +@register.filter +def get_range( value ): + """ + Filter - returns a list containing range made from given value + Usage (in template): + + + + Results with the HTML: + + + Instead of 3 one may use the variable set in the views + """ + return range( value if type(value) == int else 0 ) diff --git a/decks/views.py b/decks/views.py index b22fe02..ccb5133 100755 --- a/decks/views.py +++ b/decks/views.py @@ -1,10 +1,13 @@ +from django.http import Http404 from django.shortcuts import render_to_response -import mtgweb.lib.mtg.mtg as mtg -import mtgweb.lib.mtg.database as database +from mtgweb.analyzer.models import Deck def index(request): - db = database.TextDB('mtgweb/lib/mtg/db.txt') - deck = mtg.Deck() - print db.getCard('Forest') - deck.load('mtgweb/lib/mtg/decks/WhiteBeast.txt', db) - return render_to_response('decks/index.html', {'deck': deck}) + decks = Deck.objects.all() + return render_to_response('decks/index.html', {'decks': decks}) +def detail(request, id): + try: + deck = Deck.objects.get(pk=id) + except Deck.DoesNotExist: + raise Http404 + return render_to_response('decks/detail.html', {'deck': deck}) diff --git a/importutil.py b/importutil.py index 831e2e0..3b3a813 100644 --- a/importutil.py +++ b/importutil.py @@ -1,7 +1,15 @@ -from mtgweb.lib.mtg import database -from mtgweb.analyzer.models import Card, CardType, Attribute +import os +from mtgweb.lib.mtg import mtg, database +from mtgweb.analyzer.models import Card, CardType, Attribute, Deck, Included -def doimport(): +class LookupDB(database.Database): + def getCard(self, name): + return Card.objects.get(name=name) + def findCard(self, name): + for card in Card.objects.filter(name__icontains=name): + yield card + +def dbimport(): db = database.TextDB('lib/mtg/db.txt') for c in db.findCard(): print c @@ -21,5 +29,22 @@ def doimport(): except: attr = Attribute.objects.create(name=a) card.attributes.add(attr) +def deckimport(): + db = LookupDB() + for filename in [n for n in os.listdir('lib/mtg/decks') if n.endswith('.txt')]: + d = mtg.Deck() + d.load('lib/mtg/decks/' + filename, db) + name = filename.split('.')[0] + print "Importing deck '{0}'".format(name) + deck = Deck(name=name) + deck.save() + for card in d: + try: + c = Included.objects.get(deck=deck, card=card) + except: + c = Included(deck=deck, card=card) + c.count += 1 + c.save() -doimport() +#dbimport() +deckimport() diff --git a/templates/analyzer/card_list.html b/templates/analyzer/card_list.html new file mode 100644 index 0000000..27ca93a --- /dev/null +++ b/templates/analyzer/card_list.html @@ -0,0 +1,9 @@ + +{% block content %} +

Cards

+ +{% endblock %} diff --git a/templates/cards/manacost.html b/templates/cards/manacost.html new file mode 100644 index 0000000..69ad3a1 --- /dev/null +++ b/templates/cards/manacost.html @@ -0,0 +1,10 @@ +{% load get_range %} + +{% if cost.any > 0 or cost.converted == 0 %} + {{ cost.any }} +{% endif %} +{% for type, mana in cost.mana.mana.items %} + {% for i in mana|get_range %} + + {% endfor %} +{% endfor %} diff --git a/templates/cards/view.html b/templates/cards/view.html index 1456bb1..315ee3e 100755 --- a/templates/cards/view.html +++ b/templates/cards/view.html @@ -2,16 +2,29 @@ Card: {{ card.name }}