Skip to content

Commit fecf547

Browse files
committed
Merge pull request #30 from texastribune/uuid-pks-n-stuffing
Uuid pks n stuffing
2 parents c6a3419 + ad68b8b commit fecf547

File tree

12 files changed

+90
-29
lines changed

12 files changed

+90
-29
lines changed

MANIFEST.in

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
include setup.py README.* MANIFEST.in LICENSE
2-
recursive-include django_object_actions *
3-
recursive-include example_project *
2+
recursive-include django_object_actions/templates *
43
global-exclude *.pyc

Makefile

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1+
VERSION=0.5.0
12
PROJECT=./example_project
23
MANAGE=$(PROJECT)/manage.py
34

45
help:
56
@echo "make commands:"
6-
@echo " make help - this help"
7-
@echo " make clean - remove files generated by distutils"
8-
@echo " make test - run test suite"
9-
@echo " make coverage - run coverage"
10-
@echo " make resetdb - delete and recreate the sqlite database"
7+
@echo " make help - this help"
118
@echo " make quickstart - setup a dev environment the first time"
9+
@echo " make clean - remove generated files"
10+
@echo " make test - run test suite"
11+
@echo " make coverage - get coverage report"
12+
@echo " make resetdb - delete and recreate the sqlite database"
13+
@echo " make release - publish a release to PyPI"
1214

15+
# just a demo of how to get up and running quickly
16+
quickstart: resetdb
17+
python $(MANAGE) createsuperuser
18+
python $(MANAGE) runserver
1319

1420
clean:
1521
rm -rf .coverage
@@ -19,7 +25,6 @@ clean:
1925
rm -rf dist
2026
rm -rf *.egg-info
2127

22-
2328
test:
2429
python -W ignore::RuntimeWarning $(MANAGE) test django_object_actions
2530

@@ -35,7 +40,17 @@ resetdb:
3540
python $(MANAGE) migrate --noinput
3641
python $(MANAGE) loaddata sample_data
3742

38-
# just a demo of how to get up and running quickly
39-
quickstart: resetdb
40-
python $(MANAGE) createsuperuser
41-
python $(MANAGE) runserver
43+
# Release Instructions:
44+
#
45+
# 1. bump version number above
46+
# 2. `make release`
47+
# 3. git push origin master --tags
48+
#
49+
# If this doesn't work, make sure you have wheels installed:
50+
# pip install wheel
51+
release:
52+
@sed -i -r /version/s/[0-9.]+/$(VERSION)/ setup.py
53+
@sed -i -r /version/s/[0-9.]+/$(VERSION)/ django_object_actions/__init__.py
54+
@git commit -am "bump version to v$(VERSION)"
55+
@git tag v$(VERSION)
56+
python setup.py sdist bdist_wheel upload

django_object_actions/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" A Django app for adding object tools to models """
1+
"""A Django app for adding object tools for models in the admin."""
22
__version__ = '0.5.0'
33

