# Path: electionleaflets/apps/leaflets/models.py
# class Leaflet(models.Model):
#     title = models.CharField(blank=True, max_length=765)
#     description = models.TextField(blank=True, null=True)
#     publisher_party = models.ForeignKey(Party, blank=True, null=True)
#     constituency = models.ForeignKey(Constituency, blank=True, null=True)
# 
#     attacks = models.ManyToManyField(Party, related_name='attacks',
#         null=True, blank=True)
#     tags = models.ManyToManyField(Tag, through='LeafletTag')
#     categories = models.ManyToManyField(Category, through='LeafletCategory')
#     imprint = models.TextField(blank=True, null=True)
#     postcode = models.CharField(max_length=150, blank=True)
#     lng = models.FloatField(blank=True, null=True)
#     lat = models.FloatField(blank=True, null=True)
#     name = models.CharField(blank=True, max_length=300)
#     email = models.CharField(blank=True, max_length=300)
#     date_uploaded = models.DateTimeField(auto_now_add=True)
#     date_delivered = models.DateTimeField(blank=True, null=True)
#     status = models.CharField(choices=constants.LEAFLET_STATUSES,
#         null=True, blank=True, max_length=255)
# 
#     def __unicode__(self):
#         return self.title
# 
#     class Meta:
#         ordering = ('date_uploaded',)
# 
#     def get_absolute_url(self):
#         from django.contrib.sites.models import Site
#         return 'http://%s/leaflets/%s/' % (Site.objects.get_current().domain,
#             self.id)
# 
#     def get_first_image(self):
#         try:
#             return self.images.all()[0]
#         except IndexError:
#             # TODO: Set image_key to value for 'empty' image
#             return None
# 
#     def get_title(self):
#         if self.title and len(self.title):
#             return self.title
#         elif self.publisher_party:
#             return '%s leaflet' % self.party.name
#         else:
#             "Untitled leaflet"
# 
# class LeafletImage(models.Model):
#     leaflet = models.ForeignKey(Leaflet, related_name='images')
#     image = ImageField(upload_to="leaflets")
#     legacy_image_key = models.CharField(max_length=255)
#     image_type =  models.CharField(choices=constants.IMAGE_TYPES,
#         null=True, blank=True, max_length=255)
#     image_text = models.TextField(blank=True)
# 
#     class Meta:
#         ordering = ['image_type']
# 
#     def ocr(self):
#         if not self.image:
#             return ""
# 
#         from PIL import ImageFilter
# 
#         image_path = self.image.path
#         tmp_image_path = "/tmp/leaflet_image-%s.jpg" % self.legacy_image_key
#         # image_file = Image.open(image_path) # open colour image
#         # image_file = image_file.filter(ImageFilter.EDGE_ENHANCE)
#         # image_file = image_file.filter(ImageFilter.SMOOTH_MORE)
#         # image_file = image_file.filter(ImageFilter.FIND_EDGES)
#         # image_file = image_file.filter(ImageFilter.DETAIL)
#         # image_file = image_file.filter(ImageFilter.MinFilter(3))
#         # image_file = image_file.convert('L') # convert image to black and white
#         # image_file = ImageEnhance.Contrast(image_file)
#         # image_file = image_file.enhance(1.5)
# 
#         # image_file.save(tmp_image_path, dpi=(300,300))
# 
#         text = pytesser.image_to_string(image_path)
#         text = os.linesep.join([s for s in text.splitlines() if s])
#         self.image_text = text
#         self.save()
#         os.remove(image_path)
#         return text


import os
from django.template  import RequestContext
from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.contrib.formtools.wizard.views import NamedUrlSessionWizardView
from django.core.urlresolvers import reverse
from django.conf import settings
from django.views.generic import DetailView, ListView, DetailView
from django.core.files.storage import FileSystemStorage
from .models import Leaflet, LeafletImage
    from leaflets.models import Leaflet, LeafletImage




class ImageView(DetailView):
    model = LeafletImage
    template_name = 'leaflets/full.html'
    pk_url_kwarg = 'image_key'

# def view_full_image(request, image_key):
#
#     li = LeafletImage.objects.filter(pk=image_key)
#     if li.count() == 1:
#         li = get_object_or_404(LeafletImage, image_key=image_key)
#     else:
#         # Should not do this, we'll need to fix it
#         # probably and upload artifact
#         li = li.all()[0]
#
#     return render_to_response('leaflets/full.html',
#                             {
#                                 'image': li,
#                                 'leaflet': li.leaflet,
#                             },
#                             context_instance=RequestContext(request), )


def view_all_full_images(request, leafletid):


