# Path: post/models.py
# POST_CATEGORIES = {
# 	1: "Crop Disease/Problems",
# 	2: "Climate Related",
# 	3: "Others",
# }
# 
# Path: User/models.py
# def check_if_auth_user(request):
# 	if (request.session is not None) and request.session.has_key("user_mail_id"):
# 		return request.session["user_mail_id"]
# 	else:
# 		return None
# 
# Path: Farmers_Portal/settings.py
# MEDIA_ROOT = os.path.join(BASE_DIR,"media_cdn")


from django.shortcuts import render, get_object_or_404, redirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.contrib import messages
from django.core.files.base import ContentFile
from django.db import connection, transaction
from .models import POST_CATEGORIES
from User.models import check_if_auth_user
from Farmers_Portal.settings import MEDIA_ROOT
from collections import namedtuple
import datetime
import os
import base64

			file_content = ContentFile(image_read)

			# Iterate through the chunks.
			for chunk in file_content.chunks():
				fout.write(chunk)
			fout.close()

			image_64_encode = base64.encodestring(image_read)

		# Data modifying operation - commit required
		if image:
			if request.session["user_class"] == 'E':
				query = "INSERT INTO post_post('title', 'description', 'category', 'timestamp', 'updated', 'author_expert_id', 'image', 'image_db') Values(%s, %s, %s, %s, %s, %s, %s, %s)"
			else:
				query = "INSERT INTO post_post('title', 'description', 'category', 'timestamp', 'updated', 'author_farmer_id', 'image', 'image_db') Values(%s, %s, %s, %s, %s, %s, %s, %s)"
			cursor.execute(query, [title, disc, category,  datetime.datetime.now(),  datetime.datetime.now(), current_user.auto_id, image.name, image_64_encode])
		else:
			if request.session["user_class"] == 'E':
				query = "INSERT INTO post_post('title', 'description', 'category', 'timestamp', 'updated', 'author_expert_id') Values(%s, %s, %s, %s, %s, %s)"
			else:
				query = "INSERT INTO post_post('title', 'description', 'category', 'timestamp', 'updated', 'author_farmer_id') Values(%s, %s, %s, %s, %s, %s)"
			cursor.execute(query, [title, disc, category,  datetime.datetime.now(),  datetime.datetime.now(), current_user.auto_id])			
		
		transaction.commit()

		messages.success(request, "New Post Created")
		return redirect("home:welcome")

	context_data={

# Path: post/models.py
# POST_CATEGORIES = {
# 	1: "Crop Disease/Problems",
# 	2: "Climate Related",
# 	3: "Others",
# }
# 
# Path: User/models.py
# def check_if_auth_user(request):
# 	if (request.session is not None) and request.session.has_key("user_mail_id"):
# 		return request.session["user_mail_id"]
# 	else:
# 		return None
# 
# Path: Farmers_Portal/settings.py
# MEDIA_ROOT = os.path.join(BASE_DIR,"media_cdn")


# Path: post/views.py
from django.shortcuts import render, get_object_or_404, redirect
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.contrib import messages
from django.core.files.base import ContentFile
from django.db import connection, transaction
from .models import POST_CATEGORIES
from User.models import check_if_auth_user
from Farmers_Portal.settings import MEDIA_ROOT
from collections import namedtuple
import datetime
import os
import base64


			file_content = ContentFile(image_read)

			# Iterate through the chunks.
			for chunk in file_content.chunks():
				fout.write(chunk)
			fout.close()

			image_64_encode = base64.encodestring(image_read)

		# Data modifying operation - commit required
		if image:
			if request.session["user_class"] == 'E':
				query = "INSERT INTO post_post('title', 'description', 'category', 'timestamp', 'updated', 'author_expert_id', 'image', 'image_db') Values(%s, %s, %s, %s, %s, %s, %s, %s)"
			else:
				query = "INSERT INTO post_post('title', 'description', 'category', 'timestamp', 'updated', 'author_farmer_id', 'image', 'image_db') Values(%s, %s, %s, %s, %s, %s, %s, %s)"
			cursor.execute(query, [title, disc, category,  datetime.datetime.now(),  datetime.datetime.now(), current_user.auto_id, image.name, image_64_encode])
		else:
			if request.session["user_class"] == 'E':
				query = "INSERT INTO post_post('title', 'description', 'category', 'timestamp', 'updated', 'author_expert_id') Values(%s, %s, %s, %s, %s, %s)"
			else:
				query = "INSERT INTO post_post('title', 'description', 'category', 'timestamp', 'updated', 'author_farmer_id') Values(%s, %s, %s, %s, %s, %s)"
			cursor.execute(query, [title, disc, category,  datetime.datetime.now(),  datetime.datetime.now(), current_user.auto_id])			
		
		transaction.commit()

		messages.success(request, "New Post Created")
		return redirect("home:welcome")

	context_data={

		"category" : POST_CATEGORIES,