44

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Integration tests that actually try and use the tools setup in admin.py
3+
"""
4+
from __future__ import unicode_literals
5+
6+
from .tests import LoggedInTestCase
7+
from example_project.polls.factories import CommentFactory
8+
9+
10+
class CommentTest(LoggedInTestCase):
11+
def test_action_on_a_model_with_uuid_pk_works(self):
12+
comment = CommentFactory()
13+
url = '/admin/polls/comment/{0}/tools/hodor/'.format(comment.pk)
14+
# sanity check that url has a uuid
15+
self.assertIn('-', url)
16+
response = self.client.get(url)
17+
self.assertEqual(response.status_code, 302)

django_object_actions/tests/tests.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.test import TestCase
22

3+
from example_project.polls.factories import UserFactory
34
from example_project.polls.models import Choice
4-
from ..factories import UserFactory
55

66

77
class LoggedInTestCase(TestCase):
@@ -12,6 +12,8 @@ def setUp(self):
1212
self.assertTrue(self.client.login(username='admin', password='admin'))
1313

1414

15+
# TODO move most of these to test_admin.py after I sit down and re-read these
16+
# and don't need the fixtures
1517
class AppTests(LoggedInTestCase):
1618
fixtures = ['sample_data']
1719

django_object_actions/utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
from __future__ import unicode_literals
2+
13
from functools import wraps
24

3-
from django.conf.urls import patterns
5+
from django.conf.urls import patterns, url
46
from django.contrib import messages
57
from django.db.models.query import QuerySet
68
from django.http import Http404, HttpResponse, HttpResponseRedirect
@@ -19,8 +21,10 @@ def get_tool_urls(self):
1921
for tool in self.objectactions:
2022
tools[tool] = getattr(self, tool)
2123
my_urls = patterns('',
22-
(r'^(?P<pk>\d+)/tools/(?P<tool>\w+)/$', self.admin_site.admin_view(
23-
ModelToolsView.as_view(model=self.model, tools=tools)))
24+
# supports pks that are numbers or uuids
25+
url(r'^(?P<pk>[0-9a-f\-]+)/tools/(?P<tool>\w+)/$',
26+
self.admin_site.admin_view(
27+
ModelToolsView.as_view(model=self.model, tools=tools)))
2428
)
2529
return my_urls
2630

example_project/polls/admin.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import unicode_literals
2+
13
from django.contrib import admin
24
from django.core.urlresolvers import reverse
35
from django.db.models import F
@@ -6,7 +8,7 @@
68
from django_object_actions import (DjangoObjectActions,
79
takes_instance_or_queryset)
810

9-
from .models import Choice, Poll
11+
from .models import Choice, Poll, Comment
1012

1113

1214
class ChoiceAdmin(DjangoObjectActions, admin.ModelAdmin):
@@ -51,7 +53,7 @@ class ChoiceInline(admin.StackedInline):
5153

5254
class PollAdmin(DjangoObjectActions, admin.ModelAdmin):
5355
fieldsets = [
54-
(None, {'fields': ['question']}),
56+
(None, {'fields': ['question']}),
5557
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
5658
]
5759
inlines = [ChoiceInline]
@@ -74,6 +76,15 @@ def delete_all_choices(self, request, obj):
7476
delete_all_choices.label = "Delete All Choices"
7577

7678
objectactions = ('delete_all_choices', )
79+
admin.site.register(Poll, PollAdmin)
7780

7881

79-
admin.site.register(Poll, PollAdmin)
82+
class CommentAdmin(DjangoObjectActions, admin.ModelAdmin):
83+
def hodor(self, request, obj):
84+
if not obj.comment:
85+
# bail because we need a comment
86+
return
87+
obj.comment = ' '.join(['hodor' for x in obj.comment.split()])
88+
obj.save()
89+
objectactions = ('hodor', )
90+
admin.site.register(Comment, CommentAdmin)

django_object_actions/factories.py renamed to example_project/polls/factories.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import factory
99

10+
from . import models
11+
1012

1113
class UserFactory(factory.DjangoModelFactory):
1214
FACTORY_FOR = get_user_model()
@@ -17,3 +19,7 @@ class UserFactory(factory.DjangoModelFactory):
1719
email = factory.LazyAttribute(lambda x: '{0}@{1}.com'.format(
1820
x.first_name.lower(), x.last_name.lower()))
1921
password = factory.PostGenerationMethodCall('set_password', 'password')
22+
23+
24+
class CommentFactory(factory.DjangoModelFactory):
25+
FACTORY_FOR = models.Comment

example_project/polls/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from django.db import models
44
from django.utils import timezone
5+
from django_extensions.db.fields import UUIDField
56

67

78
class Poll(models.Model):
@@ -25,3 +26,11 @@ class Choice(models.Model):
2526

2627
def __unicode__(self):
2728
return self.choice_text
29+
30+
31+
class Comment(models.Model):
32+
uuid = UUIDField(primary_key=True)
33+
comment = models.TextField(null=True, blank=True)
34+
35+
def __unicode__(self):
36+
return self.comment or ''

requirements.txt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
Django==1.7
2-
1+
Django==1.7.1
32
dj-database-url==0.3.0
4-
# for heroku support
5-
psycopg2==2.5.4
6-
7-
django-extensions==1.3.11
8-
tox==1.7.2
3+
django-extensions==1.4.6
4+
tox==1.8.1
95
factory-boy==2.4.1
106
coverage==3.7.1
7+
8+
# for heroku support
9+
psycopg2==2.5.4

0 commit comments

Comments
 (0)