Currently serving freshly prepared alpacas.
This commit is contained in:
0
alpaca_viewer/__init__.py
Normal file
0
alpaca_viewer/__init__.py
Normal file
4
alpaca_viewer/admin.py
Normal file
4
alpaca_viewer/admin.py
Normal 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
20
alpaca_viewer/models.py
Normal 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
|
||||
11
alpaca_viewer/static/admin/css/style.css
Normal file
11
alpaca_viewer/static/admin/css/style.css
Normal 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%;
|
||||
}
|
||||
BIN
alpaca_viewer/static/admin/img/little_triangles.png
Normal file
BIN
alpaca_viewer/static/admin/img/little_triangles.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 241 B |
16
alpaca_viewer/templates/viewer.html
Normal file
16
alpaca_viewer/templates/viewer.html
Normal 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
16
alpaca_viewer/tests.py
Normal 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
16
alpaca_viewer/views.py
Normal 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})
|
||||
Reference in New Issue
Block a user