Currently serving freshly prepared alpacas.

This commit is contained in:
2012-11-19 22:05:25 -05:00
commit b9ccd2319d
14 changed files with 301 additions and 0 deletions

View File

4
alpaca_viewer/admin.py Normal file
View File

@@ -0,0 +1,4 @@
from alpaca_viewer.models import Alpaca
from django.contrib import admin
admin.site.register(Alpaca)

20
alpaca_viewer/models.py Normal file
View File

@@ -0,0 +1,20 @@
from django.db import models
class Alpaca(models.Model):
"""
Holds a url to an alpaca image for user's viewing pleasure.
Not currently storing images on-site because of storage space concerns. As much
as I like alpacas, I don't need my server completely flooded with them.
"""
url = models.URLField()
alt = models.TextField('Alt Text', blank=True)
def __unicode__(self):
if self.alt and len(self.alt) > 100:
return (self.alt[:100] + '...')
elif self.alt:
return self.alt
else:
return self.url

View File

@@ -0,0 +1,11 @@
body {
background: url(/static/admin/img/little_triangles.png) repeat;
}
div.alpaca-image {
margin: auto;
text-align: center;
}
img {
max-width: 100%;
max-height: 95%;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 241 B

View File

@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Alpaca</title>
<link href="/static/admin/css/style.css" rel="stylesheet" />
</head>
<body>
<div class="alpaca-image">
{% if alpaca %}
<a href="{% url viewer %}"><img src="{{ alpaca.url }}" {% if alpaca.alt %}alt="{{ alpaca.alt }}" {% endif %}/></a>
{% else %}
No alpaca :(
{% endif %}
</div>
</body>
<html>

16
alpaca_viewer/tests.py Normal file
View File

@@ -0,0 +1,16 @@
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)

16
alpaca_viewer/views.py Normal file
View File

@@ -0,0 +1,16 @@
from django.shortcuts import render_to_response
from alpaca_viewer.models import Alpaca
from random import randint
def viewer(request):
"""Displays a random alpaca"""
# Note: I'm not using Alpaca.objects.order_by('?')[0] because it's been known
# to be slow on some databases (MySQL) with a large dataset, so I'm playing
# it safe and just accessing a random index from .all()
alpaca = None
size = Alpaca.objects.count()
if size > 0:
i = randint(0, size-1)
alpaca = Alpaca.objects.all()[i]
return render_to_response('viewer.html', {'alpaca': alpaca})