# Path: feedback/models.py
# class Feedback(db.Model):
#     page = db.StringProperty(required=True)
#     message = db.StringProperty(required=True)
#     points = db.IntegerProperty(default=1)
#     ip = db.StringProperty()
#     submitter = db.ReferenceProperty(User)
#     submitted = db.DateTimeProperty(auto_now_add=True)
# 
#     def __unicode__(self):
#         return self.message[:50]
# 
# class Vote(db.Model):
#     feedback = db.ReferenceProperty(Feedback, required=True)
#     ip = db.StringProperty(required=True)
# 
#     def feedback_id(self):
#         return Vote.feedback.get_value_for_datastore(self).id()
# 
# Path: feedback/forms.py
# class FeedbackForm(forms.Form):
#     """
#     Simple form for user feedback.
#     """
#     message = forms.CharField(max_length=400, required=True,
#         label="How can we improve this page?",
#         widget=forms.TextInput(attrs={'class': 'text span-6'}))
#     page = forms.CharField(max_length=400, required=True,
#         widget=forms.HiddenInput)
# 
# Path: feedback/views.py
# def get_already_voted(request):
#     """
#     Don't show vote buttons if posted or voted from the same IP.
#     """
#     ip = request.META.get('REMOTE_ADDR', '0.0.0.0')
#     posted = [feedback.id()
#               for feedback in Feedback.all(keys_only=True).filter('ip', ip)]
#     voted = [vote.feedback_id()
#              for vote in Vote.all().filter('ip', ip)]
#     # logging.debug('posted=%s voted=%s' % (posted, voted))
#     return set(posted + voted)


from django import template
from django.template.loader import render_to_string
from google.appengine.api import datastore_errors
from feedback.models import Feedback, Vote
from feedback.forms import FeedbackForm
from feedback.views import get_already_voted


def render_query(request, query, limit=10):
    feedback_list = []
    for feedback in query.fetch(limit):
        try:
            submitter = feedback.submitter  # Attempt to dereference.
            feedback_list.append(feedback)
        except datastore_errors.Error:
            pass  # Ignore feedback if the submitter doesn't exist.
    already_voted = get_already_voted(request)
    return render_to_string('feedback/messages.html', locals())


@register.simple_tag
def feedback_recently(request, page=None):
    if page is None:
        page = request.META['PATH_INFO']
    query = Feedback.all()
    if page != 'all':
        query.filter('page', page)
    query.order('-submitted')
    return render_query(request, query, 20 if page == 'all' else 10)


@register.simple_tag
def feedback_popular(request, page=None):
    if page is None:
        page = request.META['PATH_INFO']
    feedback_list = []

# Path: feedback/models.py
# class Feedback(db.Model):
#     page = db.StringProperty(required=True)
#     message = db.StringProperty(required=True)
#     points = db.IntegerProperty(default=1)
#     ip = db.StringProperty()
#     submitter = db.ReferenceProperty(User)
#     submitted = db.DateTimeProperty(auto_now_add=True)
# 
#     def __unicode__(self):
#         return self.message[:50]
# 
# class Vote(db.Model):
#     feedback = db.ReferenceProperty(Feedback, required=True)
#     ip = db.StringProperty(required=True)
# 
#     def feedback_id(self):
#         return Vote.feedback.get_value_for_datastore(self).id()
# 
# Path: feedback/forms.py
# class FeedbackForm(forms.Form):
#     """
#     Simple form for user feedback.
#     """
#     message = forms.CharField(max_length=400, required=True,
#         label="How can we improve this page?",
#         widget=forms.TextInput(attrs={'class': 'text span-6'}))
#     page = forms.CharField(max_length=400, required=True,
#         widget=forms.HiddenInput)
# 
# Path: feedback/views.py
# def get_already_voted(request):
#     """
#     Don't show vote buttons if posted or voted from the same IP.
#     """
#     ip = request.META.get('REMOTE_ADDR', '0.0.0.0')
#     posted = [feedback.id()
#               for feedback in Feedback.all(keys_only=True).filter('ip', ip)]
#     voted = [vote.feedback_id()
#              for vote in Vote.all().filter('ip', ip)]
#     # logging.debug('posted=%s voted=%s' % (posted, voted))
#     return set(posted + voted)


# Path: feedback/templatetags/feedback_tags.py
from django import template
from django.template.loader import render_to_string
from google.appengine.api import datastore_errors
from feedback.models import Feedback, Vote
from feedback.forms import FeedbackForm
from feedback.views import get_already_voted



def render_query(request, query, limit=10):
    feedback_list = []
    for feedback in query.fetch(limit):
        try:
            submitter = feedback.submitter  # Attempt to dereference.
            feedback_list.append(feedback)
        except datastore_errors.Error:
            pass  # Ignore feedback if the submitter doesn't exist.
    already_voted = get_already_voted(request)
    return render_to_string('feedback/messages.html', locals())


@register.simple_tag
def feedback_recently(request, page=None):
    if page is None:
        page = request.META['PATH_INFO']
    query = Feedback.all()
    if page != 'all':
        query.filter('page', page)
    query.order('-submitted')
    return render_query(request, query, 20 if page == 'all' else 10)


@register.simple_tag
def feedback_popular(request, page=None):
    if page is None:
        page = request.META['PATH_INFO']
    feedback_list = []

    query = Feedback.all()