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

Ratings #29

Open
wants to merge 2 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
3 changes: 3 additions & 0 deletions .directory
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[Dolphin]
Timestamp=2020,5,4,6,54,0
Version=4
5 changes: 3 additions & 2 deletions lmn/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

# Register your models here, for them to be displayed in the admin view

from .models import Venue, Artist, Note, Show, Profile
from .models import Venue, Artist, Note, Show, Profile, Rating

admin.site.register(Venue)
admin.site.register(Artist)
admin.site.register(Note)
admin.site.register(Show)
admin.site.register(Profile)
admin.site.register(Profile)
admin.site.register(Rating)
8 changes: 7 additions & 1 deletion lmn/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django import forms
from .models import Note, Profile
from .models import Note, Profile, Rating

from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
Expand All @@ -21,6 +21,12 @@ class Meta:
model = Note
fields = ('title', 'text')

class NewRatingForm(forms.ModelForm):
stars = forms.IntegerField(max_value = 5, min_value = 1)
class Meta:
model = Rating
fields = ('stars',)


class UserRegistrationForm(UserCreationForm):

Expand Down
14 changes: 14 additions & 0 deletions lmn/migrations/0008_merge_20200510_2056.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 2.1.11 on 2020-05-10 20:56

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('lmn', '0007_auto_20200429_1229'),
('lmn', '0006_auto_20200505_2339'),
]

operations = [
]
34 changes: 34 additions & 0 deletions lmn/migrations/0009_auto_20200511_1105.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 2.1.11 on 2020-05-11 11:05

from django.conf import settings
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('lmn', '0008_merge_20200510_2056'),
]

operations = [
migrations.CreateModel(
name='Rating',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('stars', models.IntegerField(validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(5)])),
('show', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='lmn.Show')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.AlterUniqueTogether(
name='rating',
unique_together={('user', 'show')},
),
migrations.AlterIndexTogether(
name='rating',
index_together={('user', 'show')},
),
]
19 changes: 19 additions & 0 deletions lmn/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db import models
from django.contrib.auth.models import User
from django.core.validators import MaxValueValidator, MinValueValidator
from PIL import Image
import datetime

Expand Down Expand Up @@ -44,6 +45,9 @@ class Show(models.Model):
def __str__(self):
return 'Show with artist {} at {} on {}'.format(self.artist, self.venue, self.show_date)

def no_of_ratings(self):
ratings = Rating.objects.filter(show=self)
return len(ratings)

''' One user's opinion of one show. '''
class Note(models.Model):
Expand Down Expand Up @@ -89,4 +93,19 @@ def save(self, *args, **kwargs):
img.thumbnail(output_size)
img.save(self.profile_img.path)

''' Ratings for shows '''
class Rating(models.Model):
show = models.ForeignKey(Show, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
stars = models.IntegerField(validators=[MinValueValidator(1),
MaxValueValidator(5)])

''' Allows only one show rating for one specific user '''
class Meta:
unique_together = (('user', 'show'),)
index_together = (('user', 'show'),)

def __str__(self):
return '{} Rating'.format(self.stars)


1 change: 1 addition & 0 deletions lmn/templates/lmn/notes/new_note.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ <h2>New note for {{ show.artist.name }} at {{ show.venue.name }} on {{ show.show

<form method="POST" action="{% url 'lmn:new_note' show_pk=show.pk %}">{% csrf_token %}
{{ form.as_p }}
{{ rating.as_p }}
<P>
<input type='submit' value='Add Note'>
</form>
Expand Down
2 changes: 1 addition & 1 deletion lmn/templates/lmn/notes/note_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ <h2 id="note_page_title">{{ note.show.artist.name }} at {{ note.show.venue.name

<p id="note_title"><b>{{ note.title}}</b></p>
<p id="note_text">{{ note.text }}</b>

<!-- Rating for show -->
{% endblock %}
2 changes: 1 addition & 1 deletion lmn/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@
path('register/', views_users.register, name='register'),

# Database/Admin related
path('c_?U7j399Hhq', views_admin.admin_main, name='api_data')
path('U7j399Hhq', views_admin.admin_main, name='api_data')
]
3 changes: 2 additions & 1 deletion lmn/views_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
def get_shows():

# Parameters for API request
key = os.environ.get('TICK_MASTER')
# key = os.environ.get('TICK_MASTER')
key = 'wO4gBkxqFIMLDRfALyzCSWYglhOPzUhu'
c_name = 'music' # Filter events to music
num_shows = 20 # Number of upcoming shows
countryCode = 'US'
Expand Down
12 changes: 8 additions & 4 deletions lmn/views_notes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.shortcuts import render, redirect, get_object_or_404

from .models import Venue, Artist, Note, Show
from .forms import VenueSearchForm, NewNoteForm, ArtistSearchForm, UserRegistrationForm
from .forms import VenueSearchForm, NewNoteForm, ArtistSearchForm, UserRegistrationForm, NewRatingForm

from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
Expand All @@ -15,19 +15,23 @@ def new_note(request, show_pk):
show = get_object_or_404(Show, pk=show_pk)

if request.method == 'POST' :

form = NewNoteForm(request.POST)
ratingForm = NewRatingForm(request.POST)
if form.is_valid():
note = form.save(commit=False)
note.user = request.user
note.show = show
note.save()
rating = ratingForm.save(commit=False)
rating.show = show
rating.user = request.user
rating.save()
return redirect('lmn:note_detail', note_pk=note.pk)

else :
form = NewNoteForm()

return render(request, 'lmn/notes/new_note.html' , { 'form': form , 'show': show })
ratingForm = NewRatingForm()
return render(request, 'lmn/notes/new_note.html' , { 'form': form , 'show': show , 'rating': ratingForm})


def latest_notes(request):
Expand Down
16 changes: 8 additions & 8 deletions lmnop_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@

# Uncomment this when you are ready to use Postgres.

'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'lmnop',
'USER' : 'user',
'PASSWORD' : os.environ['LMNOP_DB_PW'],
'HOST' : '/cloudsql/lmnop-275222:us-central1:lmnop-db',
'PORT' : '5432',
},
# 'default': {
# 'ENGINE': 'django.db.backends.postgresql',
# 'NAME': 'lmnop',
# 'USER' : 'user',
# 'PASSWORD' : os.environ['LMNOP_DB_PW'],
# 'HOST' : '/cloudsql/lmnop-275222:us-central1:lmnop-db',
# 'PORT' : '5432',
# },
}
# When you use Postgres, comment out or remove this DB config.

Expand Down