Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update models #70

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2017-03-26 18:57
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('london_cafes', '0001_initial'),
]

operations = [
migrations.RenameField(
model_name='cafe',
old_name='atmosphere',
new_name='atmosphere_rating',
),
migrations.RenameField(
model_name='cafe',
old_name='coffee',
new_name='coffee_rating',
),
migrations.RenameField(
model_name='cafe',
old_name='wifi',
new_name='wifi_rating',
),
]
19 changes: 19 additions & 0 deletions london_cafes/london_cafes/migrations/0003_removed_created_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2017-03-26 19:21
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('london_cafes', '0002_renamed_ratings_fields'),
]

operations = [
migrations.RemoveField(
model_name='cafe',
name='created_at',
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2017-03-26 19:54
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('london_cafes', '0003_removed_created_at'),
]

operations = [
migrations.RemoveField(
model_name='cafe',
name='address',
),
migrations.AddField(
model_name='cafe',
name='address1',
field=models.CharField(default='address1', max_length=200),
preserve_default=False,
),
migrations.AddField(
model_name='cafe',
name='address2',
field=models.CharField(default='address2', max_length=200),
preserve_default=False,
),
migrations.AddField(
model_name='cafe',
name='address3',
field=models.CharField(default='address3', max_length=200),
preserve_default=False,
),
migrations.AddField(
model_name='cafe',
name='city',
field=models.CharField(default='city', max_length=200),
preserve_default=False,
),
migrations.AddField(
model_name='cafe',
name='post_code',
field=models.CharField(default='postcode', max_length=30),
preserve_default=False,
),
migrations.DeleteModel(
name='Address',
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2017-03-29 18:40
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('london_cafes', '0004_moved_address_fields_to_cafe'),
]

operations = [
migrations.CreateModel(
name='Ratings',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('rating', models.CharField(choices=[(1, b'Not Good'), (2, b'OK'), (3, b'Good'), (4, b'Very Good'), (5, b'Excellent')], default=5, max_length=30)),
],
),
migrations.AlterField(
model_name='cafe',
name='atmosphere_rating',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='atmosphere_rating', to='london_cafes.Ratings'),
),
migrations.AlterField(
model_name='cafe',
name='coffee_rating',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='coffee_rating', to='london_cafes.Ratings'),
),
migrations.AlterField(
model_name='cafe',
name='wifi_rating',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='wifi_rating', to='london_cafes.Ratings'),
),
migrations.DeleteModel(
name='Atmosphere',
),
migrations.DeleteModel(
name='Coffee',
),
migrations.DeleteModel(
name='Wifi',
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2017-03-29 18:52
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('london_cafes', '0005_removed_atmosphere_wifi_coffee_models'),
]

operations = [
migrations.AddField(
model_name='cafe',
name='deleted',
field=models.BooleanField(default=False),
),
]
64 changes: 32 additions & 32 deletions london_cafes/london_cafes/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
from django.db import models
from django.utils import timezone

#class CafeQuerySet(models.QuerySet):
class CafeManager(models.Manager):

def atmosphere_exact_rating(self, rating):
return self.filter(atmosphere_rating__rating=rating)

def coffee_exact_rating(self, rating):
return self.filter(coffee_rating__rating=rating)

def wifi_exact_rating(self, rating):
return self.filter(wifi_rating__rating=rating)

def atmosphere_gte_rating(self, rating):
return self.filter(atmosphere_rating__rating__gte=rating)

def coffee_gte_rating(self, rating):
return self.filter(coffee_rating__rating__gte=rating)

def wifi_gte_rating(self, rating):
return self.filter(wifi_rating__rating__gte=rating)

RATING_OPTIONS = (
(1, 'Not Good'),
Expand All @@ -9,43 +29,23 @@
(5, 'Excellent'),
)


class Address(models.Model):
street = models.TextField()
postcode = models.TextField()
city = models.TextField()

class Meta:
verbose_name_plural = 'addresses'


class Ratings(models.Model):
rating = models.CharField(
max_length=30,
choices=RATING_OPTIONS,
default=5)

class Meta:
abstract = True


class Atmosphere(Ratings):
pass


class Coffee(Ratings):
pass


class Wifi(Ratings):
pass


class Cafe(models.Model):
created_at = models.DateTimeField(default=timezone.now, editable=False)
name = models.CharField(max_length=200)
deleted = models.BooleanField(default=False)
description = models.CharField(max_length=200)
address = models.ForeignKey(Address)
atmosphere = models.ForeignKey(Atmosphere)
coffee = models.ForeignKey(Coffee)
wifi = models.ForeignKey(Wifi)
address1 = models.CharField(max_length=200)
address2 = models.CharField(max_length=200)
address3 = models.CharField(max_length=200)
city = models.CharField(max_length=200)
post_code = models.CharField(max_length=30)
atmosphere_rating = models.ForeignKey(Ratings, related_name="atmosphere_rating")
coffee_rating = models.ForeignKey(Ratings, related_name="coffee_rating")
wifi_rating = models.ForeignKey(Ratings, related_name="wifi_rating")

objects = CafeManager()
42 changes: 42 additions & 0 deletions london_cafes/london_cafes/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from django.test import TestCase
from .models import Ratings, Cafe

class CafeManagerTestCase(TestCase):

def setUp(self):
rating3 = Ratings.objects.create(id=1, rating=3)
rating5 = Ratings.objects.create(id=2, rating=5)
rating1 = Ratings.objects.create(id=3, rating=1)
Cafe.objects.create(id=1, name="cafe1", description="desc1", address1="a1", address2="a2", address3="a3", city="city1", post_code="A1 ABC", atmosphere_rating=rating5, coffee_rating=rating3, wifi_rating=rating3)
Cafe.objects.create(id=2, name="cafe2", description="desc2", address1="a12", address2="a2", address3="a3", city="city1", post_code="A1 ABC", atmosphere_rating=rating5, coffee_rating=rating3, wifi_rating=rating1)
Cafe.objects.create(id=3, name="cafe3", description="desc3", address1="a13", address2="a2", address3="a3", city="city1", post_code="A1 ABC", atmosphere_rating=rating1, coffee_rating=rating1, wifi_rating=rating1)

def test_atmosphere_exact_ratings_filter(self):
cafes_expected = Cafe.objects.filter(id__in=[1,2])
cafes = Cafe.objects.atmosphere_exact_rating(5)
self.assertItemsEqual(cafes, cafes_expected)

def test_coffee_exact_ratings_filter(self):
cafes_expected = Cafe.objects.filter(id__in=[1,2])
cafes = Cafe.objects.coffee_exact_rating(3)
self.assertItemsEqual(cafes, cafes_expected)

def test_wifi_exact_ratings_filter(self):
cafes_expected = Cafe.objects.filter(id__in=[2,3])
cafes = Cafe.objects.wifi_exact_rating(1)
self.assertItemsEqual(cafes, cafes_expected)

def test_atmosphere_gte_ratings_filter(self):
cafes_expected = Cafe.objects.filter(id__in=[1,2])
cafes = Cafe.objects.atmosphere_gte_rating(5)
self.assertItemsEqual(cafes, cafes_expected)

def test_coffee_gte_ratings_filter(self):
cafes_expected = Cafe.objects.filter(id__in=[1,2])
cafes = Cafe.objects.coffee_gte_rating(3)
self.assertItemsEqual(cafes, cafes_expected)

def test_wifi_gte_ratings_filter(self):
cafes_expected = Cafe.objects.filter(id__in=[1])
cafes = Cafe.objects.wifi_gte_rating(2)
self.assertItemsEqual(cafes, cafes_expected)