# Path: media_explorer/models.py
# class Element(models.Model):
#     """
#     The Element model will contain images and videos
#     NOTE: if type=video you can still have a thumbnail_image
#     """
# 
#     TYPE_CHOICES = (('image','Image'),('video','Video'))
# 
#     name = models.CharField(max_length=150,blank=True,null=True)
#     file_name = models.CharField(max_length=150,blank=True,null=True)
#     original_file_name = models.CharField(max_length=150,blank=True,null=True)
#     credit = models.CharField(max_length=255,blank=True,null=True)
#     description = models.TextField(blank=True,null=True)
#     image = models.ImageField(blank=True,null=True,max_length=255,upload_to="images/")
#     image_url = models.CharField(max_length=255,blank=True,null=True)
#     image_width = models.IntegerField(blank=True,null=True,default='0')
#     image_height = models.IntegerField(blank=True,null=True,default='0')
#     video_url = models.CharField(max_length=255,blank=True,null=True)
#     video_embed = models.TextField(blank=True,null=True)
#     manual_embed_code = models.BooleanField(_("Manually enter video embed code"), default=False)
#     thumbnail_image = models.ImageField(blank=True,null=True,max_length=255,upload_to="images/")
#     thumbnail_image_url = models.CharField(max_length=255,blank=True,null=True)
#     thumbnail_image_width = models.IntegerField(blank=True,null=True,default='0')
#     thumbnail_image_height = models.IntegerField(blank=True,null=True,default='0')
#     type = models.CharField(_("Type"), max_length=10, default="image",choices=TYPE_CHOICES)
#     created_at = models.DateTimeField(blank=True,null=True,auto_now_add=True)
#     updated_at = models.DateTimeField(blank=True,null=True,auto_now=True)
# 
#     class Meta:
#         verbose_name_plural = "Elements"
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)
# 
#     def save(self, *args, **kwargs):
#         if not self.name:
#             if self.type == "image":
#                 self.name = self.file_name
#             elif self.type == "video":
#                 self.name = self.video_url
#         super(Element, self).save(*args, **kwargs)
# 
#     def save(self, *args, **kwargs):
# 
#         if not self.name:
#             if self.type == "image":
#                 self.name = self.file_name
#             elif self.type == "video":
#                 self.name = self.video_url
# 
#         super(Element, self).save(*args, **kwargs)
# 
#         save_again = False
# 
#         #Init thumbnail_image
#         if self.type == "image" \
#                 and self.image \
#                 and not self.thumbnail_image:
#             self.image_url = self.image.url
#             self.thumbnail_image = self.image
#             self.thumbnail_image_url = self.image.url
#             save_again = True
# 
#         if save_again:
#             super(Element, self).save(*args, **kwargs)
# 
# class Gallery(models.Model):
#     """
#     The Gallery model will contain info about our media gallery
#     """
# 
#     name = models.CharField(max_length=100,blank=True,null=True)
#     short_code = models.CharField(max_length=100,blank=True,null=True)
#     description = models.TextField(blank=True,null=True)
#     thumbnail_image = models.ImageField(blank=True,null=True,max_length=255,upload_to="images/")
#     thumbnail_image_url = models.CharField(max_length=255,blank=True,null=True)
#     elements = models.ManyToManyField(Element, through="GalleryElement")
#     created_at = models.DateTimeField(blank=True,null=True,auto_now_add=True)
#     updated_at = models.DateTimeField(blank=True,null=True,auto_now=True)
# 
#     class Meta:
#         verbose_name_plural = "Galleries"
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)
# 
# class GalleryElement(models.Model):
#     """
#     The Gallery Element model will contain list of elements
#     """
# 
#     gallery = models.ForeignKey(Gallery)
#     element = models.ForeignKey(Element)
#     credit = models.CharField(max_length=255,blank=True,null=True)
#     description = models.TextField(blank=True,null=True)
#     sort_by = models.IntegerField(blank=True,null=True,default='0')
#     created_at = models.DateTimeField(blank=True,null=True,auto_now_add=True)
#     updated_at = models.DateTimeField(blank=True,null=True,auto_now=True)
# 
#     class Meta:
#         verbose_name = "Gallery element"
#         verbose_name_plural = "Gallery elements"
#         ordering = ["sort_by"]


import math, json
from django.http import HttpResponse
from django.views.generic import View
from media_explorer.models import Element, Gallery, GalleryElement
from django.conf import settings
from django.db.models import Q
        #TODO - move to helper so you don't maintain same code twice
        data = {}
        query = None
        and_query = None
        or_query = None
        filter = request.GET.get('filter', None)

        data["page_size"] = settings.DME_PAGE_SIZE

        if filter:
            try:
                filter_list = filter.split(" ")
                or_query = Q(name__icontains=filter_list[0]) | Q(description__icontains=filter_list[0])
                for term in filter_list[1:]:
                    or_query.add((Q(name__icontains=term) | Q(description__icontains=term)), or_query.connector)
            except Exception as e:
                pass

        if and_query:
            query = and_query

        if or_query:
            if query:
                query.add(or_query,Q.AND)
            else:
                query = or_query

        if query:
            data["total_entries"] = Gallery.objects.filter(query).count()
        else:

