Skip to content

Commit

Permalink
Merge branch 'master' into pyup-update-simplejson-3.10.0-to-3.11.1
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmior authored Jun 28, 2017
2 parents 0663225 + c1e10a2 commit cdcc6f7
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 16 deletions.
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: python
python:
- "2.6"
- "2.7"
services:
- postgresql
env:
global:
- DJANGO_DEBUG=1
- UPLOADCARE_DISABLED=1
- ROLLBAR_DISABLED=1
- DJANGO_SECRET_KEY=foo
- DATABASE_URL=postgres://[email protected]/travis_ci_test
- HOSTNAMES=localhost
install:
- pip install -r requirements.txt
before_script:
- psql -c 'CREATE DATABASE travis_ci_test;' -U postgres
script:
- python manage.py test pylinks
9 changes: 5 additions & 4 deletions pylinks/links/migrations/0007_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ def forwards(self, orm):
))
db.create_unique(m2m_table_name, ['link_id', 'category_id'])

# Copy existing categories
for link in orm.Link.objects.all():
link.categories.add(link.category)
link.save()
if not db.dry_run:
# Copy existing categories
for link in orm.Link.objects.all():
link.categories.add(link.category)
link.save()


def backwards(self, orm):
Expand Down
13 changes: 11 additions & 2 deletions pylinks/links/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import os

from django.db import models
from django.core.urlresolvers import reverse
from pylinks.main.models import DatedModel
from pylinks.links.utils import LinkFileField

if not os.environ.get('UPLOADCARE_DISABLED', False):
from pylinks.links.utils import LinkFileField
else:
from django.db.models import FileField as LinkFileField


class Category(DatedModel):
Expand All @@ -27,7 +33,7 @@ class Link(DatedModel):
help_text='URL to link to. Leave blank if uploading a file.')
file = LinkFileField(blank=True, null=True, default=None, max_length=500,
help_text='A file to be uploaded and linked to instead ' \
+ 'of the URL.')
+ 'of the URL.', upload_to='uploads/')
categories = models.ManyToManyField(Category, related_name='links')
description = models.TextField(null=True,
help_text='Description of the link or file contents')
Expand All @@ -47,6 +53,9 @@ def get_absolute_url(self):
def __unicode__(self):
return self.title

class Meta:
ordering = ('title',)

def save(self, *args, **kwargs):
if self.file:
self.url = self.file.cdn_url
Expand Down
80 changes: 80 additions & 0 deletions pylinks/links/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from django.test import Client, TestCase
from django.core.urlresolvers import reverse

from .models import Category, Link

class CategoryModelTests(TestCase):

def test_category_sort(self):
Category(title='Test 2', slug='test2').save()
Category(title='Test 1', slug='test1').save()

self.assertEqual(['Test 1', 'Test 2'], map(str, Category.objects.all()))

class LinkModelTests(TestCase):
def setUp(self):
self.url = 'https://github.com/'
self.link = Link(title='GitHub', url=self.url)

def test_track_link(self):
self.assertEqual(self.link.get_absolute_url(), self.url)
self.link.save()
self.assertEqual(self.link.visits, 0)
self.assertEqual(self.link.get_absolute_url(),
reverse('track_link', kwargs={'link_id': self.link.id}))

def test_link_title(self):
self.assertEqual(str(self.link), 'GitHub')

def test_increment_visits(self):
self.link.save()
response = self.client.get(
reverse('track_link', kwargs={'link_id': self.link.id}))
self.assertEqual(response.status_code, 302)
self.assertEqual(response['Location'], self.link.url)
self.assertEqual(Link.objects.get(pk=self.link.id).visits, 1)

class ListViewTests(TestCase):
def setUp(self):
ca = Category(title='A', slug='a')
ca.save()
cb = Category(title='B', slug='b')
cb.save()

link = Link(title='Google', url='https://www.google.ca', visits=1)
link.save()
ca.links.add(link)

link = Link(title='GitHub', url='https://github.com', visits=2)
link.save()
ca.links.add(link)

link = Link(title='UW', url='https://uwaterloo.ca', visits=3)
link.save()
cb.links.add(link)

class CategoryListViewTests(ListViewTests):
def test_category_list(self):
response = self.client.get(reverse('category_links', kwargs={'slug': 'a'}))
self.assertQuerysetEqual(response.context['links'], [
'<Link: GitHub>',
'<Link: Google>'
])

class PopularListViewTests(ListViewTests):
def test_popular_list(self):
response = self.client.get(reverse('popular_links'))
self.assertQuerysetEqual(response.context['links'], [
'<Link: UW>',
'<Link: GitHub>',
'<Link: Google>'
])

class RecentListViewTests(ListViewTests):
def test_recent_list(self):
response = self.client.get(reverse('recent_links'))
self.assertQuerysetEqual(response.context['links'], [
'<Link: UW>',
'<Link: GitHub>',
'<Link: Google>'
])
4 changes: 4 additions & 0 deletions pylinks/links/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def __repr__(self):

# Patch FileField to return LinkFile instances
class LinkFileField(FileField):
def __init__(self, *args, **kwargs):
kwargs.pop('upload_to', None)
super(LinkFileField, self).__init__(*args, **kwargs)

def to_python(self, value):
if value is None or value == '':
return value
Expand Down
2 changes: 1 addition & 1 deletion pylinks/links/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class PopularListView(LinkListView):
template_name = 'links/popular.htm'

def get_queryset(self):
return Link.objects.all().order_by('-visits')
return Link.objects.all().order_by('-visits', 'title')


def track_link(request, link_id):
Expand Down
21 changes: 12 additions & 9 deletions pylinks/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@
'django.middleware.transaction.TransactionMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
'rollbar.contrib.django.middleware.RollbarNotifierMiddleware',
'django_user_agents.middleware.UserAgentMiddleware',
)

if not os.environ.get('ROLLBAR_DISABLED', False):
MIDDLEWARE_CLASSES.append('rollbar.contrib.django.middleware.RollbarNotifierMiddleware')

ROOT_URLCONF = 'pylinks.urls'

# Python dotted path to the WSGI application used by Django's runserver.
Expand All @@ -109,7 +110,7 @@
# Don't forget to use absolute paths, not relative paths.
)

INSTALLED_APPS = (
INSTALLED_APPS = [
'pylinks.main',
'pylinks.links',
'pylinks.feeds',
Expand All @@ -135,9 +136,10 @@
'storages',
's3_folder_storage',
'google_analytics',
'pyuploadcare.dj',
'django_user_agents',
)
]

if not os.environ.get('UPLOADCARE_DISABLED', False):
INSTALLED_APPS.append('pyuploadcare.dj')

CSP_DEFAULT_SRC = ("'self'",)
CSP_REPORTS_EMAIL_ADMINS = False
Expand Down Expand Up @@ -210,11 +212,12 @@
'environment': 'development' if DEBUG else 'production',
'branch': 'master',
'root': os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
}
} if not os.environ.get('ROLLBAR_DISABLED', False) else {}

UPLOADCARE = {
'pub_key': os.environ['UPLOADCARE_PUB_KEY'],
'secret': os.environ['UPLOADCARE_SECRET']
}

GRAPPELLI_INDEX_DASHBOARD = 'pylinks.dashboard.CustomIndexDashboard'
} if not os.environ.get('UPLOADCARE_DISABLED', False) else {}

GRAPPELLI_INDEX_DASHBOARD = 'pylinks.dashboard.CustomIndexDashboard'

0 comments on commit cdcc6f7

Please sign in to comment.