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

Django HW #9

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@ docs/_build/
target/

data/
.DS_Store
.direnv/
.envrc
convert_ml_1m_data.py
ml-1m/
movieratings/.DS_Store
movieratings/.idea/
movieratings/fixtures/
movieratings/db.sqlite3
.idea/
11 changes: 10 additions & 1 deletion movieratings/movieratings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'movies',
)

MIDDLEWARE_CLASSES = (
Expand Down Expand Up @@ -75,9 +76,15 @@
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
'default': {
'sqlite3': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'movies',
'USER': 'manish',
'HOST': '127.0.0.1',
}
}

Expand All @@ -100,3 +107,5 @@
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'

LOGIN_REDIRECT_URL = '/index'
12 changes: 12 additions & 0 deletions movieratings/movieratings/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@
"""
from django.conf.urls import include, url
from django.contrib import admin
from movies import views

urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url('^', include('django.contrib.auth.urls')),
url('^index$', views.allmovies, name='index'),
url(r'^topmovies$', views.topmovies, name='topmovies'),
url(r'^allmovies$', views.allmovies, name='allmovies'),
url(r'^allraters$', views.allraters, name='allraters'),
url(r'^movie/(?P<movie_id>\d+)$', views.movie, name='movie'),
url(r'^rater/(?P<rater_id>\d+)$', views.rater, name='rater'),
url(r'^register$', views.user_register, name='user_register'),
url(r'^rate/(?P<movie_id>\d*)$', views.rate_movie, name='rate_movie'),
url(r'^edit_rating/(?P<movie_id>\d*)$', views.edit_rating, name='edit_rating'),
url(r'^delete_rating/(?P<rating_id>\d*)$', views.delete_rating, name='delete_rating'),
]
Empty file added movieratings/movies/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions movieratings/movies/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.contrib import admin
from .models import Rater, Movie, Rating, Genre

# Register your models here.

class MovieAdmin(admin.ModelAdmin):
fields = ['title', 'genres']
list_display = ['title', 'average_rating']

class GenreAdmin(admin.ModelAdmin):
fields = ['genre']



admin.site.register(Rater)
admin.site.register(Movie, MovieAdmin)
admin.site.register(Rating)
admin.site.register(Genre, GenreAdmin)
36 changes: 36 additions & 0 deletions movieratings/movies/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django import forms
from django.contrib.auth.models import User
from .models import Rating


class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())

class Meta:
model = User
fields = ('username', 'email', 'password',)


class RatingForm(forms.ModelForm):
RATING_CHOICES = ((1, 1), (2, 2), (3, 3), (4, 4), (5, 5))

rating = forms.ChoiceField(choices=RATING_CHOICES,
label="Rating",
required=True)
class Meta:
model = Rating
fields = ('rating',)

class EditForm(forms.ModelForm):
RATING_CHOICES = ((1, 1), (2, 2), (3, 3), (4, 4), (5, 5))

rating = forms.ChoiceField(choices=RATING_CHOICES,
label="Rating",
required=True)


class Meta:
model = Rating
fields = ('rating',)


32 changes: 32 additions & 0 deletions movieratings/movies/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
]

operations = [
migrations.CreateModel(
name='Movies',
fields=[
('id', models.AutoField(primary_key=True, verbose_name='ID', auto_created=True, serialize=False)),
],
),
migrations.CreateModel(
name='Rater',
fields=[
('id', models.AutoField(primary_key=True, verbose_name='ID', auto_created=True, serialize=False)),
('userid', models.IntegerField()),
],
),
migrations.CreateModel(
name='Ratings',
fields=[
('id', models.AutoField(primary_key=True, verbose_name='ID', auto_created=True, serialize=False)),
],
),
]
65 changes: 65 additions & 0 deletions movieratings/movies/migrations/0002_auto_20150615_1921.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings


class Migration(migrations.Migration):

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

operations = [
migrations.CreateModel(
name='Movie',
fields=[
('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)),
('title', models.CharField(max_length=255)),
('genre', models.CharField(max_length=255)),
],
),
migrations.CreateModel(
name='Rating',
fields=[
('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)),
('rating', models.IntegerField()),
('timestamp', models.DateTimeField()),
('movie', models.ForeignKey(to='movies.Movie')),
],
),
migrations.DeleteModel(
name='Movies',
),
migrations.DeleteModel(
name='Ratings',
),
migrations.RenameField(
model_name='rater',
old_name='userid',
new_name='age',
),
migrations.AddField(
model_name='rater',
name='user',
field=models.OneToOneField(null=True, to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='rater',
name='zip_code',
field=models.CharField(max_length=10, default=10000),
preserve_default=False,
),
migrations.AddField(
model_name='rating',
name='rater',
field=models.ForeignKey(to='movies.Rater'),
),
migrations.AddField(
model_name='movie',
name='raters',
field=models.ManyToManyField(through='movies.Rating', to='movies.Rater'),
),
]
23 changes: 23 additions & 0 deletions movieratings/movies/migrations/0003_auto_20150615_1941.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('movies', '0002_auto_20150615_1921'),
]

operations = [
migrations.RenameField(
model_name='movie',
old_name='genre',
new_name='genres',
),
migrations.RemoveField(
model_name='movie',
name='raters',
),
]
30 changes: 30 additions & 0 deletions movieratings/movies/migrations/0004_auto_20150615_2046.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('movies', '0003_auto_20150615_1941'),
]

operations = [
migrations.CreateModel(
name='Genre',
fields=[
('id', models.AutoField(serialize=False, auto_created=True, verbose_name='ID', primary_key=True)),
('genre', models.CharField(max_length=255)),
],
),
migrations.RemoveField(
model_name='movie',
name='genres',
),
migrations.AddField(
model_name='movie',
name='genres',
field=models.ManyToManyField(to='movies.Genre'),
),
]
24 changes: 24 additions & 0 deletions movieratings/movies/migrations/0005_auto_20150617_1716.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('movies', '0004_auto_20150615_2046'),
]

operations = [
migrations.AddField(
model_name='movie',
name='raters',
field=models.ManyToManyField(to='movies.Rater', through='movies.Rating'),
),
migrations.AlterField(
model_name='rating',
name='timestamp',
field=models.DateTimeField(auto_now_add=True),
),
]
Empty file.
51 changes: 51 additions & 0 deletions movieratings/movies/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from django.db import models
from django.contrib.auth.models import User
# Create your models here.


class Rater(models.Model):
age = models.IntegerField()
zip_code = models.CharField(max_length=10)
user = models.OneToOneField(User, null=True)

def __str__(self):
return "Rater {}".format(self.id)


class Genre(models.Model):
genre = models.CharField(max_length=255)

def __str__(self):
return self.genre


class Movie(models.Model):
title = models.CharField(max_length=255)
genres = models.ManyToManyField(Genre)
raters = models.ManyToManyField(Rater, through='Rating')

def __str__(self):
return self.title

def average_rating(self):
return self.rating_set.all().aggregate(models.Avg('rating'))['rating__avg']


class Rating(models.Model):
rating = models.IntegerField()
rater = models.ForeignKey(Rater)
movie = models.ForeignKey(Movie)
timestamp = models.DateTimeField(auto_now_add=True)

def __str__(self):
return "{} / {}".format(self.movie, self.rater)

def create_user():
for rater in Rater.objects.all():
user = User.objects.create_user(
"user{}".format(rater.id),
"user{}@theironyard.com".format(rater.id),
"user{}".format(rater.id)
)
rater.user = user
rater.save()
16 changes: 16 additions & 0 deletions movieratings/movies/templates/movies/allmovies.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "movies/base.html" %}

{% block content %}
<h2>All Movies</h2>

{% for movie in movies %}
<div>
<p>
<ul>
<li><a href="{% url 'movie' movie.id %}">{{ movie }} {{ movie.average_rating|floatformat:2 }}</li>
</ul>
</p>
</div>
{% endfor %}
{% endblock %}
</html>
16 changes: 16 additions & 0 deletions movieratings/movies/templates/movies/allraters.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "movies/base.html" %}

{% block content %}
<h2>All Raters</h2>

{% for rater in raters %}
<div>
<p>
<ul>
<li><a href="{% url 'rater' rater.id %}">{{ rater.id }} - Age: {{ rater.age }} - Zip: {{ rater.zip_code }}</li>
</ul>
</p>
</div>
{% endfor %}
{% endblock %}
</html></html>
Loading