-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlogApp.py
183 lines (152 loc) · 6.13 KB
/
BlogApp.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python3
from cement.core.foundation import CementApp
from cement.core.controller import CementBaseController, expose
from cement.core import handler
from pprint import pprint as pp
from rmodel import *
from peewee import *
# define application controllers
class BlogAppBaseController(CementBaseController):
class Meta:
label = 'base'
description = "BlogApp is primitive blog control CLI app ."
# arguments = [
#(['--base-opt'], dict(help="option under base controller")),
# ]
@expose(help="base controller default command", hide=True)
def default(self):
info_msg = "BlogApp - a basic blog control tool."
banner = '#' + '@' * (len(info_msg) - 2) + '#'
border = '|' + ' ' * (len(info_msg) - 2) + '|'
lines = [banner, border, info_msg, border, banner]
disp = '\n'.join(lines)
print(disp)
self.app.args.parse_args(['--help'])
class PostController(CementBaseController):
class Meta:
label = 'post'
stacked_on = 'base'
stacked_type = 'nested'
description = "follows Post manipulation subcommands"
@expose(help="second-controller default command", hide=True)
def default(self):
self.app.args.parse_args(['--help'])
class CategoryController(CementBaseController):
class Meta:
label = 'category'
stacked_on = 'base'
stacked_type = 'nested'
description = "follows Category manipulation subcommands"
@expose(help="second-controller default command", hide=True)
def default(self):
self.app.args.parse_args(['--help'])
class PostSubController(CementBaseController):
class Meta:
label = 'post_sub_controller'
stacked_on = 'post'
stacked_type = 'embedded'
description = "this controller is embedded in the Post Controller"
arguments = [
(['extra_arguments'],
dict(help='title and content', action='store', nargs='*')), (['--category'],
dict(help='category name', action='store')),
]
@expose(help="this is a command under the PostController")
def add(self):
# self.app.args.parse_args(['--help'])
if self.app.pargs.extra_arguments and len(self.app.pargs.extra_arguments) == 2:
title = self.app.pargs.extra_arguments[0]
content = self.app.pargs.extra_arguments[1]
if self.app.pargs.category:
category = self.app.pargs.category
boo = False
for p in Category.select():
if p.category == category:
boo = True
break
if boo == False:
cate = Category(category=category)
cate.save()
firstpost = Blog_post(
title=title, content=content, category=category)
firstpost.save()
else:
firstpost = Blog_post(title=title, content=content)
firstpost.save()
@expose(help="this is a command under the PostController")
def list(self):
for p in Blog_post.select():
print(p.id, p.title, p.content, p.category)
@expose(help="this is a command under the PostController")
def search(self):
if self.app.pargs.extra_arguments and len(self.app.pargs.extra_arguments) == 1 and not(self.app.pargs.category):
keyword = self.app.pargs.extra_arguments[0]
for p in Blog_post.select():
try:
if (keyword in p.title) or (keyword in p.content) or (keyword in p.category):
print(p.id, p.title, p.content, p.category)
except:
pass
class CategorySubController(CementBaseController):
class Meta:
label = 'category_sub_controller'
stacked_on = 'category'
stacked_type = 'embedded'
description = "this controller is embedded the Category Controller"
arguments = [
(['xtra_arguments'],
dict(help='<category-name> or {<post-id> and <cat-id>}', action='store', nargs='*')),
]
@expose(help="this is a command under the CategoryController")
def add(self):
if self.app.pargs.xtra_arguments and len(self.app.pargs.xtra_arguments) == 1:
cat_name = self.app.pargs.xtra_arguments[0]
cat = Category(category=cat_name)
cat.save()
@expose(help="this is a command under the CategoryController")
def list(self):
for p in Category.select():
print(p.id, p.category)
@expose(help="this is a command under the CategoryController")
def assign(self):
if self.app.pargs.xtra_arguments and len(self.app.pargs.xtra_arguments) == 2:
post_id = int(self.app.pargs.xtra_arguments[0])
cat_id = int(self.app.pargs.xtra_arguments[1])
boo = False
for c in Category.select():
print(type(c.id), type(cat_id))
if c.id == cat_id:
boo = True
cat = c.category
break
if boo == False:
print("mentioned category id doesnt exist")
else:
foo = False
for b in Blog_post.select():
if b.id == post_id:
foo = True
b.category = cat
b.save()
break
if foo == False:
print("mentioned post id doesnt exist")
def main():
try:
# create the application
app = CementApp('BlogApp')
# register controllers
handler.register(BlogAppBaseController)
handler.register(PostController)
handler.register(CategoryController)
handler.register(PostSubController)
handler.register(CategorySubController)
# setup the application
app.setup()
# run the application
app.run()
finally:
# close the application
app.close()
if __name__ == '__main__':
main()