2025-02-14 10:01:07 +05:30
|
|
|
import os
|
2025-07-02 12:27:03 +05:30
|
|
|
import tempfile
|
2025-02-14 10:01:07 +05:30
|
|
|
from pathlib import Path
|
|
|
|
|
|
2025-02-13 17:14:54 +05:30
|
|
|
from django import forms
|
|
|
|
|
from django.core.exceptions import ValidationError
|
2025-02-14 10:01:07 +05:30
|
|
|
from django.core.files.base import ContentFile
|
|
|
|
|
from django.core.files.storage import default_storage
|
|
|
|
|
from django.template.loader import render_to_string
|
2025-02-13 17:14:54 +05:30
|
|
|
from django.utils.translation import gettext_lazy as _
|
2025-02-14 10:01:07 +05:30
|
|
|
|
|
|
|
|
from base.forms import ModelForm
|
|
|
|
|
|
2025-02-13 17:14:54 +05:30
|
|
|
from .gdrive import authenticate
|
2025-02-14 10:01:07 +05:30
|
|
|
from .models import *
|
2025-02-13 17:14:54 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
class LocalBackupSetupForm(ModelForm):
|
|
|
|
|
verbose_name = "Server Backup"
|
2025-02-14 10:01:07 +05:30
|
|
|
backup_db = forms.BooleanField(
|
|
|
|
|
required=False, help_text="Enable to backup database to server."
|
|
|
|
|
)
|
|
|
|
|
backup_media = forms.BooleanField(
|
|
|
|
|
required=False, help_text="Enable to backup all media files to server."
|
|
|
|
|
)
|
|
|
|
|
interval = forms.BooleanField(
|
|
|
|
|
required=False,
|
|
|
|
|
help_text="Enable to automate the backup in a period of seconds.",
|
|
|
|
|
)
|
|
|
|
|
fixed = forms.BooleanField(
|
|
|
|
|
required=False, help_text="Enable to automate the backup in a fixed time."
|
|
|
|
|
)
|
2025-02-13 17:14:54 +05:30
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = LocalBackup
|
2025-02-14 10:01:07 +05:30
|
|
|
exclude = ["active"]
|
2025-02-13 17:14:54 +05:30
|
|
|
|
|
|
|
|
def as_p(self):
|
|
|
|
|
"""
|
|
|
|
|
Render the form fields as HTML table rows with Bootstrap styling.
|
|
|
|
|
"""
|
|
|
|
|
context = {"form": self}
|
|
|
|
|
table_html = render_to_string("common_form.html", context)
|
|
|
|
|
return table_html
|
2025-02-14 10:01:07 +05:30
|
|
|
|
2025-02-13 17:14:54 +05:30
|
|
|
def clean(self):
|
|
|
|
|
cleaned_data = super().clean()
|
2025-02-14 10:01:07 +05:30
|
|
|
backup_db = cleaned_data.get("backup_db")
|
|
|
|
|
backup_media = cleaned_data.get("backup_media")
|
|
|
|
|
interval = cleaned_data.get("interval")
|
|
|
|
|
fixed = cleaned_data.get("fixed")
|
|
|
|
|
seconds = cleaned_data.get("seconds")
|
|
|
|
|
hour = cleaned_data.get("hour")
|
|
|
|
|
minute = cleaned_data.get("minute")
|
|
|
|
|
backup_path = cleaned_data.get("backup_path")
|
2025-02-13 17:14:54 +05:30
|
|
|
path = Path(backup_path)
|
|
|
|
|
if not path.exists():
|
2025-02-14 10:01:07 +05:30
|
|
|
raise ValidationError({"backup_path": _("The directory does not exist.")})
|
2025-02-13 17:14:54 +05:30
|
|
|
if backup_db == False and backup_media == False:
|
|
|
|
|
raise forms.ValidationError("Please select any backup option.")
|
|
|
|
|
if interval == False and fixed == False:
|
|
|
|
|
raise forms.ValidationError("Please select any backup automate option.")
|
2025-02-14 10:01:07 +05:30
|
|
|
if interval == True and seconds == None:
|
|
|
|
|
raise ValidationError({"seconds": _("This field is required.")})
|
|
|
|
|
if fixed == True and hour == None:
|
|
|
|
|
raise ValidationError({"hour": _("This field is required.")})
|
2025-02-13 17:14:54 +05:30
|
|
|
if seconds:
|
|
|
|
|
if seconds < 0:
|
2025-02-14 10:01:07 +05:30
|
|
|
raise ValidationError(
|
|
|
|
|
{"seconds": _("Negative value is not accepatable.")}
|
|
|
|
|
)
|
2025-02-13 17:14:54 +05:30
|
|
|
if hour:
|
|
|
|
|
if hour < 0 or hour > 24:
|
2025-02-14 10:01:07 +05:30
|
|
|
raise ValidationError({"hour": _("Enter a hour between 0 to 24.")})
|
|
|
|
|
if minute:
|
2025-02-13 17:14:54 +05:30
|
|
|
if minute < 0 or minute > 60:
|
2025-02-14 10:01:07 +05:30
|
|
|
raise ValidationError({"minute": _("Enter a minute between 0 to 60.")})
|
|
|
|
|
return cleaned_data
|
|
|
|
|
|
2025-02-13 17:14:54 +05:30
|
|
|
|
|
|
|
|
class GdriveBackupSetupForm(ModelForm):
|
|
|
|
|
verbose_name = "Gdrive Backup"
|
2025-02-14 10:01:07 +05:30
|
|
|
backup_db = forms.BooleanField(
|
|
|
|
|
required=False,
|
|
|
|
|
label="Backup DB",
|
|
|
|
|
help_text="Enable to backup database to Gdrive",
|
|
|
|
|
)
|
|
|
|
|
backup_media = forms.BooleanField(
|
|
|
|
|
required=False,
|
|
|
|
|
label="Backup Media",
|
|
|
|
|
help_text="Enable to backup all media files to Gdrive",
|
|
|
|
|
)
|
|
|
|
|
interval = forms.BooleanField(
|
|
|
|
|
required=False,
|
|
|
|
|
help_text="Enable to automate the backup in a period of seconds.",
|
|
|
|
|
)
|
|
|
|
|
fixed = forms.BooleanField(
|
|
|
|
|
required=False, help_text="Enable to automate the backup in a fixed time."
|
|
|
|
|
)
|
2025-02-13 17:14:54 +05:30
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = GoogleDriveBackup
|
2025-02-14 10:01:07 +05:30
|
|
|
exclude = ["active"]
|
2025-02-13 17:14:54 +05:30
|
|
|
|
|
|
|
|
def as_p(self):
|
|
|
|
|
"""
|
|
|
|
|
Render the form fields as HTML table rows with Bootstrap styling.
|
|
|
|
|
"""
|
|
|
|
|
context = {"form": self}
|
|
|
|
|
table_html = render_to_string("common_form.html", context)
|
|
|
|
|
return table_html
|
2025-02-14 10:01:07 +05:30
|
|
|
|
2025-02-13 17:14:54 +05:30
|
|
|
def clean(self):
|
|
|
|
|
cleaned_data = super().clean()
|
2025-02-14 10:01:07 +05:30
|
|
|
backup_db = cleaned_data.get("backup_db")
|
|
|
|
|
backup_media = cleaned_data.get("backup_media")
|
|
|
|
|
interval = cleaned_data.get("interval")
|
|
|
|
|
fixed = cleaned_data.get("fixed")
|
|
|
|
|
seconds = cleaned_data.get("seconds")
|
|
|
|
|
hour = cleaned_data.get("hour")
|
|
|
|
|
minute = cleaned_data.get("minute")
|
|
|
|
|
service_account_file = cleaned_data.get("service_account_file")
|
|
|
|
|
|
2025-02-13 17:14:54 +05:30
|
|
|
try:
|
2025-07-02 12:27:03 +05:30
|
|
|
# Read file content from InMemoryUploadedFile or whatever you receive
|
|
|
|
|
file_data = service_account_file.read()
|
|
|
|
|
file_name = service_account_file.name
|
|
|
|
|
|
|
|
|
|
# Save using Django's storage (optional, if you need to persist it later)
|
|
|
|
|
if not GoogleDriveBackup.objects.exists():
|
|
|
|
|
# Save to storage if no backup exists
|
|
|
|
|
relative_path = default_storage.save(file_name, ContentFile(file_data))
|
|
|
|
|
|
|
|
|
|
# Always write to temp file for authentication (because .path isn't supported)
|
|
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".json") as tmp_file:
|
|
|
|
|
tmp_file.write(file_data)
|
|
|
|
|
tmp_file.flush()
|
|
|
|
|
temp_path = tmp_file.name
|
|
|
|
|
|
|
|
|
|
# Authenticate using temp file path
|
|
|
|
|
authenticate(temp_path)
|
|
|
|
|
|
|
|
|
|
# Clean up temp file
|
|
|
|
|
os.remove(temp_path)
|
2025-02-14 10:01:07 +05:30
|
|
|
|
2025-02-13 17:14:54 +05:30
|
|
|
except Exception as e:
|
|
|
|
|
raise forms.ValidationError("Please provide a valid service account file.")
|
|
|
|
|
if backup_db == False and backup_media == False:
|
|
|
|
|
raise forms.ValidationError("Please select any backup option.")
|
|
|
|
|
if interval == False and fixed == False:
|
|
|
|
|
raise forms.ValidationError("Please select any backup automate option.")
|
2025-02-14 10:01:07 +05:30
|
|
|
if interval == True and seconds == None:
|
|
|
|
|
raise ValidationError({"seconds": _("This field is required.")})
|
|
|
|
|
if fixed == True and hour == None:
|
|
|
|
|
raise ValidationError({"hour": _("This field is required.")})
|
2025-02-13 17:14:54 +05:30
|
|
|
if seconds:
|
|
|
|
|
if seconds < 0:
|
2025-02-14 10:01:07 +05:30
|
|
|
raise ValidationError(
|
|
|
|
|
{"seconds": _("Negative value is not accepatable.")}
|
|
|
|
|
)
|
2025-02-13 17:14:54 +05:30
|
|
|
if hour:
|
|
|
|
|
if hour < 0 or hour > 24:
|
2025-02-14 10:01:07 +05:30
|
|
|
raise ValidationError({"hour": _("Enter a hour between 0 to 24.")})
|
|
|
|
|
if minute:
|
2025-02-13 17:14:54 +05:30
|
|
|
if minute < 0 or minute > 60:
|
2025-02-14 10:01:07 +05:30
|
|
|
raise ValidationError({"minute": _("Enter a minute between 0 to 60.")})
|
|
|
|
|
return cleaned_data
|