Skip to content

Commit

Permalink
added swappable fk for UserRating to Reting
Browse files Browse the repository at this point in the history
  • Loading branch information
OmegaDroid committed Oct 14, 2016
1 parent b577792 commit 24c4711
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 36 deletions.
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ hypothesis
pip-tools
selenium
mock
swapper
58 changes: 31 additions & 27 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,39 @@
#
# pip-compile requirements.in
#
beautifulsoup4==4.4.1 # via webtest
click==6.2 # via pip-tools
coverage==4.0.3
django-braces==1.8.1
django-model-utils==2.4
django-webtest==1.7.8
django==1.10
fancycompleter==0.4 # via pdbpp
beautifulsoup4==4.5.1 # via webtest
click==6.6 # via pip-tools
coverage==4.2
django-braces==1.9.0
django-model-utils==2.6
django-webtest==1.8.0
django==1.10.2
fancycompleter==0.5 # via pdbpp
first==2.0.1 # via pip-tools
flake8==2.5.1
hypothesis==1.16.0
mccabe==0.3.1 # via flake8
mock==1.3.0
model-mommy==1.2.6
flake8==3.0.4
hypothesis==3.5.3
mccabe==0.5.2 # via flake8
mock==2.0.0
model-mommy==1.3.0
ordereddict==1.1 # via pdbpp
path.py==8.1.2
pbr==1.8.1 # via mock
path.py==8.2.1
pbr==1.10.0 # via mock
pdbpp==0.8.3
pep8==1.5.7 # via flake8
pip-tools==1.3.0
pip-tools==1.7.0
py==1.4.31 # via pytest
pyflakes==1.0.0 # via flake8
pygments==2.0.2 # via pdbpp
pytest-cov==2.2.0
pytest-django==2.9.1
pytest==2.8.4
selenium==2.48.0
six==1.10.0 # via django-braces, mock, model-mommy, pip-tools, webtest
waitress==0.8.10 # via webtest
webob==1.5.1 # via webtest
webtest==2.0.20 # via django-webtest
pycodestyle==2.0.0 # via flake8
pyflakes==1.2.3 # via flake8
pygments==2.1.3 # via pdbpp
pytest-cov==2.4.0
pytest-django==3.0.0
pytest==3.0.3
selenium==3.0.0
six==1.10.0 # via mock, model-mommy, pip-tools, webtest
spark-parser==1.4.0 # via uncompyle6
swapper==1.0.0
uncompyle6==2.9.1 # via hypothesis
waitress==1.0.0 # via webtest
webob==1.6.1 # via webtest
webtest==2.0.23 # via django-webtest
wmctrl==0.3 # via pdbpp
xdis==3.0.2 # via uncompyle6
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def get_version(package):
install_requires=[
'django',
'django-model-utils',
'django-braces'
'django-braces',
'swapper',
],
classifiers=[
'Development Status :: 4 - Beta',
Expand Down
10 changes: 10 additions & 0 deletions star_ratings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
from __future__ import unicode_literals

import swapper

from .app_settings import Settings

__version__ = '0.5.3'

default_app_config = 'star_ratings.apps.StarRatingsAppConfig'
app_settings = Settings()


def get_star_ratings_rating_model_name():
return swapper.get_model_name('star_ratings', 'Rating')


def get_star_ratings_rating_model():
return swapper.load_model('star_ratings', 'Rating')
20 changes: 14 additions & 6 deletions star_ratings/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import swapper
from django.db import models, migrations
from decimal import Decimal
import model_utils.fields
import django.utils.timezone
from django.conf import settings
from django.db.migrations.migration import SwappableTuple

dependancies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('contenttypes', '0001_initial'),
]

class Migration(migrations.Migration):
swappable_dep = swapper.dependency('star_ratings', 'Rating')
if swappable_dep == migrations.swappable_dependency('star_ratings.Rating'):
dependancies.append(swappable_dep)

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('contenttypes', '0001_initial'),
]

class Migration(migrations.Migration):
dependencies = dependancies

operations = [
migrations.CreateModel(
Expand All @@ -27,6 +34,7 @@ class Migration(migrations.Migration):
('content_type', models.ForeignKey(blank=True, null=True, to='contenttypes.ContentType')),
],
options={
'swappable': swapper.swappable_setting('star_rating', 'Rating')
},
bases=(models.Model,),
),
Expand All @@ -38,7 +46,7 @@ class Migration(migrations.Migration):
('modified', model_utils.fields.AutoLastModifiedField(verbose_name='modified', editable=False, default=django.utils.timezone.now)),
('ip', models.GenericIPAddressField(blank=True, null=True)),
('score', models.PositiveSmallIntegerField()),
('rating', models.ForeignKey(related_name='user_ratings', to='star_ratings.Rating')),
('rating', models.ForeignKey(related_name='user_ratings', to=swapper.get_model_name('star_ratings', 'Rating'))),
('user', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
],
options={
Expand Down
7 changes: 5 additions & 2 deletions star_ratings/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import division, unicode_literals
from decimal import Decimal

import swapper
from warnings import warn
from django.conf import settings
from django.core.exceptions import ValidationError
Expand All @@ -11,7 +13,7 @@
from django.utils.translation import ugettext as _
from model_utils.models import TimeStampedModel

from . import app_settings
from . import app_settings, get_star_ratings_rating_model_name


def _clean_user(user):
Expand Down Expand Up @@ -70,6 +72,7 @@ class Rating(models.Model):

class Meta:
unique_together = ['content_type', 'object_id']
swappable = swapper.swappable_setting('star_ratings', 'Rating')

@property
def percentage(self):
Expand Down Expand Up @@ -122,7 +125,7 @@ class UserRating(TimeStampedModel):
user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True)
ip = models.GenericIPAddressField(blank=True, null=True)
score = models.PositiveSmallIntegerField()
rating = models.ForeignKey(Rating, related_name='user_ratings')
rating = models.ForeignKey(get_star_ratings_rating_model_name(), related_name='user_ratings')

objects = UserRatingManager()

Expand Down

0 comments on commit 24c4711

Please sign in to comment.