# Path: quotes/models.py
# class Author(models.Model):
#     name = models.CharField(max_length=200)
#     short_bio = models.TextField(max_length=500, null=True, blank=True)
#     slug = models.SlugField(max_length=100, blank=True)
#     uuid = models.CharField(max_length=100, blank=True, null=True)
#     avatar = ImageField(upload_to='authors',
#                         blank=True,
#                         null=True)
# 
#     class Meta:
#         ordering = ['name']
# 
#     def __unicode__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return reverse('author', kwargs={'slug': self.slug})
# 
#     def get_avatar(self, size='60x60'):
#         try:
#             thumbnail = get_thumbnail(self.avatar,
#                                  size,
#                                  crop='center',
#                                  quality=70)
#             if thumbnail:
#                 return thumbnail.url
#         except ThumbnailError:
#             return None
# 
# class Tag(models.Model):
#     name = models.CharField(max_length=100)
#     slug = models.SlugField(max_length=100, blank=True)
#     uuid = models.CharField(max_length=100, blank=True, null=True)
# 
#     class Meta:
#         ordering = ['name']
# 
#     def __unicode__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return reverse('tag', kwargs={'slug': self.slug})
# 
# class Quote(models.Model):
#         body = models.TextField()
#         authors = models.ManyToManyField(Author)
#         tags = models.ManyToManyField(Tag, blank=True, null=True)
#         slug = models.SlugField(max_length=100, blank=True)
#         publish_date = models.DateTimeField(default=datetime.now)
#         source = models.TextField(null=True, blank=True)
#         featured = models.BooleanField(default=False)
#         uuid = models.CharField(max_length=100, blank=True, null=True)
# 
#         class Meta:
#             ordering = ['-publish_date']
# 
#         def __unicode__(self):
#             return self.body
# 
#         def get_absolute_url(self):
#             return reverse('quote', kwargs={'uuid': self.uuid})
# 
#         def serialize(self):
#             return {
#                 'body': self.body,
#                 'url': self.get_absolute_url(),
#                 'authors': [
#                     {
#                         'name': author.name,
#                         'url': author.get_absolute_url(),
#                         'avatar': author.get_avatar(),
#                     }
#                     for author in self.authors.all()
#                 ],
#                 'tags': [
#                     {
#                         'name': tag.name,
#                         'url': tag.get_absolute_url(),
#                     }
#                     for tag in self.tags.all()
#                 ]
#             }
# 
# Path: quotes/forms.py
# class QuoteForm(forms.Form):
#     name = forms.CharField(label='Your name', max_length=50)
#     email = forms.EmailField(label='Your e-mail')
#     quote = forms.Field(label='Quote', widget=forms.Textarea)
#     authors = forms.CharField(label='Authors, separated by commas')
#     tags = forms.CharField(label='Tags, separated by commas')
#     source = forms.CharField(label='Source (website, book etc.)')
# 
#     def save(self, data):
#             message = "%s\n\nFrom: %s <%s>\nAuthors: %s\n" \
#                       "Tags: %s\nSource: %s\n" % (data.get('quote'),
#                                                   data.get('name'),
#                                                   data.get('email'),
#                                                   data.get('authors'),
#                                                   data.get('tags'),
#                                                   data.get('source'))
# 
#             send_mail('[defprogramming] New quote', message, data.get('email'),
#                       [settings.SERVER_EMAIL], fail_silently=False)


import json
from django.conf import settings
from django.core.paginator import Paginator
from django.core.urlresolvers import reverse
from django.http import HttpResponsePermanentRedirect, HttpResponse
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.views.decorators.cache import cache_page
from django.views.generic import DetailView, ListView
from .models import Author, Tag, Quote
from .forms import QuoteForm
                                  locals(),
                                  context_instance=RequestContext(request))


def quote_redirect(request, slug):
    quote = get_object_or_404(Quote, slug=slug)
    return HttpResponsePermanentRedirect(quote.get_absolute_url())


def random(request):
    quote = Quote.objects.order_by('?')[0]
    return render_to_response('quotes/random.html',
                              {'quote': quote},
                              context_instance=RequestContext(request))


def submit_quote(request):
    title = "Submit a quote | defprogramming"
    description = "Use this form to submit a quote. Please send only quotes " \
                  "about programming, coding, software industry."
    sent = False

    if request.method == 'POST':
        form = QuoteForm(request.POST)

        if form.is_valid():
            form.save(request.POST)
            sent = True

    else:

