Skip to content
This repository was archived by the owner on May 23, 2020. It is now read-only.

Commit 52fb086

Browse files
committed
added autobackup to json file
1 parent e7a409b commit 52fb086

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

autobackupjsondb/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[^.]*

blog/admin.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@
1313
from blog.models import *
1414

1515

16+
class AuthorResource(resources.ModelResource):
17+
18+
class Meta:
19+
model = Author
20+
21+
1622
class AuthorAdmin(admin.ModelAdmin):
23+
resource_class = AuthorResource
1724
list_display = ('user', 'website', 'about')
1825
search_fields = ['user__username', 'user__email', 'about']
1926
list_filter = ['user__is_active', 'user__is_staff', 'user__is_superuser']
@@ -83,7 +90,14 @@ class PostAdmin(ImportExportModelAdmin, admin.ModelAdmin):
8390
list_per_page = 20
8491

8592

93+
class PageResource(resources.ModelResource):
94+
95+
class Meta:
96+
model = Page
97+
98+
8699
class PageAdmin(admin.ModelAdmin):
100+
resource_class = PageResource
87101
list_display = ('title', 'author', 'created', 'modified', 'publish')
88102
prepopulated_fields = {'slug': ('title',)}
89103
search_fields = ['title', 'description', 'author__user__username']

blog/management/__init__.py

Whitespace-only changes.

blog/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import os
2+
import json
3+
import time
4+
from django.core.management.base import BaseCommand, CommandError
5+
6+
from blog.admin import (
7+
AuthorResource, TagResource, PostResource,
8+
GalleryResource, VisitorResource, PageResource
9+
)
10+
from blog.models import Post
11+
12+
'''
13+
Refferences:
14+
- http://stackoverflow.com/a/11789141/3445802
15+
- http://stackoverflow.com/a/3287063/6396981
16+
See alseo:
17+
- https://docs.djangoproject.com/es/1.9/howto/custom-management-commands/
18+
19+
*) If you work with `hosting`, you can setup on `Cron Jobs`,
20+
and setup your time with command:
21+
source /path/to/yourenv/bin/activate && cd /path/to/yourenv/yourproject && ./manage.py autobackup yes
22+
23+
24+
*) But, if you work with VPS or SERVER, please following this command bellow:
25+
$ sudo crontab -e
26+
27+
# Setup to daily method.
28+
[minute] [hour] [date] [month] [year]
29+
30+
59 23 * * * source /path/to/yourenv/bin/activate && cd /path/to/yourenv/yourproject && ./manage.py autobackup yes
31+
32+
'''
33+
34+
35+
class Command(BaseCommand):
36+
help = 'To backup the blog using django-import-export!'
37+
38+
def add_arguments(self, parser):
39+
parser.add_argument(
40+
'backup',
41+
help='To backup the blog using django-import-export!'
42+
)
43+
44+
def handle(self, *args, **options):
45+
if options['backup'] == 'yes':
46+
47+
# Checking directory backup, exist or yet!
48+
directory_backup = "autobackupjsondb"
49+
if os.path.isdir(directory_backup) == False:
50+
os.makedirs(directory_backup)
51+
52+
def backupMixin(resources_set, fname):
53+
filepath = '{}/{}.json'.format(
54+
directory_backup,
55+
fname
56+
)
57+
with open(filepath, 'w') as outfile:
58+
json.dump(resources_set.json, outfile)
59+
60+
flastbackup = directory_backup + \
61+
'/last_backup_date_{}'.format(time.strftime("%d-%m-%Y"))
62+
with open(flastbackup, 'w') as f:
63+
f.write('Just for date info!')
64+
f.close()
65+
66+
authorset = AuthorResource().export()
67+
backupMixin(authorset, 'author')
68+
69+
tagset = TagResource().export()
70+
backupMixin(tagset, 'tag')
71+
72+
postset = PostResource().export()
73+
backupMixin(postset, 'post')
74+
75+
galleryset = GalleryResource().export()
76+
backupMixin(galleryset, 'gallery')
77+
78+
visitorset = VisitorResource().export()
79+
backupMixin(visitorset, 'visitor')
80+
81+
pageset = PageResource().export()
82+
backupMixin(pageset, 'page')
83+
84+
self.stdout.write(self.style.SUCCESS('[+] Successfully backup blog!'))
85+
else:
86+
self.stdout.write(self.style.WARNING('[-] Can not backup blog!'))

0 commit comments

Comments
 (0)