initial
This commit is contained in:
0
contact/__init__.py
Normal file
0
contact/__init__.py
Normal file
5
contact/admin.py
Normal file
5
contact/admin.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.contrib import admin
|
||||
from contact.models import *
|
||||
|
||||
admin.site.register(Contact)
|
||||
admin.site.register(contactPageSEO)
|
||||
6
contact/apps.py
Normal file
6
contact/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ContactConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'contact'
|
||||
28
contact/forms.py
Normal file
28
contact/forms.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from django import forms
|
||||
from contact.models import *
|
||||
|
||||
# # # # # # # # # # # # # # # # # #
|
||||
# Contact Form #
|
||||
# # # # # # # # # # # # # # # # # #
|
||||
class ContactForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Contact
|
||||
fields = '__all__'
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
for field in self.fields.values():
|
||||
field.widget.attrs['class'] = 'form-control'
|
||||
|
||||
# # # # # # # # # # # # # # # # # #
|
||||
# Contact Page SEO Form #
|
||||
# # # # # # # # # # # # # # # # # #
|
||||
class contactPageSEOForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = contactPageSEO
|
||||
fields = '__all__'
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
for field in self.fields.values():
|
||||
field.widget.attrs['class'] = 'form-control'
|
||||
25
contact/migrations/0001_initial.py
Normal file
25
contact/migrations/0001_initial.py
Normal file
@@ -0,0 +1,25 @@
|
||||
# Generated by Django 4.2.4 on 2023-09-13 13:20
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Contact',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=100)),
|
||||
('email', models.EmailField(max_length=254)),
|
||||
('phone', models.CharField(max_length=100)),
|
||||
('subject', models.CharField(max_length=100)),
|
||||
('message', models.TextField()),
|
||||
],
|
||||
),
|
||||
]
|
||||
21
contact/migrations/0002_contactpageseo.py
Normal file
21
contact/migrations/0002_contactpageseo.py
Normal file
@@ -0,0 +1,21 @@
|
||||
# Generated by Django 4.2.4 on 2023-09-17 09:13
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('contact', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='contactPageSEO',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('meta_title', models.CharField(blank=True, max_length=500, null=True)),
|
||||
('meta_description', models.CharField(blank=True, max_length=1000, null=True)),
|
||||
],
|
||||
),
|
||||
]
|
||||
20
contact/migrations/0003_contact_created_at.py
Normal file
20
contact/migrations/0003_contact_created_at.py
Normal file
@@ -0,0 +1,20 @@
|
||||
# Generated by Django 4.2.4 on 2023-09-20 08:03
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.utils.timezone
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('contact', '0002_contactpageseo'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='contact',
|
||||
name='created_at',
|
||||
field=models.DateField(auto_now_add=True, default=django.utils.timezone.now),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
||||
18
contact/migrations/0004_alter_contact_phone.py
Normal file
18
contact/migrations/0004_alter_contact_phone.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.4 on 2023-10-18 05:15
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('contact', '0003_contact_created_at'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='contact',
|
||||
name='phone',
|
||||
field=models.CharField(blank=True, max_length=100, null=True),
|
||||
),
|
||||
]
|
||||
0
contact/migrations/__init__.py
Normal file
0
contact/migrations/__init__.py
Normal file
25
contact/models.py
Normal file
25
contact/models.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from django.db import models
|
||||
|
||||
# # # # # # # # # # # # # # # # # #
|
||||
# Contact Model #
|
||||
# # # # # # # # # # # # # # # # # #
|
||||
class Contact(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
email = models.EmailField()
|
||||
phone = models.CharField(max_length=100, blank=True, null=True)
|
||||
subject = models.CharField(max_length=100)
|
||||
message = models.TextField()
|
||||
created_at = models.DateField(auto_now_add=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
# # # # # # # # # # # # # # # # # #
|
||||
# Contact Page SEO #
|
||||
# # # # # # # # # # # # # # # # # #
|
||||
class contactPageSEO(models.Model):
|
||||
meta_title = models.CharField(max_length=500, blank=True, null=True)
|
||||
meta_description = models.CharField(max_length=1000, blank=True, null=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.meta_title
|
||||
3
contact/tests.py
Normal file
3
contact/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
7
contact/urls.py
Normal file
7
contact/urls.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from django.urls import path
|
||||
from contact.views import *
|
||||
|
||||
urlpatterns = [
|
||||
path('contact-us/', contactPageFront, name='contactPageFront'),
|
||||
path('contact/', ContactSubmit, name='ContactSubmit'),
|
||||
]
|
||||
56
contact/views.py
Normal file
56
contact/views.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from django.shortcuts import render, redirect, get_object_or_404
|
||||
from contact.models import *
|
||||
from contact.forms import *
|
||||
from django.utils import timezone
|
||||
from datetime import datetime
|
||||
from django.http import JsonResponse
|
||||
|
||||
# # # # # # # # # # # # # # # # # #
|
||||
# Contact Page Front #
|
||||
# # # # # # # # # # # # # # # # # #
|
||||
def contactPageFront(request):
|
||||
seo = contactPageSEO.objects.first()
|
||||
|
||||
context = {
|
||||
'seo' : seo,
|
||||
}
|
||||
return render(request, 'front/main/contact.html', context)
|
||||
|
||||
# # # # # # # # # # # # # # # # # #
|
||||
# Contact Submit #
|
||||
# # # # # # # # # # # # # # # # # #
|
||||
def ContactSubmit(request):
|
||||
if request.method == 'POST' and request.headers.get('x-requested-with') == 'XMLHttpRequest':
|
||||
# Check if there's a session variable for the last submission time
|
||||
last_submission_time_str = request.session.get('last_contact_submission_time')
|
||||
|
||||
if last_submission_time_str:
|
||||
# Convert the stored time string back to a datetime object
|
||||
last_submission_time = datetime.fromisoformat(last_submission_time_str)
|
||||
|
||||
# Calculate the time difference between the last submission and the current time
|
||||
time_difference = timezone.now() - last_submission_time
|
||||
|
||||
# Check if less than five minutes have passed since the last submission
|
||||
if time_difference.total_seconds() < 300: # 300 seconds = 5 minutes
|
||||
# Calculate the time left for the next submission
|
||||
time_left_seconds = 300 - time_difference.total_seconds()
|
||||
minutes_left = int(time_left_seconds / 60)
|
||||
seconds_left = int(time_left_seconds % 60)
|
||||
|
||||
return JsonResponse({'status': 'error', 'message': f'You can submit again in {minutes_left} minutes and {seconds_left} seconds.'})
|
||||
|
||||
# Save the current time in the session as a string in ISO 8601 format
|
||||
request.session['last_contact_submission_time'] = timezone.now().isoformat()
|
||||
|
||||
form = ContactForm(request.POST)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
return JsonResponse({'status': 'success', 'message': 'Contact form submitted successfully! We will get back to you soon.'})
|
||||
else:
|
||||
return JsonResponse({'status': 'error', 'message': 'Invalid submit! Try again.'})
|
||||
|
||||
return JsonResponse({'status': 'error', 'message': 'Invalid method or not an AJAX request!'})
|
||||
|
||||
def error_404(request, exception):
|
||||
return render(request, 'error/404.html', status=404)
|
||||
Reference in New Issue
Block a user