2010-07-06 19:25:42 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# View definitions are created here.
|
|
|
|
from django.shortcuts import render_to_response
|
|
|
|
from django.http import HttpResponse,HttpResponseRedirect
|
|
|
|
from djagen.collector.models import *
|
|
|
|
from djagen.collector.forms import ContactForm, QueryForm
|
|
|
|
from djagen.collector.wrappers import render_response
|
|
|
|
from django.conf import settings
|
|
|
|
from django.utils.datastructures import MultiValueDictKeyError
|
|
|
|
import magic
|
|
|
|
import os
|
|
|
|
import datetime, time
|
|
|
|
from django.core.paginator import Paginator, EmptyPage, InvalidPage
|
|
|
|
|
|
|
|
import string
|
|
|
|
|
|
|
|
BASE_URL = settings.BASE_URL
|
|
|
|
|
|
|
|
def main(request):
|
|
|
|
selected_entries = Entries.objects.select_related()
|
|
|
|
entries_list1 = selected_entries.filter(entry_id__label_personal = 1)
|
|
|
|
entries_list2 = selected_entries.filter(entry_id__label_lkd = 1)
|
|
|
|
entries_list3 = selected_entries.filter(entry_id__label_community = 1)
|
|
|
|
entries_list = entries_list1 | entries_list2 | entries_list3
|
|
|
|
|
|
|
|
# This setting gets the content truncated which contains more than <truncate_words> words.
|
|
|
|
truncate_words = 250
|
|
|
|
items_per_page = 25
|
|
|
|
|
|
|
|
#get the last run time
|
|
|
|
run_time = RunTime.objects.all()[0]
|
|
|
|
|
|
|
|
#get the last entries' date
|
|
|
|
last_entry_date = Entries.objects.all()[0].date
|
|
|
|
day = datetime.timedelta(days=1)
|
|
|
|
last_date_li = []
|
2010-08-05 12:00:09 +03:00
|
|
|
for x in xrange(5):
|
2010-07-06 19:25:42 +03:00
|
|
|
last_entry_date -= day
|
|
|
|
last_date_li.append(last_entry_date)
|
|
|
|
|
2010-07-10 01:11:14 +03:00
|
|
|
return render_response(request, 'main/main.html' ,{
|
2010-07-06 19:25:42 +03:00
|
|
|
'entries_list':entries_list,
|
|
|
|
'truncate_words':truncate_words,
|
|
|
|
'items_per_page':repr(items_per_page),
|
|
|
|
'run_time':run_time,
|
|
|
|
#'pag_entries_list':pag_entries_list,
|
|
|
|
'BASE_URL': BASE_URL,
|
|
|
|
'last_date_li': last_date_li,
|
2010-07-08 14:53:25 +03:00
|
|
|
'info_area':'main',
|
2010-07-06 19:25:42 +03:00
|
|
|
})
|
|
|
|
def member_subscribe(request):
|
2010-07-08 14:53:25 +03:00
|
|
|
info_area = 'subscribe'
|
|
|
|
|
2010-07-06 19:25:42 +03:00
|
|
|
if request.method == 'POST':
|
|
|
|
form = ContactForm(request.POST, request.FILES)
|
|
|
|
#return HttpResponse(str(request.FILES))
|
|
|
|
if form.is_valid():
|
|
|
|
human = True
|
|
|
|
try:
|
|
|
|
check = handle_uploaded_file(request.FILES['hackergotchi'])
|
|
|
|
except MultiValueDictKeyError:
|
2010-07-10 01:11:14 +03:00
|
|
|
check = (False,False)
|
2010-07-06 19:25:42 +03:00
|
|
|
#save the author information
|
2010-07-10 01:11:14 +03:00
|
|
|
|
2010-07-06 19:25:42 +03:00
|
|
|
if check[0]:
|
|
|
|
f = request.FILES['hackergotchi']
|
|
|
|
|
|
|
|
#change the name of the file with the unique name created
|
|
|
|
f.name = check[1]
|
|
|
|
|
|
|
|
author = Authors(author_name=request.POST['name'], author_surname=request.POST['surname'], author_email=request.POST['email'], channel_url=request.POST['feed'], author_face=f.name, is_approved=0, current_status=5)
|
|
|
|
else:
|
|
|
|
author = Authors(author_name=request.POST['name'], author_surname=request.POST['surname'], author_email=request.POST['email'], channel_url=request.POST['feed'], is_approved=0, current_status=5)
|
2010-07-15 14:48:56 +03:00
|
|
|
|
2010-07-10 01:11:14 +03:00
|
|
|
author.save()
|
2010-07-06 19:25:42 +03:00
|
|
|
|
2010-07-10 01:11:14 +03:00
|
|
|
#save the history with explanation
|
|
|
|
author.history_set.create(action_type=5, action_date=datetime.datetime.now(), action_explanation=request.POST['message'])
|
2010-07-15 14:48:56 +03:00
|
|
|
|
2010-07-06 19:25:42 +03:00
|
|
|
#send mail part
|
|
|
|
#fill it here
|
2010-07-08 14:53:25 +03:00
|
|
|
return render_response(request, 'main/subscribe.html/',{'submit': 'done', 'BASE_URL': BASE_URL,'info_area':info_area})
|
2010-07-06 19:25:42 +03:00
|
|
|
else:
|
|
|
|
form = ContactForm()
|
2010-07-08 14:53:25 +03:00
|
|
|
return render_response(request, 'main/subscribe.html', {'form': form, 'BASE_URL': BASE_URL,'info_area':info_area})
|
2010-07-06 19:25:42 +03:00
|
|
|
|
|
|
|
def handle_uploaded_file(f):
|
|
|
|
|
|
|
|
if not f.name: return False
|
|
|
|
#lets create a unique name for the image
|
|
|
|
t = str(time.time()).split(".")
|
|
|
|
img_name = t[0] + t[1].f.name.split(".")[1]
|
|
|
|
f.name = img_name
|
|
|
|
path = os.path.join(settings.FILE_UPLOAD_TEMP_DIR, f.name)
|
|
|
|
|
|
|
|
destination = open(path, 'wb+')
|
|
|
|
for chunk in f.chunks():
|
|
|
|
destination.write(chunk)
|
|
|
|
destination.close()
|
|
|
|
|
|
|
|
m = magic.open(magic.MAGIC_MIME)
|
|
|
|
m.load()
|
|
|
|
t = m.file(path)
|
|
|
|
if t.split('/')[0] == 'image':
|
|
|
|
return (True, f.name)
|
|
|
|
else:
|
|
|
|
os.unlink(path)
|
|
|
|
return (False, '')
|
|
|
|
|
|
|
|
def list_members(request):
|
2010-07-08 14:53:25 +03:00
|
|
|
info_area = 'members'
|
2010-07-06 19:25:42 +03:00
|
|
|
|
|
|
|
authors = Authors.objects.all()
|
|
|
|
|
2010-07-08 14:53:25 +03:00
|
|
|
return render_response(request, 'main/members.html', {'members': authors, 'BASE_URL': BASE_URL,'info_area' : info_area })
|
2010-08-07 16:42:02 +03:00
|
|
|
|
2010-07-06 19:25:42 +03:00
|
|
|
def query(request):
|
2010-08-07 16:42:02 +03:00
|
|
|
# Determine if method is POST.
|
|
|
|
if (request.method == 'POST'):
|
|
|
|
## If Yes:
|
|
|
|
form = QueryForm(request.POST)
|
|
|
|
|
|
|
|
# Determine if all of them were valid.
|
|
|
|
if (form.is_valid()):
|
|
|
|
|
|
|
|
## If Yes:
|
|
|
|
|
|
|
|
q_author_name = request.POST['q_author_name']
|
|
|
|
q_author_surname = request.POST['q_author_surname']
|
|
|
|
q_text = request.POST['q_text']
|
|
|
|
q_date_year = request.POST['q_date_year']
|
|
|
|
q_date_month = request.POST['q_date_month']
|
|
|
|
q_date_day = request.POST['q_date_day']
|
2010-08-08 13:16:46 +03:00
|
|
|
# Redirect or call /archive/ view with the existing POST arguments.
|
|
|
|
#++ Complex string operations in order to form needed target_url.
|
2010-08-07 16:42:02 +03:00
|
|
|
args_part = "?q_author_name=%s&q_author_surname=%s&q_text=%s" % (q_author_name,q_author_surname,q_text)
|
|
|
|
date_part = ''
|
|
|
|
if (q_date_year):
|
|
|
|
date_part = q_date_year
|
|
|
|
if(q_date_month):
|
|
|
|
date_part += "/" + q_date_month
|
|
|
|
if(q_date_day):
|
|
|
|
date_part += "/" + q_date_day + "/"
|
|
|
|
target_url = BASE_URL+"/archive/" + date_part + args_part
|
|
|
|
#--
|
|
|
|
return HttpResponseRedirect(target_url)
|
|
|
|
else:
|
|
|
|
# Issue an error message and show the form again.
|
|
|
|
form = QueryForm()
|
|
|
|
info_area = "query"
|
|
|
|
|
|
|
|
return render_to_response('main/query.html', {'q_form': form, 'BASE_URL': BASE_URL,'info_area':info_area})
|
|
|
|
else:
|
|
|
|
# Show the form.
|
|
|
|
|
|
|
|
form = QueryForm()
|
|
|
|
info_area = "query"
|
|
|
|
|
|
|
|
return render_to_response('main/query.html', {'q_form': form, 'BASE_URL': BASE_URL,'info_area':info_area})
|
|
|
|
|
2010-08-05 12:00:09 +03:00
|
|
|
|
2010-08-16 10:40:40 +03:00
|
|
|
def archive(request,archive_year=None,archive_month=None,archive_day=None,label=None):
|
2010-08-08 16:29:29 +03:00
|
|
|
info_area = 'archive'
|
2010-07-06 19:25:42 +03:00
|
|
|
# This setting gets the content truncated which contains more than <truncate_words> words.
|
|
|
|
truncate_words = 250
|
|
|
|
items_per_page = 25
|
|
|
|
|
|
|
|
#get the last run time
|
|
|
|
run_time = RunTime.objects.all()[0]
|
|
|
|
|
2010-08-05 12:00:09 +03:00
|
|
|
# Now we are creating few scenarios depending on the incoming variables.
|
|
|
|
# This part is for validation.
|
|
|
|
if ('q_author_name' in request.GET and request.GET['q_author_name']): #If that exists and not empty
|
|
|
|
q_author_name = request.GET['q_author_name']
|
2010-07-06 19:25:42 +03:00
|
|
|
else:
|
2010-08-07 16:42:02 +03:00
|
|
|
q_author_name = ""
|
2010-08-05 12:00:09 +03:00
|
|
|
if ('q_author_surname' in request.GET and request.GET['q_author_surname']): #If that exists and not empty
|
|
|
|
q_author_surname = request.GET['q_author_surname']
|
|
|
|
else:
|
2010-08-07 16:42:02 +03:00
|
|
|
q_author_surname = ""
|
2010-08-05 12:00:09 +03:00
|
|
|
if ('q_text' in request.GET and request.GET['q_text']): #If that exists and not empty
|
|
|
|
q_text = request.GET['q_text']
|
|
|
|
else:
|
2010-08-07 16:42:02 +03:00
|
|
|
q_text = ""
|
2010-08-16 10:40:40 +03:00
|
|
|
if ((label == "personal") or 'q_label_personal' in request.GET and request.GET['q_label_personal'] == '1'): #If that exists and not empty
|
|
|
|
q_label_personal = 1
|
2010-08-05 12:00:09 +03:00
|
|
|
else:
|
2010-08-07 16:42:02 +03:00
|
|
|
q_label_personal = ""
|
2010-08-16 10:40:40 +03:00
|
|
|
if ((label == "community") or 'q_label_community' in request.GET and request.GET['q_label_community'] == '1'):
|
|
|
|
q_label_community = 1
|
2010-08-05 12:00:09 +03:00
|
|
|
else:
|
2010-08-07 16:42:02 +03:00
|
|
|
q_label_community = ""
|
2010-07-15 14:48:56 +03:00
|
|
|
|
2010-08-16 10:40:40 +03:00
|
|
|
if ((label == "lkd") or 'q_label_lkd' in request.GET and request.GET['q_label_lkd']=='1'):
|
|
|
|
q_label_lkd = 1
|
2010-08-05 12:00:09 +03:00
|
|
|
else:
|
2010-08-07 16:42:02 +03:00
|
|
|
q_label_lkd = ""
|
2010-08-16 10:40:40 +03:00
|
|
|
if ((label == "eng") or 'q_label_eng' in request.GET and request.GET['q_label_eng']=='1'):
|
|
|
|
q_label_eng = 1
|
|
|
|
else:
|
|
|
|
q_label_eng = ""
|
2010-08-05 12:00:09 +03:00
|
|
|
#--
|
|
|
|
#--
|
|
|
|
|
|
|
|
# Querying
|
|
|
|
entries_list = Entries.objects.select_related()
|
|
|
|
# Name - surname queries.
|
|
|
|
|
|
|
|
if(q_author_name):
|
|
|
|
entries_list = entries_list.filter(entry_id__author_name__iexact = q_author_name)
|
|
|
|
if(q_author_surname):
|
|
|
|
entries_list = entries_list.filter(entry_id__author_surname__iexact = q_author_surname)
|
|
|
|
elif(q_author_surname):
|
|
|
|
entries_list = entries_list.filter(entry_id__author_surname__iexact = q_author_surname)
|
|
|
|
|
2010-08-07 16:42:02 +03:00
|
|
|
# Label based queries.
|
2010-08-05 12:00:09 +03:00
|
|
|
if(q_label_personal):
|
|
|
|
entries_list = entries_list.filter(entry_id__label_personal = 1)
|
|
|
|
if(q_label_community):
|
|
|
|
entries_list = entries_list.filter(entry_id__label_community = 1)
|
|
|
|
if(q_label_lkd):
|
|
|
|
entries_list = entries_list.filter(entry_id__label_lkd = 1)
|
2010-08-16 10:40:40 +03:00
|
|
|
if(q_label_eng):
|
|
|
|
entries_list = entries_list.filter(entry_id__label_eng = 1)
|
|
|
|
|
2010-08-05 12:00:09 +03:00
|
|
|
|
|
|
|
|
2010-08-07 16:42:02 +03:00
|
|
|
# Text search.
|
2010-08-05 12:00:09 +03:00
|
|
|
if(q_text):
|
2010-08-07 16:42:02 +03:00
|
|
|
entries_list = entries_list.filter(content_text__icontains = q_text)
|
|
|
|
# Date based queries.
|
2010-08-05 12:00:09 +03:00
|
|
|
if(archive_year):
|
|
|
|
entries_list = entries_list.filter(date__year = archive_year)
|
|
|
|
if(archive_month):
|
|
|
|
entries_list = entries_list.filter(date__month = archive_month)
|
|
|
|
if(archive_day):
|
|
|
|
entries_list = entries_list.filter(date__day = archive_day)
|
|
|
|
|
|
|
|
#--
|
|
|
|
|
|
|
|
# Pagination
|
|
|
|
elements_in_a_page = 25 # This determines, how many elements will be displayed in a paginator page.
|
|
|
|
paginator = Paginator(entries_list,elements_in_a_page)
|
|
|
|
# Validation for page number if it is not int return first page.
|
|
|
|
try:
|
|
|
|
page = int(request.GET.get('page', '1'))
|
|
|
|
except ValueError:
|
|
|
|
page = 1
|
|
|
|
|
|
|
|
# If page request is out of range, return last page .
|
|
|
|
try:
|
|
|
|
p_entries_list = paginator.page(page)
|
|
|
|
except (EmptyPage, InvalidPage):
|
|
|
|
p_entries_list = paginator.page(paginator.num_pages)
|
|
|
|
#--
|
|
|
|
|
|
|
|
return render_to_response('main/archive.html' ,{
|
|
|
|
'entries_list':entries_list,
|
|
|
|
'p_entries_list':p_entries_list,
|
|
|
|
'truncate_words':truncate_words,
|
|
|
|
'items_per_page':repr(items_per_page),
|
|
|
|
'run_time':run_time,
|
|
|
|
'info_area':info_area,
|
2010-08-08 16:29:29 +03:00
|
|
|
'archive_year' : archive_year,
|
|
|
|
'archive_month' : archive_month,
|
|
|
|
'archive_day' : archive_day,
|
2010-08-05 12:00:09 +03:00
|
|
|
'q_author_name':q_author_name,
|
|
|
|
'q_author_surname':q_author_surname,
|
|
|
|
'q_text':q_text,
|
|
|
|
'BASE_URL':BASE_URL,
|
|
|
|
})
|
2010-07-06 19:25:42 +03:00
|
|
|
|
2010-08-07 16:42:02 +03:00
|
|
|
def issue_error(request,error_msg=None):
|
|
|
|
info_area = 'error'
|
|
|
|
if not(error_msg):
|
|
|
|
error_msg = 'Bilinmeyen bir hata oluştu!'
|
|
|
|
return render_response(request, 'main/error.html', {'error_msg': error_msg,'info_area' : info_area })
|