# Path: electionleaflets/apps/leaflets/models.py
# class Leaflet(models.Model):
#     title = models.CharField(blank=True, max_length=765)
#     description = models.TextField(blank=True, null=True)
#     publisher_party = models.ForeignKey(Party, blank=True, null=True)
#     constituency = models.ForeignKey(Constituency, blank=True, null=True)
# 
#     attacks = models.ManyToManyField(Party, related_name='attacks',
#         null=True, blank=True)
#     tags = models.ManyToManyField(Tag, through='LeafletTag')
#     categories = models.ManyToManyField(Category, through='LeafletCategory')
#     imprint = models.TextField(blank=True, null=True)
#     postcode = models.CharField(max_length=150, blank=True)
#     lng = models.FloatField(blank=True, null=True)
#     lat = models.FloatField(blank=True, null=True)
#     name = models.CharField(blank=True, max_length=300)
#     email = models.CharField(blank=True, max_length=300)
#     date_uploaded = models.DateTimeField(auto_now_add=True)
#     date_delivered = models.DateTimeField(blank=True, null=True)
#     status = models.CharField(choices=constants.LEAFLET_STATUSES,
#         null=True, blank=True, max_length=255)
# 
#     def __unicode__(self):
#         return self.title
# 
#     class Meta:
#         ordering = ('date_uploaded',)
# 
#     def get_absolute_url(self):
#         from django.contrib.sites.models import Site
#         return 'http://%s/leaflets/%s/' % (Site.objects.get_current().domain,
#             self.id)
# 
#     def get_first_image(self):
#         try:
#             return self.images.all()[0]
#         except IndexError:
#             # TODO: Set image_key to value for 'empty' image
#             return None
# 
#     def get_title(self):
#         if self.title and len(self.title):
#             return self.title
#         elif self.publisher_party:
#             return '%s leaflet' % self.party.name
#         else:
#             "Untitled leaflet"
# 
# class LeafletImage(models.Model):
#     leaflet = models.ForeignKey(Leaflet, related_name='images')
#     image = ImageField(upload_to="leaflets")
#     legacy_image_key = models.CharField(max_length=255)
#     image_type =  models.CharField(choices=constants.IMAGE_TYPES,
#         null=True, blank=True, max_length=255)
#     image_text = models.TextField(blank=True)
# 
#     class Meta:
#         ordering = ['image_type']
# 
#     def ocr(self):
#         if not self.image:
#             return ""
# 
#         from PIL import ImageFilter
# 
#         image_path = self.image.path
#         tmp_image_path = "/tmp/leaflet_image-%s.jpg" % self.legacy_image_key
#         # image_file = Image.open(image_path) # open colour image
#         # image_file = image_file.filter(ImageFilter.EDGE_ENHANCE)
#         # image_file = image_file.filter(ImageFilter.SMOOTH_MORE)
#         # image_file = image_file.filter(ImageFilter.FIND_EDGES)
#         # image_file = image_file.filter(ImageFilter.DETAIL)
#         # image_file = image_file.filter(ImageFilter.MinFilter(3))
#         # image_file = image_file.convert('L') # convert image to black and white
#         # image_file = ImageEnhance.Contrast(image_file)
#         # image_file = image_file.enhance(1.5)
# 
#         # image_file.save(tmp_image_path, dpi=(300,300))
# 
#         text = pytesser.image_to_string(image_path)
#         text = os.linesep.join([s for s in text.splitlines() if s])
#         self.image_text = text
#         self.save()
#         os.remove(image_path)
#         return text


# Path: electionleaflets/apps/leaflets/views.py
import os
from django.template  import RequestContext
from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.contrib.formtools.wizard.views import NamedUrlSessionWizardView
from django.core.urlresolvers import reverse
from django.conf import settings
from django.views.generic import DetailView, ListView, DetailView
from django.core.files.storage import FileSystemStorage
from .models import Leaflet, LeafletImage
    from leaflets.models import Leaflet, LeafletImage





class ImageView(DetailView):
    model = LeafletImage
    template_name = 'leaflets/full.html'
    pk_url_kwarg = 'image_key'

# def view_full_image(request, image_key):
#
#     li = LeafletImage.objects.filter(pk=image_key)
#     if li.count() == 1:
#         li = get_object_or_404(LeafletImage, image_key=image_key)
#     else:
#         # Should not do this, we'll need to fix it
#         # probably and upload artifact
#         li = li.all()[0]
#
#     return render_to_response('leaflets/full.html',
#                             {
#                                 'image': li,
#                                 'leaflet': li.leaflet,
#                             },
#                             context_instance=RequestContext(request), )


def view_all_full_images(request, leafletid):


    leaflet = get_object_or_404(Leaflet, pk=leafletid)