# Path: media_explorer/models.py
# class Element(models.Model):
#     """
#     The Element model will contain images and videos
#     NOTE: if type=video you can still have a thumbnail_image
#     """
# 
#     TYPE_CHOICES = (('image','Image'),('video','Video'))
# 
#     name = models.CharField(max_length=150,blank=True,null=True)
#     file_name = models.CharField(max_length=150,blank=True,null=True)
#     original_file_name = models.CharField(max_length=150,blank=True,null=True)
#     credit = models.CharField(max_length=255,blank=True,null=True)
#     description = models.TextField(blank=True,null=True)
#     image = models.ImageField(blank=True,null=True,max_length=255,upload_to="images/")
#     image_url = models.CharField(max_length=255,blank=True,null=True)
#     image_width = models.IntegerField(blank=True,null=True,default='0')
#     image_height = models.IntegerField(blank=True,null=True,default='0')
#     video_url = models.CharField(max_length=255,blank=True,null=True)
#     video_embed = models.TextField(blank=True,null=True)
#     manual_embed_code = models.BooleanField(_("Manually enter video embed code"), default=False)
#     thumbnail_image = models.ImageField(blank=True,null=True,max_length=255,upload_to="images/")
#     thumbnail_image_url = models.CharField(max_length=255,blank=True,null=True)
#     thumbnail_image_width = models.IntegerField(blank=True,null=True,default='0')
#     thumbnail_image_height = models.IntegerField(blank=True,null=True,default='0')
#     type = models.CharField(_("Type"), max_length=10, default="image",choices=TYPE_CHOICES)
#     created_at = models.DateTimeField(blank=True,null=True,auto_now_add=True)
#     updated_at = models.DateTimeField(blank=True,null=True,auto_now=True)
# 
#     class Meta:
#         verbose_name_plural = "Elements"
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)
# 
#     def save(self, *args, **kwargs):
#         if not self.name:
#             if self.type == "image":
#                 self.name = self.file_name
#             elif self.type == "video":
#                 self.name = self.video_url
#         super(Element, self).save(*args, **kwargs)
# 
#     def save(self, *args, **kwargs):
# 
#         if not self.name:
#             if self.type == "image":
#                 self.name = self.file_name
#             elif self.type == "video":
#                 self.name = self.video_url
# 
#         super(Element, self).save(*args, **kwargs)
# 
#         save_again = False
# 
#         #Init thumbnail_image
#         if self.type == "image" \
#                 and self.image \
#                 and not self.thumbnail_image:
#             self.image_url = self.image.url
#             self.thumbnail_image = self.image
#             self.thumbnail_image_url = self.image.url
#             save_again = True
# 
#         if save_again:
#             super(Element, self).save(*args, **kwargs)
# 
# class Gallery(models.Model):
#     """
#     The Gallery model will contain info about our media gallery
#     """
# 
#     name = models.CharField(max_length=100,blank=True,null=True)
#     short_code = models.CharField(max_length=100,blank=True,null=True)
#     description = models.TextField(blank=True,null=True)
#     thumbnail_image = models.ImageField(blank=True,null=True,max_length=255,upload_to="images/")
#     thumbnail_image_url = models.CharField(max_length=255,blank=True,null=True)
#     elements = models.ManyToManyField(Element, through="GalleryElement")
#     created_at = models.DateTimeField(blank=True,null=True,auto_now_add=True)
#     updated_at = models.DateTimeField(blank=True,null=True,auto_now=True)
# 
#     class Meta:
#         verbose_name_plural = "Galleries"
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)
# 
# class GalleryElement(models.Model):
#     """
#     The Gallery Element model will contain list of elements
#     """
# 
#     gallery = models.ForeignKey(Gallery)
#     element = models.ForeignKey(Element)
#     credit = models.CharField(max_length=255,blank=True,null=True)
#     description = models.TextField(blank=True,null=True)
#     sort_by = models.IntegerField(blank=True,null=True,default='0')
#     created_at = models.DateTimeField(blank=True,null=True,auto_now_add=True)
#     updated_at = models.DateTimeField(blank=True,null=True,auto_now=True)
# 
#     class Meta:
#         verbose_name = "Gallery element"
#         verbose_name_plural = "Gallery elements"
#         ordering = ["sort_by"]


# Path: media_explorer/views.py
import math, json
from django.http import HttpResponse
from django.views.generic import View
from media_explorer.models import Element, Gallery, GalleryElement
from django.conf import settings
from django.db.models import Q

        #TODO - move to helper so you don't maintain same code twice
        data = {}
        query = None
        and_query = None
        or_query = None
        filter = request.GET.get('filter', None)

        data["page_size"] = settings.DME_PAGE_SIZE

        if filter:
            try:
                filter_list = filter.split(" ")
                or_query = Q(name__icontains=filter_list[0]) | Q(description__icontains=filter_list[0])
                for term in filter_list[1:]:
                    or_query.add((Q(name__icontains=term) | Q(description__icontains=term)), or_query.connector)
            except Exception as e:
                pass

        if and_query:
            query = and_query

        if or_query:
            if query:
                query.add(or_query,Q.AND)
            else:
                query = or_query

        if query:
            data["total_entries"] = Gallery.objects.filter(query).count()
        else:

            data["total_entries"] = Gallery.objects.all().count()