Now importing decks, displaying them, plus cards display mana symbols for their cost!

This commit is contained in:
Correl Roush 2010-09-17 17:28:46 -04:00
parent 0e6f05bac5
commit 8e1bba2ceb
11 changed files with 132 additions and 24 deletions

View file

@ -24,4 +24,12 @@ class Card(models.Model, mtg.Card):
return self.name return self.name
class Deck(models.Model): class Deck(models.Model):
name = models.CharField(max_length=80) 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)

View file

View file

@ -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):
<ul>{% for i in 3|get_range %}
<li>{{ i }}. Do something</li>
{% endfor %}</ul>
Results with the HTML:
<ul>
<li>0. Do something</li>
<li>1. Do something</li>
<li>2. Do something</li>
</ul>
Instead of 3 one may use the variable set in the views
"""
return range( value if type(value) == int else 0 )

View file

@ -1,10 +1,13 @@
from django.http import Http404
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
import mtgweb.lib.mtg.mtg as mtg from mtgweb.analyzer.models import Deck
import mtgweb.lib.mtg.database as database
def index(request): def index(request):
db = database.TextDB('mtgweb/lib/mtg/db.txt') decks = Deck.objects.all()
deck = mtg.Deck() return render_to_response('decks/index.html', {'decks': decks})
print db.getCard('Forest') def detail(request, id):
deck.load('mtgweb/lib/mtg/decks/WhiteBeast.txt', db) try:
return render_to_response('decks/index.html', {'deck': deck}) deck = Deck.objects.get(pk=id)
except Deck.DoesNotExist:
raise Http404
return render_to_response('decks/detail.html', {'deck': deck})

View file

@ -1,7 +1,15 @@
from mtgweb.lib.mtg import database import os
from mtgweb.analyzer.models import Card, CardType, Attribute 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') db = database.TextDB('lib/mtg/db.txt')
for c in db.findCard(): for c in db.findCard():
print c print c
@ -21,5 +29,22 @@ def doimport():
except: except:
attr = Attribute.objects.create(name=a) attr = Attribute.objects.create(name=a)
card.attributes.add(attr) 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()

View file

@ -0,0 +1,9 @@
{% block content %}
<h2>Cards</h2>
<ul>
{% for card in object_list %}
<li>{{ card.name }}</li>
{% endfor %}
</ul>
{% endblock %}

View file

@ -0,0 +1,10 @@
{% load get_range %}
{% if cost.any > 0 or cost.converted == 0 %}
<span class="mana">{{ cost.any }}</span>
{% endif %}
{% for type, mana in cost.mana.mana.items %}
{% for i in mana|get_range %}
<span class="mana {{ type }}"></span>
{% endfor %}
{% endfor %}

View file

@ -2,16 +2,29 @@
<head> <head>
<title>Card: {{ card.name }}</title> <title>Card: {{ card.name }}</title>
<style type="text/css"> <style type="text/css">
.mana {
height: 1em;
width: 1em;
border-radius: 0.5em;
-moz-border-radius: 5px;
display: block;
float: left;
color: #eee;
background-color: gray;
border: 1px solid black;
margin-right: 2px;
text-align: center;
}
.card { .card {
border: 1px solid gray; border: 1px solid gray;
background-color: #999; background-color: #999;
padding: 3px; padding: 3px;
} }
.card.R { background-color: red; } .mana.R, .card.R { background-color: red; }
.card.G { background-color: green; } .mana.G, .card.G { background-color: green; }
.card.U { background-color: blue; } .mana.U, .card.U { background-color: blue; }
.card.B { background-color: black; } .mana.B, .card.B { background-color: black; }
.card.W { background-color: white; } .mana.W, .card.W { background-color: white; }
.card .title, .card .title,
.card .info { .card .info {
border: 1px solid gray; border: 1px solid gray;
@ -51,7 +64,13 @@
<div class="card {{ card.colors|join:" " }}"> <div class="card {{ card.colors|join:" " }}">
<div class="title"> <div class="title">
<span class="name">{{ card.name|title }}</span> <span class="name">{{ card.name|title }}</span>
<span class="cost">{% if card.cost %}{{ card.cost }}{% endif %}</span> <span class="cost">
{% if card.cost %}
{% with card.cost as cost %}
{% include 'cards/manacost.html' %}
{% endwith %}
{% endif %}
</span>
</div> </div>
<div class="picture"></div> <div class="picture"></div>
<div class="info"> <div class="info">

6
templates/decks/detail.html Executable file
View file

@ -0,0 +1,6 @@
<h2>{{deck}}</h2>
<ul>
{% for card in deck.included_set.all %}
<li>{{card.count}}x <a href="{% url mtgweb.cards.views.display name=card.card.name %}">{{ card.card }}</a></li>
{% endfor %}
</ul>

View file

@ -1,6 +1,6 @@
<html> <h2>Decks</h2>
<body> <ul>
<pre>foobar {% for deck in decks %}
</pre> <li><a href="{% url mtgweb.decks.views.detail deck.id %}">{{ deck }}</a></li>
</body> {% endfor %}
</html> </ul>

View file

@ -1,4 +1,6 @@
from django.conf.urls.defaults import * from django.conf.urls.defaults import *
from django.views.generic import list_detail
from mtgweb.analyzer.models import Card
# Uncomment the next two lines to enable the admin: # Uncomment the next two lines to enable the admin:
from django.contrib import admin from django.contrib import admin
@ -7,8 +9,10 @@ admin.autodiscover()
urlpatterns = patterns('', urlpatterns = patterns('',
# Example: # Example:
# (r'^mtgweb/', include('mtgweb.foo.urls')), # (r'^mtgweb/', include('mtgweb.foo.urls')),
(r'^cards/$', list_detail.object_list, {'queryset': Card.objects.all(), 'paginate_by': 20}),
(r'^cards/(?P<name>.*?)/$', 'mtgweb.cards.views.display'), (r'^cards/(?P<name>.*?)/$', 'mtgweb.cards.views.display'),
(r'^decks/$', 'mtgweb.decks.views.index'), (r'^decks/$', 'mtgweb.decks.views.index'),
(r'^decks/(?P<id>\d+)/$', 'mtgweb.decks.views.detail'),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs' # Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation: # to INSTALLED_APPS to enable admin documentation: