-
Notifications
You must be signed in to change notification settings - Fork 155
/
settings.py
120 lines (102 loc) · 4.77 KB
/
settings.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
119
120
from flask import session
class Settings:
def __init__(self, default_config):
self.collection = default_config['SETTINGS_COLLECTION']
self.config = default_config
self.config['PER_PAGE'] = 15
self.config['SEARCH'] = False
self.config['BLOG_TITLE'] = 'Blog'
self.config['BLOG_DESCRIPTION'] = ''
self.response = {'error': None, 'data': None}
self.debug_mode = default_config['DEBUG']
def get_config(self):
try:
cursor = self.collection.find_one()
if cursor:
self.config['PER_PAGE'] = cursor.get(
'per_page', self.config['PER_PAGE'])
self.config['SEARCH'] = cursor.get(
'use_search', self.config['SEARCH'])
self.config['BLOG_TITLE'] = cursor.get(
'title', self.config['BLOG_TITLE'])
self.config['BLOG_DESCRIPTION'] = cursor.get(
'description', self.config['BLOG_DESCRIPTION'])
return self.config
except Exception, e:
self.print_debug_info(e, self.debug_mode)
self.response['error'] = 'System error..'
def is_installed(self):
posts_cnt = self.config['POSTS_COLLECTION'].find().count()
users_cnt = self.config['USERS_COLLECTION'].find().count()
configs_cnt = self.config['SETTINGS_COLLECTION'].find().count()
if posts_cnt and users_cnt and configs_cnt:
session['installed'] = True
return True
else:
session['installed'] = False
return False
def install(self, blog_data, user_data):
import user
import post
userClass = user.User(self.config)
postClass = post.Post(self.config)
self.response['error'] = None
try:
self.config['POSTS_COLLECTION'].ensure_index([('date', -1)])
self.config['POSTS_COLLECTION'].ensure_index(
[('tags', 1), ('date', -1)])
self.config['POSTS_COLLECTION'].ensure_index([('permalink', 1)])
self.config['POSTS_COLLECTION'].ensure_index(
[('query', 1), ('orderby', 1)])
self.config['USERS_COLLECTION'].ensure_index([('date', 1)])
post_data = {'title': 'Hello World!',
'preview': 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod',
'body': 'tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam',
'tags': [],
'author': user_data['_id']}
post = postClass.validate_post_data(post_data)
user_create = userClass.save_user(user_data)
post_create = postClass.create_new_post(post)
if blog_data['per_page'].isdigit():
blog_settings_error = None
self.collection.insert(blog_data)
else:
blog_settings_error = '"Per page" field need to be integer..'
if user_create['error'] or post_create['error'] or blog_settings_error:
self.response['error'] = []
self.response['error'].append(user_create['error'])
self.response['error'].append(post_create['error'])
self.response['error'].append(blog_settings_error)
self.config['POSTS_COLLECTION'].drop()
self.config['USERS_COLLECTION'].drop()
self.collection.drop()
return self.response
except Exception, e:
self.print_debug_info(e, self.debug_mode)
self.response['error'] = 'Installation error..'
def update_settings(self, data):
self.response['error'] = None
try:
cursor = self.collection.find_one()
self.collection.update(
{'_id': cursor['_id']}, {'$set': data}, upsert=False, multi=False)
self.response['data'] = True
return self.response
except Exception, e:
self.print_debug_info(e, self.debug_mode)
self.response['error'] = 'Settings update error..'
@staticmethod
def print_debug_info(msg, show=False):
if show:
import sys
import os
error_color = '\033[32m'
error_end = '\033[0m'
error = {'type': sys.exc_info()[0].__name__,
'file': os.path.basename(sys.exc_info()[2].tb_frame.f_code.co_filename),
'line': sys.exc_info()[2].tb_lineno,
'details': str(msg)}
print error_color
print '\n\n---\nError type: %s in file: %s on line: %s\nError details: %s\n---\n\n'\
% (error['type'], error['file'], error['line'], error['details'])
print error_end