Skip to content

Commit

Permalink
Pylint updates
Browse files Browse the repository at this point in the history
  • Loading branch information
horilla-opensource committed Feb 14, 2025
1 parent 8552588 commit 07e9c23
Show file tree
Hide file tree
Showing 16 changed files with 269 additions and 228 deletions.
1 change: 0 additions & 1 deletion horilla/locale/pt-br/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -25608,4 +25608,3 @@ msgstr "Progresso"

#~ msgid "individual"
#~ msgstr "Creation"

2 changes: 1 addition & 1 deletion horilla_backup/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
default_app_config = 'horilla_backup.apps.backupConfig'
default_app_config = "horilla_backup.apps.backupConfig"
3 changes: 1 addition & 2 deletions horilla_backup/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from django.contrib import admin

from .models import *

# Register your models here.

admin.site.register(LocalBackup)
admin.site.register(GoogleDriveBackup)


11 changes: 6 additions & 5 deletions horilla_backup/apps.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from django.apps import AppConfig
from django.apps import AppConfig


class BackupConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'horilla_backup'
default_auto_field = "django.db.models.BigAutoField"
name = "horilla_backup"

def ready(self):
from django.urls import include, path

from horilla.urls import urlpatterns

urlpatterns.append(
path("backup/", include("horilla_backup.urls")),
)
super().ready()

176 changes: 91 additions & 85 deletions horilla_backup/forms.py
Original file line number Diff line number Diff line change
@@ -1,93 +1,105 @@
import os
from pathlib import Path

from django import forms
from .models import *
from base.forms import ModelForm
from django.template.loader import render_to_string
from django.core.exceptions import ValidationError
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from django.template.loader import render_to_string
from django.utils.translation import gettext_lazy as _
from pathlib import Path

from base.forms import ModelForm

from .gdrive import authenticate
from django.core.files.storage import default_storage
from django.core.files.base import ContentFile
import os
from .models import *


class LocalBackupSetupForm(ModelForm):
verbose_name = "Server Backup"
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.")
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."
)

class Meta:
model = LocalBackup
exclude = ['active']
exclude = ["active"]


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



def clean(self):
cleaned_data = super().clean()
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')
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")
path = Path(backup_path)
if not path.exists():
raise ValidationError({
'backup_path': _('The directory does not exist.')
})
raise ValidationError({"backup_path": _("The directory does not exist.")})
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.")
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.')
})
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.")})
if seconds:
if seconds < 0:
raise ValidationError({
'seconds': _('Negative value is not accepatable.')
})
raise ValidationError(
{"seconds": _("Negative value is not accepatable.")}
)
if hour:
if hour < 0 or hour > 24:
raise ValidationError({
'hour': _('Enter a hour between 0 to 24.')
})
if minute:
raise ValidationError({"hour": _("Enter a hour between 0 to 24.")})
if minute:
if minute < 0 or minute > 60:
raise ValidationError({
'minute': _('Enter a minute between 0 to 60.')
})
return cleaned_data

raise ValidationError({"minute": _("Enter a minute between 0 to 60.")})
return cleaned_data


class GdriveBackupSetupForm(ModelForm):
verbose_name = "Gdrive Backup"
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.")
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."
)

class Meta:
model = GoogleDriveBackup
exclude = ['active']

exclude = ["active"]

def as_p(self):
"""
Expand All @@ -96,60 +108,54 @@ def as_p(self):
context = {"form": self}
table_html = render_to_string("common_form.html", context)
return table_html

def clean(self):
cleaned_data = super().clean()
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')
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")

try:
if GoogleDriveBackup.objects.exists():
authenticate(service_account_file.path)
else:
file_data = service_account_file.read()
# Save the processed file to the desired location
file_name = service_account_file.name
new_file_name = file_name
new_file_name = file_name
# Save using Django's default storage system
relative_path = default_storage.save(new_file_name, ContentFile(file_data))
relative_path = default_storage.save(
new_file_name, ContentFile(file_data)
)
# Get the full absolute path
full_path = default_storage.path(relative_path)
full_path = default_storage.path(relative_path)
authenticate(full_path)
os.remove(full_path)

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.")
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.')
})
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.")})
if seconds:
if seconds < 0:
raise ValidationError({
'seconds': _('Negative value is not accepatable.')
})
raise ValidationError(
{"seconds": _("Negative value is not accepatable.")}
)
if hour:
if hour < 0 or hour > 24:
raise ValidationError({
'hour': _('Enter a hour between 0 to 24.')
})
if minute:
raise ValidationError({"hour": _("Enter a hour between 0 to 24.")})
if minute:
if minute < 0 or minute > 60:
raise ValidationError({
'minute': _('Enter a minute between 0 to 60.')
})
return cleaned_data
raise ValidationError({"minute": _("Enter a minute between 0 to 60.")})
return cleaned_data
37 changes: 18 additions & 19 deletions horilla_backup/gdrive.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
from googleapiclient.discovery import build
import os

from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
import os


SCOPES = ['https://www.googleapis.com/auth/drive']


SCOPES = ["https://www.googleapis.com/auth/drive"]


def authenticate(service_account_file):
creds = service_account.Credentials.from_service_account_file(service_account_file, scopes=SCOPES)
creds = service_account.Credentials.from_service_account_file(
service_account_file, scopes=SCOPES
)
return creds

def upload_file(file_path, service_account_file, parent_folder_id):

def upload_file(file_path, service_account_file, parent_folder_id):
creds = authenticate(service_account_file)
service = build('drive', 'v3', credentials=creds)
service = build("drive", "v3", credentials=creds)
parent_folder_id = parent_folder_id

file_metadata = {
'name' : os.path.basename(file_path),
'parents' : [parent_folder_id]
}
file_metadata = {"name": os.path.basename(file_path), "parents": [parent_folder_id]}
media = MediaFileUpload(file_path, resumable=True)
file = service.files().create(
body=file_metadata,
media_body=media,
fields='id'
).execute()
file = (
service.files()
.create(body=file_metadata, media_body=media, fields="id")
.execute()
)
6 changes: 4 additions & 2 deletions horilla_backup/migrations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@

def shutdown_function():
from horilla_backup.models import GoogleDriveBackup, LocalBackup

if GoogleDriveBackup.objects.exists():
google_drive_backup = GoogleDriveBackup.objects.first()
google_drive_backup.active = False
google_drive_backup.active = False
google_drive_backup.save()
if LocalBackup.objects.exists():
local_backup = LocalBackup.objects.first()
local_backup.active = False
local_backup.save()


try:
atexit.register(shutdown_function)
except:
pass
pass
Loading

0 comments on commit 07e9c23

Please sign in to comment.