Skip to content

Commit

Permalink
Advance(2) Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hassanabidpk committed Aug 4, 2016
1 parent bc805e7 commit 882e30e
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 25 deletions.
3 changes: 2 additions & 1 deletion menu/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib import admin
from .models import Cupcake
from .models import Cupcake,Comment

admin.site.register(Cupcake)
admin.site.register(Comment)
9 changes: 8 additions & 1 deletion menu/forms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from django import forms
from .models import Cupcake
from .models import Cupcake,Comment


class CupcakeForm(forms.ModelForm):

class Meta:
model = Cupcake
fields = ('name','rating','price','image')


class CommentForm(forms.ModelForm):

class Meta:
model = Comment
fields = ('text',)
30 changes: 30 additions & 0 deletions menu/migrations/0002_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.8 on 2016-08-04 07:52
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

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

operations = [
migrations.CreateModel(
name='Comment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('text', models.TextField()),
('created_date', models.DateTimeField(default=django.utils.timezone.now)),
('approved_comment', models.BooleanField(default=False)),
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='menu.Cupcake')),
('writer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
16 changes: 15 additions & 1 deletion menu/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,18 @@ class Cupcake(models.Model):

def __str__(self):
return self.name



class Comment(models.Model):
post = models.ForeignKey(Cupcake, related_name='comments')
writer = models.ForeignKey(User)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
approved_comment = models.BooleanField(default=False)

def approve(self):
self.approved_comment = True
self.save()

def __str__(self):
return self.text
Binary file added menu/static/menu/images/avatar.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 56 additions & 20 deletions menu/templates/menu/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{% if cake %}
<h2 class="text-center">Order Cupcake</h2>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-4 col-md-offset-2 col-md-lg-2">
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-4 col-md-offset-2 col-lg-offset-2">
<div class="card">
<div class="card-img-top">
<div class="image" style="background-image: url({{ cake.image.url }});"></div>
Expand Down Expand Up @@ -34,27 +34,63 @@ <h2 class="text-center">Order Cupcake</h2>
</div>
</div>
</div>
{% else %}
<h2 class="text-center">No Cupcake found :(</h2>
{% endif %}
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">{{ cake.name }}</h4>
</div>
<div class="modal-body">
<p>Order completed 주문 완료됬었습니다!</p>
<p>{% now "jS F Y H:i" %}</p>
<p>Price : {{ cake.price }}</p>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">{{ cake.name }}</h4>
</div>
<div class="modal-body">
<p>Order completed 주문 완료됬었습니다!</p>
<p>{% now "jS F Y H:i" %}</p>
<p>Price : {{ cake.price }}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-7 col-lg-7 col-md-offset-3 col-lg-offset-2">
<h3>Comments ({{ cake.comments.count }})</h3>
<p></p>
{% if user.is_authenticated %}
<form method="POST">{% csrf_token %}
<!-- {{ form.as_p }} -->
<div class="form-group">
<input type="text" name="{{ form.text.html_name }}" class="form-control" placeholder="Write comment">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
{% else %}
<p><a href="{% url 'accounts:login' %}">Login</a> to post comment</p>
{% endif %}

{% for comment in cake.comments.all %}
{% if comment.approved_comment %}
<div class="media">
<div class="media-left">
<a href="#">
<img class="media-object" src="{% static 'menu/images/avatar.png' %}" alt="">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">{{ comment.writer }}</h4>
<p>{{ comment.text|linebreaks }}</p>
<i>{{ comment.created_date }}</i>
</div>
</div>
{% endif %}
{% empty %}
<p>No comments here yet :(</p>
{% endfor %}
</div>
</div>
{% else %}
<h2 class="text-center">No Cupcake found :(</h2>
{% endif %}
</div>
</div>
{% endblock %}
15 changes: 13 additions & 2 deletions menu/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

from django.shortcuts import render, get_object_or_404,redirect
from .models import Cupcake
from .forms import CupcakeForm
from .forms import CupcakeForm,CommentForm
from django.utils import timezone
from django.contrib.auth.decorators import login_required

Expand All @@ -13,7 +13,18 @@ def cupcake_list(request):

def cupcake_detail(request,pk):
cake = get_object_or_404(Cupcake,pk=pk)
context = {"cake": cake}
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.post = cake
comment.writer = request.user
comment.approved_comment = True
comment.save()
return redirect('menu.views.cupcake_detail', pk=cake.pk)
else:
form = CommentForm()
context = {"cake": cake, "form":form}
return render(request,"menu/detail.html",context)

@login_required
Expand Down

0 comments on commit 882e30e

Please sign in to comment.