from django.db import models
from django.contrib.comments.signals import comment_was_posted
from django.core.mail import send_mail
from app.models import ItfModel

class Issue(ItfModel):
  title = models.CharField(max_length=255)
  summary = models.TextField(blank=True, null=True)
  date = models.DateField()
  html = models.TextField(blank=True)

  fts_fields = ['title', 'summary']
  fk_filters = []

  def preview_dict(self):
    return {
      'id': self.id,
      'title': self.title,
      'summary': self.summary,
    }

  def info_dict(self):
    d = self.preview_dict()
    d['html'] = self.html
    return d
  
  def list_dict(self):
    return {
      'id': self.id,
      'title': self.title
    }

  def __unicode__(self):
    return self.title

  def get_dict(self):
  #FIXME: return self.date in JSON-friendly way
    return {
      'id': self.id,
      'title': self.title,
      'summary': self.summary,
    }

# Create your models here.

def comments_notify(sender, **kwargs):
  comment = kwargs['comment']
  name = comment.name
  email = comment.email
  content = comment.comment
  erang_id = comment.content_object.id
  comment_id = comment.id
#  url = "http://theatreforum.in/erang/?issue_id=%d" % (erang_id)
  admin_url = "http://theatreforum.in/admin/comments/comment/%d/" % (comment_id)
  message = "Name: %s \n Email: %s \n Comment: %s\n\n Moderate: %s" % (name, email, content, admin_url)
  send_mail("New comment on Theatreforum.in", message, "do_not_reply@theatreforum.in", ["sanjaybhangar@gmail.com"])
  return True

comment_was_posted.connect(comments_notify)
