-
Notifications
You must be signed in to change notification settings - Fork 50
/
views.py
120 lines (97 loc) · 3.95 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from django.shortcuts import render, get_object_or_404, HttpResponse
from django.http import JsonResponse
from django.core import serializers
import json
from django.db.models import Q
from decouple import config
from django.core.mail import send_mail
from django.conf import settings
from info.forms import MessageForm
from info.models import (
Competence,
Education,
Experience,
Project,
Information,
Message
)
def email_send(data):
old_message = Message.objects.last()
if old_message.name == data['name'] and old_message.email == data['email'] and old_message.message == data['message']:
return False
subject = 'Portfolio : Mail from {}'.format(data['name'])
message = '{}\nSender Email: {}'.format(data['message'], data['email'])
email_from = settings.EMAIL_HOST_USER
recipient_list = [settings.EMAIL_HOST_USER, ]
send_mail(subject, message, email_from, recipient_list)
return True
def homePage(request):
template_name = 'homePage.html'
context = {}
if request.method == 'POST':
if request.POST.get('rechaptcha', None):
form = MessageForm(request.POST)
if form.is_valid():
form.save(commit=False)
data = {
'name': request.POST['name'],
'email': request.POST['email'],
'message': request.POST['message']
}
if email_send(data):
form.save()
return JsonResponse({'success': True})
else:
return JsonResponse({'success': False, 'errors': form.errors})
return JsonResponse({'success': False, 'errors': "Oops, you have to check the recaptcha !"})
if request.method == 'GET':
form = MessageForm()
competences = Competence.objects.all().order_by('id')
education = Education.objects.all().order_by('-id')
experiences = Experience.objects.all().order_by('-id')
projects = Project.objects.filter(show_in_slider=True).order_by('-id')
info = Information.objects.first()
context = {
'info': info,
'competences': competences,
'education': education,
'experiences': experiences,
'projects': projects,
'form': form,
'recaptcha_key': config("recaptcha_site_key", default="")
}
return render(request, template_name, context)
def projectsPage(request):
template_name = 'projects/projects_page.html'
if request.method == 'GET':
projects = Project.objects.all().order_by('-id')
context = {
'projects': projects
}
return render(request, template_name, context)
def projectDetail(request, slug):
template_name = 'projects/project_detail.html'
if request.method == 'GET':
project = get_object_or_404(Project, slug=slug)
return render(request, template_name, {'project': project})
def search(request):
if request.method == 'POST':
search_text = request.POST.get('searchText', False)
if search_text:
lookups = Q(title__icontains=search_text) | Q(
description__icontains=search_text) | Q(tools__icontains=search_text)
objs = Project.objects.filter(lookups)
if objs:
projects = Project.objects.filter(lookups).values()
projects = list(projects)
for project, obj in zip(projects, objs):
project.update({
'url': obj.get_project_absolute_url(),
'image_url': obj.image.url
})
return JsonResponse({'success': True, 'projects': projects, 'searchText': search_text})
return JsonResponse({'success': False, 'searchText': search_text})
def handler404(request, exception):
return render(request, 'errors/404.html', status=404)
def test404(request):
return render(request, 'errors/404.html')