Skip to content

Commit

Permalink
normal mode working
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCoding committed Jun 11, 2015
1 parent ef38d91 commit 63d4d06
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ movieratings/fixtures/
*.sqlite
*.sqlite3
db.sqlite3
movieratings/db.sqlite3
10 changes: 9 additions & 1 deletion movieratings/Rater/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ class Rater(models.Model):
def rating_count(self):
return self.rating_set.count()

def avg_rating(self):
r_set = self.rating_set.all()
if len(r_set) == 0:
return 0
else:
dict = r_set.aggregate(Avg('rating'))
return round(dict["rating__avg"], 2)

def __str__(self):
# return "rater #"+str(models.ForeignKey("self"))+" has rated "\
# +str(self.rating_count())+" movies"
Expand All @@ -30,7 +38,7 @@ def avg_rating(self):
return 0
else:
dict = r_set.aggregate(Avg('rating'))
return dict["rating__avg"]
return round(dict["rating__avg"], 2)

def total_ratings(self):
return self.rating_set.count()
Expand Down
7 changes: 6 additions & 1 deletion movieratings/Rater/templates/Rater/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
</head>
<body>
<header>
<h1><a href="{% url 'index' %}">Movie index</a></h1>
<ul>
<li><a href="http://alancoding.github.io/">Alan's Github Page</a></li>
<li><a href="http://alancoding.github.io/mine.html">Why work? Play Minesweeper.</a></li>
<li><a href="http://alancoding.github.io/jumble.html">Word Jumble</a></li>
</ul>
<h1><a href="{% url 'view_top20_movies' %}">Movie top 20 list</a></h1>
</header>
{% block content %}
{% endblock %}
Expand Down
13 changes: 3 additions & 10 deletions movieratings/Rater/templates/Rater/index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
{% extends "Rater/base.html" %}

{% block content %}
<h2>All movies</h2>
{% for movie in movies %}
<div>
<p>{{ movie.title }}</p>

<p>
<em>{{ movie.release_date }}</em>
-
{{ movie.rating__count }} ratings</p>
</div>
<h2>This is the index page</h2>
<p>there are many pages like it</p>
<p>but this one is the index</p>
{% endfor %}
{% endblock %}
13 changes: 13 additions & 0 deletions movieratings/Rater/templates/Rater/movie.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "Rater/base.html" %}

{% block content %}
<h2>Movie: {{ movie.title }}</h2>
<p>average score = {{ movie.avg_rating }}</p>
<p>has been reviewed by users</p>
<ul>
{% for rating in ratings %}
<li><a href="{% url 'view_user' rating.rater.id %}">User number {{ rating.rater.id }}</a>
gave it {{ rating.rating }} stars</li>
{% endfor %}

{% endblock %}
14 changes: 14 additions & 0 deletions movieratings/Rater/templates/Rater/top20.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends "Rater/base.html" %}

{% block content %}
<h2>All movies</h2>
<p>(highest scoring, reviewed by 10 or more people)</p>
{% for movie in movies %}
<div>
<p><a href="{% url 'view_movie' movie.id %}">{{ movie.title }}</a>
- average rating of
{{ movie.avg_rating }} stars, reviewed by
{{ movie.total_ratings }} users</p>
</div>
{% endfor %}
{% endblock %}
13 changes: 13 additions & 0 deletions movieratings/Rater/templates/Rater/user.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "Rater/base.html" %}

{% block content %}
<h2>User number {{ rater.id }}</h2>
<p>average score given = {{ rater.avg_rating }}</p>
<p>the movies and scores given were:</p>
<ul>
{% for rating in ratings %}
<li><a href="{% url 'view_movie' rating.movie.id %}">{{ rating.movie.title }}</a>
rated {{ rating.rating }} stars</li>
{% endfor %}

{% endblock %}
28 changes: 24 additions & 4 deletions movieratings/Rater/views.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
from django.shortcuts import render

# Create your views here.
from django.db.models import Count
from django.db.models import Count, Avg
from .models import Movie, Rater, Rating
#from django.contrib.auth.models import Rater


def index(request, arg2):
movies = Movie.objects.annotate(Count('rating')).order_by('-rating__count')[:20]
def view_index(request):
return render(request, "Rater/index.html", {"var": 1})

def view_top20_movies(request):
big_movies = Movie.objects.annotate(num_ratings=Count('rating')).filter(num_ratings__gt=10)
movies = sorted(big_movies, key=lambda a: a.avg_rating(), reverse=True)[:20]
#movies = big_movies.order_by('-num_ratings')[:20]
#movies = Movie.objects.annotate(Avg('rating')).order_by('-rating__avg')[:20]
#movies = Movie.objects.order_by('-avg_rating')[:20]
# also try Rater_rating later
# statuses = Status.objects.annotate(Count('favorite')).order_by('-posted_at')
return render(request,
"Rater/index.html",
"Rater/top20.html",
{"movies": movies})

def view_user(request, rater_id):
r = Rater.objects.get(pk=rater_id)
rs = r.rating_set.all()
return render(request,
"Rater/user.html",
{"rater": r, "ratings": rs})

def view_movie(request, movie_id):
movie = Movie.objects.get(pk=movie_id)
rs = movie.rating_set.all()
return render(request,
"Rater/movie.html",
{"movie": movie, "ratings": rs })

# def show_user(request, user_id):
# user = User.objects.get(pk=user_id)
Expand Down
5 changes: 4 additions & 1 deletion movieratings/movieratings/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^(index.html)?$', Rater_views.index, name="index"),
url(r'^index.html$', Rater_views.view_index, name="view_index"),
url(r'^top20.html$', Rater_views.view_top20_movies, name="view_top20_movies"),
url(r'^user.html(?P<rater_id>\d+)$', Rater_views.view_user, name="view_user"),
url(r'^movie.html(?P<movie_id>\d+)$', Rater_views.view_movie, name="view_movie"),

# url(r'^user/(?P<user_id>\d+)$', updates_views.show_user, name="show_user")
]

0 comments on commit 63d4d06

Please sign in to comment.