# Path: quotes/models.py
# class Author(models.Model):
#     name = models.CharField(max_length=200)
#     short_bio = models.TextField(max_length=500, null=True, blank=True)
#     slug = models.SlugField(max_length=100, blank=True)
#     uuid = models.CharField(max_length=100, blank=True, null=True)
#     avatar = ImageField(upload_to='authors',
#                         blank=True,
#                         null=True)
# 
#     class Meta:
#         ordering = ['name']
# 
#     def __unicode__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return reverse('author', kwargs={'slug': self.slug})
# 
#     def get_avatar(self, size='60x60'):
#         try:
#             thumbnail = get_thumbnail(self.avatar,
#                                  size,
#                                  crop='center',
#                                  quality=70)
#             if thumbnail:
#                 return thumbnail.url
#         except ThumbnailError:
#             return None
# 
# class Tag(models.Model):
#     name = models.CharField(max_length=100)
#     slug = models.SlugField(max_length=100, blank=True)
#     uuid = models.CharField(max_length=100, blank=True, null=True)
# 
#     class Meta:
#         ordering = ['name']
# 
#     def __unicode__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return reverse('tag', kwargs={'slug': self.slug})
# 
# class Quote(models.Model):
#         body = models.TextField()
#         authors = models.ManyToManyField(Author)
#         tags = models.ManyToManyField(Tag, blank=True, null=True)
#         slug = models.SlugField(max_length=100, blank=True)
#         publish_date = models.DateTimeField(default=datetime.now)
#         source = models.TextField(null=True, blank=True)
#         featured = models.BooleanField(default=False)
#         uuid = models.CharField(max_length=100, blank=True, null=True)
# 
#         class Meta:
#             ordering = ['-publish_date']
# 
#         def __unicode__(self):
#             return self.body
# 
#         def get_absolute_url(self):
#             return reverse('quote', kwargs={'uuid': self.uuid})
# 
#         def serialize(self):
#             return {
#                 'body': self.body,
#                 'url': self.get_absolute_url(),
#                 'authors': [
#                     {
#                         'name': author.name,
#                         'url': author.get_absolute_url(),
#                         'avatar': author.get_avatar(),
#                     }
#                     for author in self.authors.all()
#                 ],
#                 'tags': [
#                     {
#                         'name': tag.name,
#                         'url': tag.get_absolute_url(),
#                     }
#                     for tag in self.tags.all()
#                 ]
#             }
# 
# Path: quotes/forms.py
# class QuoteForm(forms.Form):
#     name = forms.CharField(label='Your name', max_length=50)
#     email = forms.EmailField(label='Your e-mail')
#     quote = forms.Field(label='Quote', widget=forms.Textarea)
#     authors = forms.CharField(label='Authors, separated by commas')
#     tags = forms.CharField(label='Tags, separated by commas')
#     source = forms.CharField(label='Source (website, book etc.)')
# 
#     def save(self, data):
#             message = "%s\n\nFrom: %s <%s>\nAuthors: %s\n" \
#                       "Tags: %s\nSource: %s\n" % (data.get('quote'),
#                                                   data.get('name'),
#                                                   data.get('email'),
#                                                   data.get('authors'),
#                                                   data.get('tags'),
#                                                   data.get('source'))
# 
#             send_mail('[defprogramming] New quote', message, data.get('email'),
#                       [settings.SERVER_EMAIL], fail_silently=False)


# Path: quotes/views.py
import json
from django.conf import settings
from django.core.paginator import Paginator
from django.core.urlresolvers import reverse
from django.http import HttpResponsePermanentRedirect, HttpResponse
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.views.decorators.cache import cache_page
from django.views.generic import DetailView, ListView
from .models import Author, Tag, Quote
from .forms import QuoteForm

                                  locals(),
                                  context_instance=RequestContext(request))


def quote_redirect(request, slug):
    quote = get_object_or_404(Quote, slug=slug)
    return HttpResponsePermanentRedirect(quote.get_absolute_url())


def random(request):
    quote = Quote.objects.order_by('?')[0]
    return render_to_response('quotes/random.html',
                              {'quote': quote},
                              context_instance=RequestContext(request))


def submit_quote(request):
    title = "Submit a quote | defprogramming"
    description = "Use this form to submit a quote. Please send only quotes " \
                  "about programming, coding, software industry."
    sent = False

    if request.method == 'POST':
        form = QuoteForm(request.POST)

        if form.is_valid():
            form.save(request.POST)
            sent = True

    else:

        form = QuoteForm()