forked from xuming/micolog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
1119 lines (941 loc) · 37.8 KB
/
admin.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '1.2')
import wsgiref.handlers
from django.conf import settings
settings._target = None
from django.utils import simplejson
from django.utils.translation import ugettext as _
import base
from model import *
from app.pingback import autoPingback
from app.trackback import TrackBack
import xmlrpclib
from xmlrpclib import Fault
class Error404(base.BaseRequestHandler):
#@printinfo
def get(self,slug=None):
self.render2('views/admin/404.html')
class setlanguage(base.BaseRequestHandler):
def get(self):
lang_code = self.param('language')
next = self.param('next')
if (not next) and os.environ.has_key('HTTP_REFERER'):
next = os.environ['HTTP_REFERER']
if not next:
next = '/'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from django.utils.translation import check_for_language, activate
from django.conf import settings
settings._target = None
if lang_code and check_for_language(lang_code):
g_blog.language=lang_code
activate(g_blog.language)
g_blog.save()
self.redirect(next)
## if hasattr(request, 'session'):
## request.session['django_language'] = lang_code
## else:
## cookiestr='django_language=%s;expires=%s;domain=%s;path=/'%( lang_code,
## (datetime.now()+timedelta(days=100)).strftime("%a, %d-%b-%Y %H:%M:%S GMT"),
## ''
## )
## self.write(cookiestr)
## self.response.headers.add_header('Set-Cookie', cookiestr)
def fetch_result(target_uri):
for RETRY in range(5):
try:
response = urlfetch.fetch(target_uri)
return response
except urlfetch.DownloadError:
logging.info('Download Error, Retry %s times'%RETRY)
continue
except:
raise base.PingbackError(16)
else:
logging.info('Times Over')
raise base.PingbackError(16)
class admin_do_action(base.BaseRequestHandler):
@base.requires_admin
def get(self,slug=None):
try:
func=getattr(self,'action_'+slug)
if func and callable(func):
func()
else:
self.render2('views/admin/error.html',{'message':_('This operate has not defined!')})
except:
self.render2('views/admin/error.html',{'message':_('This operate has not defined!')})
def post(self,slug=None):
try:
func=getattr(self,'action_'+slug)
if func and callable(func):
func()
else:
self.render2('views/admin/error.html',{'message':_('This operate has not defined!')})
except:
self.render2('views/admin/error.html',{'message':_('This operate has not defined!')})
@base.requires_admin
def action_test(self):
self.write(os.environ)
@base.requires_admin
def action_cacheclear(self):
memcache.flush_all()
self.write(_('"Cache cleared successful"'))
@base.requires_admin
def action_updatecomments(self):
for entry in Entry.all():
cnt=entry.comments().count()
if cnt<>entry.commentcount:
entry.commentcount=cnt
entry.put()
self.write(_('"All comments updated"'))
@base.requires_admin
def action_updatecommentno(self):
for entry in Entry.all():
entry.update_commentno()
self.write(_('"All comments number Updates."'))
@base.requires_admin
def action_updatelink(self):
link_format=self.param('linkfmt')
if link_format:
link_format=link_format.strip()
g_blog.link_format=link_format
g_blog.save()
for entry in Entry.all():
vals={'year':entry.date.year,'month':str(entry.date.month).zfill(2),'day':entry.date.day,
'postname':entry.slug,'post_id':entry.post_id}
if entry.slug:
newlink=link_format%vals
else:
newlink=g_blog.default_link_format%vals
if entry.link<>newlink:
entry.link=newlink
entry.put()
self.write(_('"Link formated succeed"'))
else:
self.write(_('"Please input url format."'))
@base.requires_admin
def action_init_blog(self,slug=None):
for com in Comment.all():
com.delete()
for entry in Entry.all():
entry.delete()
g_blog.entrycount=0
self.write(_('"Init has succeed."'))
@base.requires_admin
def action_update_tags(self,slug=None):
for tag in Tag.all():
tag.delete()
for entry in Entry.all().filter('entrytype =','post'):
if entry.tags:
for t in entry.tags:
try:
Tag.add(t)
except:
base.traceback.print_exc()
self.write(_('"All tags for entry have been updated."'))
@base.requires_admin
def action_update_archives(self,slug=None):
for archive in Archive.all():
archive.delete()
entries=Entry.all().filter('entrytype =','post')
archives={}
for entry in entries:
my = entry.date.strftime('%B %Y') # September-2008
sy = entry.date.strftime('%Y') #2008
sm = entry.date.strftime('%m') #09
if archives.has_key(my):
archive=archives[my]
archive.entrycount+=1
else:
archive = Archive(monthyear=my,year=sy,month=sm,entrycount=1)
archives[my]=archive
for ar in archives.values():
ar.put()
self.write(_('"All entries have been updated."'))
def action_trackback_ping(self):
tbUrl=self.param('tbUrl')
title=self.param('title')
excerpt=self.param('excerpt')
url=self.param('url')
blog_name=self.param('blog_name')
tb=TrackBack(tbUrl,title,excerpt,url,blog_name)
tb.ping()
def action_pingback_ping(self):
"""Try to notify the server behind `target_uri` that `source_uri`
points to `target_uri`. If that fails an `PingbackError` is raised.
"""
source_uri=self.param('source')
target_uri=self.param('target')
try:
#response =urlfetch.fetch(target_uri)
response=fetch_result(target_uri) #retry up to 5 times
except:
raise base.PingbackError(32)
try:
pingback_uri = response.headers['X-Pingback']
except KeyError:
_pingback_re = re.compile(r'<link rel="pingback" href="([^"]+)" ?/?>(?i)')
match = _pingback_re.search(response.content)
if match is None:
raise base.PingbackError(33)
pingback_uri =base.urldecode(match.group(1))
rpc = xmlrpclib.ServerProxy(pingback_uri)
try:
return rpc.pingback.ping(source_uri, target_uri)
except Fault, e:
raise base.PingbackError(e.faultCode)
except:
raise base.PingbackError(32)
class admin_tools(base.BaseRequestHandler):
def __init__(self):
base.BaseRequestHandler.__init__(self)
self.current="config"
@base.requires_admin
def get(self,slug=None):
self.render2('views/admin/tools.html')
class admin_sitemap(base.BaseRequestHandler):
def __init__(self):
base.BaseRequestHandler.__init__(self)
self.current="config"
@base.requires_admin
def get(self,slug=None):
self.render2('views/admin/sitemap.html')
@base.requires_admin
def post(self):
str_options= self.param('str_options').split(',')
for name in str_options:
value=self.param(name)
setattr(g_blog,name,value)
bool_options= self.param('bool_options').split(',')
for name in bool_options:
value=self.param(name)=='on'
setattr(g_blog,name,value)
int_options= self.param('int_options').split(',')
for name in int_options:
try:
value=int( self.param(name))
setattr(g_blog,name,value)
except:
pass
float_options= self.param('float_options').split(',')
for name in float_options:
try:
value=float( self.param(name))
setattr(g_blog,name,value)
except:
pass
g_blog.save()
self.render2('views/admin/sitemap.html',{})
class admin_import(base.BaseRequestHandler):
def __init__(self):
base.BaseRequestHandler.__init__(self)
self.current='config'
@base.requires_admin
def get(self,slug=None):
gblog_init()
self.render2('views/admin/import.html',{'importitems':
self.blog.plugins.filter('is_import_plugin',True)})
## def post(self):
## try:
## queue=taskqueue.Queue("import")
## wpfile=self.param('wpfile')
## #global imt
## imt=import_wordpress(wpfile)
## imt.parse()
## memcache.set("imt",imt)
##
#### import_data=OptionSet.get_or_insert(key_name="import_data")
#### import_data.name="import_data"
#### import_data.bigvalue=pickle.dumps(imt)
#### import_data.put()
##
## queue.add(taskqueue.Task( url="/admin/import_next"))
## self.render2('views/admin/import.html',
## {'postback':True})
## return
## memcache.set("import_info",{'count':len(imt.entries),'msg':'Begin import...','index':1})
## #self.blog.import_info={'count':len(imt.entries),'msg':'Begin import...','index':1}
## if imt.categories:
## queue.add(taskqueue.Task( url="/admin/import_next",params={'cats': pickle.dumps(imt.categories),'index':1}))
##
## return
## index=0
## if imt.entries:
## for entry in imt.entries :
## try:
## index=index+1
## queue.add(taskqueue.Task(url="/admin/import_next",params={'entry':pickle.dumps(entry),'index':index}))
## except:
## pass
##
## except:
## self.render2('views/admin/import.html',{'error':'import faiure.'})
class admin_setup(base.BaseRequestHandler):
def __init__(self):
self.current='config'
@base.requires_admin
def get(self,slug=None):
vals={'themes':ThemeIterator()}
self.render2('views/admin/setup.html',vals)
@base.requires_admin
def post(self):
old_theme=g_blog.theme_name
str_options= self.param('str_options').split(',')
for name in str_options:
value=self.param(name)
setattr(g_blog,name,value)
bool_options= self.param('bool_options').split(',')
for name in bool_options:
value=self.param(name)=='on'
setattr(g_blog,name,value)
int_options= self.param('int_options').split(',')
for name in int_options:
try:
value=int( self.param(name))
setattr(g_blog,name,value)
except:
pass
float_options= self.param('float_options').split(',')
for name in float_options:
try:
value=float( self.param(name))
setattr(g_blog,name,value)
except:
pass
if old_theme !=g_blog.theme_name:
g_blog.get_theme()
g_blog.owner=self.login_user
g_blog.author=g_blog.owner.nickname()
g_blog.save()
gblog_init()
vals={'themes':ThemeIterator()}
memcache.flush_all()
self.render2('views/admin/setup.html',vals)
class admin_entry(base.BaseRequestHandler):
def __init__(self):
base.BaseRequestHandler.__init__(self)
self.current='write'
@base.requires_admin
def get(self,slug='post'):
action=self.param("action")
entry=None
cats=Category.all()
alltags=Tag.all()
if action and action=='edit':
try:
key=self.param('key')
entry=Entry.get(key)
except:
pass
else:
action='add'
def mapit(cat):
return {'name':cat.name,'slug':cat.slug,'select':entry and cat.key() in entry.categorie_keys}
vals={'action':action,'entry':entry,'entrytype':slug,'cats':map(mapit,cats),'alltags':alltags}
self.render2('views/admin/entry.html',vals)
@base.requires_admin
def post(self,slug='post'):
action=self.param("action")
title=self.param("post_title")
content=self.param('content')
tags=self.param("tags")
cats=self.request.get_all('cats')
key=self.param('key')
if self.param('publish')!='':
published=True
elif self.param('unpublish')!='':
published=False
else:
published=self.param('published')=='True'
allow_comment=self.parambool('allow_comment')
allow_trackback=self.parambool('allow_trackback')
entry_slug=self.param('slug')
entry_parent=self.paramint('entry_parent')
menu_order=self.paramint('menu_order')
entry_excerpt=self.param('excerpt').replace('\n','<br />')
password=self.param('password')
sticky=self.parambool('sticky')
is_external_page=self.parambool('is_external_page')
target=self.param('target')
external_page_address=self.param('external_page_address')
def mapit(cat):
return {'name':cat.name,'slug':cat.slug,'select':cat.slug in cats}
vals={'action':action,'postback':True,'cats':Category.all(),'entrytype':slug,
'cats':map(mapit,Category.all()),
'entry':{ 'title':title,'content':content,'strtags':tags,'key':key,'published':published,
'allow_comment':allow_comment,
'allow_trackback':allow_trackback,
'slug':entry_slug,
'entry_parent':entry_parent,
'excerpt':entry_excerpt,
'menu_order':menu_order,
'is_external_page':is_external_page,
'target':target,
'external_page_address':external_page_address,
'password':password,
'sticky':sticky}
}
if not (title and (content or (is_external_page and external_page_address))):
vals.update({'result':False, 'msg':_('Please input title and content.')})
self.render2('views/admin/entry.html',vals)
else:
if action=='add':
entry= Entry(title=title,content=content)
entry.settags(tags)
entry.entrytype=slug
entry.slug=entry_slug.replace(" ","_")
entry.entry_parent=entry_parent
entry.menu_order=menu_order
entry.excerpt=entry_excerpt
entry.is_external_page=is_external_page
entry.target=target
entry.external_page_address=external_page_address
newcates=[]
entry.allow_comment=allow_comment
entry.allow_trackback=allow_trackback
entry.author=self.author.user
entry.author_name=self.author.dispname
entry.password=password
entry.sticky=sticky
if cats:
for cate in cats:
c=Category.all().filter('slug =',cate)
if c:
newcates.append(c[0].key())
entry.categorie_keys=newcates;
entry.save(published)
if published:
smsg=_('Saved ok. <a href="/%(link)s" target="_blank">View it now!</a>')
else:
smsg=_('Saved ok.')
vals.update({'action':'edit','result':True,'msg':smsg%{'link':str(entry.link)},'entry':entry})
self.render2('views/admin/entry.html',vals)
if published and entry.allow_trackback and g_blog.allow_pingback:
try:
autoPingback(str(entry.fullurl),HTML=content)
except:
pass
elif action=='edit':
try:
entry=Entry.get(key)
entry.title=title
entry.content=content
entry.slug=entry_slug.replace(' ','-')
entry.entry_parent=entry_parent
entry.menu_order=menu_order
entry.excerpt=entry_excerpt
entry.is_external_page=is_external_page
entry.target=target
entry.external_page_address=external_page_address
entry.settags(tags)
entry.author=self.author.user
entry.author_name=self.author.dispname
entry.password=password
entry.sticky=sticky
newcates=[]
if cats:
for cate in cats:
c=Category.all().filter('slug =',cate)
if c:
newcates.append(c[0].key())
entry.categorie_keys=newcates;
entry.allow_comment=allow_comment
entry.allow_trackback=allow_trackback
entry.save(published)
if published:
smsg=_('Saved ok. <a href="/%(link)s" target="_blank">View it now!</a>')
else:
smsg=_('Saved ok.')
vals.update({'result':True,'msg':smsg%{'link':str(base.urlencode( entry.link))},'entry':entry})
self.render2('views/admin/entry.html',vals)
if published and entry.allow_trackback and g_blog.allow_pingback:
try:
autoPingback(entry.fullurl,HTML=content)
except:
pass
except:
vals.update({'result':False,'msg':_('Error:Entry can''t been saved.')})
self.render2('views/admin/entry.html',vals)
class admin_entries(base.BaseRequestHandler):
@base.requires_admin
def get(self,slug='post'):
try:
page_index=int(self.param('page'))
except:
page_index=1
entries=Entry.all().filter('entrytype =',slug).order('-date')
entries,links=base.Pager(query=entries,items_per_page=15).fetch(page_index)
self.render2('views/admin/'+slug+'s.html',
{
'current':slug+'s',
'entries':entries,
'pager':links
}
)
@base.requires_admin
def post(self,slug='post'):
try:
linkcheck= self.request.get_all('checks')
for id in linkcheck:
kid=int(id)
entry=Entry.get_by_id(kid)
#delete it's comments
#entry.delete_comments()
entry.delete()
g_blog.entrycount-=1
finally:
self.redirect('/admin/entries/'+slug)
class admin_categories(base.BaseRequestHandler):
@base.requires_admin
def get(self,slug=None):
try:
page_index=int(self.param('page'))
except:
page_index=1
cats=Category.allTops()
entries,pager=base.Pager(query=cats,items_per_page=15).fetch(page_index)
self.render2('views/admin/categories.html',
{
'current':'categories',
'cats':cats,
'pager':pager
}
)
@base.requires_admin
def post(self,slug=None):
try:
linkcheck= self.request.get_all('checks')
for key in linkcheck:
cat=Category.get(key)
if cat:
cat.delete()
finally:
self.redirect('/admin/categories')
class admin_comments(base.BaseRequestHandler):
@base.requires_admin
def get(self,slug=None):
try:
page_index=int(self.param('page'))
except:
page_index=1
cq=self.param('cq')
cv=self.param('cv')
if cq and cv:
query=Comment.all().filter(cq+' =',cv).order('-date')
else:
query=Comment.all().order('-date')
comments,pager=base.Pager(query=query,items_per_page=15).fetch(page_index)
self.render2('views/admin/comments.html',
{
'current':'comments',
'comments':comments,
'pager':pager,
'cq':cq,
'cv':cv
}
)
@base.requires_admin
def post(self,slug=None):
try:
linkcheck= self.request.get_all('checks')
entrykeys=[]
for key in linkcheck:
comment=Comment.get(key)
comment.delit()
entrykeys.append(comment.entry.key())
entrykeys=set(entrykeys)
for key in entrykeys:
e=Entry.get(key)
e.update_commentno()
e.removecache()
memcache.delete("/feed/comments")
finally:
self.redirect(self.request.uri)
class admin_links(base.BaseRequestHandler):
@base.requires_admin
def get(self,slug=None):
self.render2('views/admin/links.html',
{
'current':'links',
'links':Link.all().filter('linktype =','blogroll')#.order('-createdate')
}
)
@base.requires_admin
def post(self):
linkcheck= self.request.get_all('linkcheck')
for link_id in linkcheck:
kid=int(link_id)
link=Link.get_by_id(kid)
link.delete()
self.redirect('/admin/links')
class admin_link(base.BaseRequestHandler):
@base.requires_admin
def get(self,slug=None):
action=self.param("action")
vals={'current':'links'}
if action and action=='edit':
try:
action_id=int(self.param('id'))
link=Link.get_by_id(action_id)
vals.update({'link':link})
except:
pass
else:
action='add'
vals.update({'action':action})
self.render2('views/admin/link.html',vals)
@base.requires_admin
def post(self):
action=self.param("action")
name=self.param("link_name")
url=self.param("link_url")
comment = self.param("link_comment")
vals={'action':action,'postback':True,'current':'links'}
if not (name and url):
vals.update({'result':False,'msg':_('Please input name and url.')})
self.render2('views/admin/link.html',vals)
else:
if action=='add':
link= Link(linktext=name,href=url,linkcomment=comment)
link.put()
vals.update({'result':True,'msg':'Saved ok'})
self.render2('views/admin/link.html',vals)
elif action=='edit':
try:
action_id=int(self.param('id'))
link=Link.get_by_id(action_id)
link.linktext=name
link.href=url
link.linkcomment = comment
link.put()
#goto link manage page
self.redirect('/admin/links')
except:
vals.update({'result':False,'msg':_('Error:Link can''t been saved.')})
self.render2('views/admin/link.html',vals)
class admin_category(base.BaseRequestHandler):
def __init__(self):
base.BaseRequestHandler.__init__(self)
self.current='categories'
@base.requires_admin
def get(self,slug=None):
action=self.param("action")
key=self.param('key')
category=None
if action and action=='edit':
try:
category=Category.get(key)
except:
pass
else:
action='add'
vals={'action':action,'category':category,'key':key,'categories':[c for c in Category.all() if not category or c.key()!=category.key()]}
self.render2('views/admin/category.html',vals)
@base.requires_admin
def post(self):
def check(cate):
parent=cate.parent_cat
skey=cate.key()
while parent:
if parent.key()==skey:
return False
parent=parent.parent_cat
return True
action=self.param("action")
name=self.param("name")
slug=self.param("slug")
parentkey=self.param('parentkey')
key=self.param('key')
vals={'action':action,'postback':True}
try:
if action=='add':
cat= Category(name=name,slug=slug)
if not (name and slug):
raise Exception(_('Please input name and slug.'))
if parentkey:
cat.parent_cat=Category.get(parentkey)
cat.put()
self.redirect('/admin/categories')
#vals.update({'result':True,'msg':_('Saved ok')})
#self.render2('views/admin/category.html',vals)
elif action=='edit':
cat=Category.get(key)
cat.name=name
cat.slug=slug
if not (name and slug):
raise Exception(_('Please input name and slug.'))
if parentkey:
cat.parent_cat=Category.get(parentkey)
if not check(cat):
raise Exception(_('A circle declaration found.'))
else:
cat.parent_cat=None
cat.put()
self.redirect('/admin/categories')
except Exception ,e :
if cat.is_saved():
cates=[c for c in Category.all() if c.key()!=cat.key()]
else:
cates= Category.all()
vals.update({'result':False,'msg':e.message,'category':cat,'key':key,'categories':cates})
self.render2('views/admin/category.html',vals)
class admin_status(base.BaseRequestHandler):
@base.requires_admin
def get(self):
self.render2('views/admin/status.html',{'cache':memcache.get_stats(),'current':'status','environ':os.environ})
class admin_authors(base.BaseRequestHandler):
@base.requires_admin
def get(self):
try:
page_index=int(self.param('page'))
except:
page_index=1
authors=User.all().filter('isAuthor =',True)
entries,pager=base.Pager(query=authors,items_per_page=15).fetch(page_index)
self.render2('views/admin/authors.html',
{
'current':'authors',
'authors':authors,
'pager':pager
}
)
@base.requires_admin
def post(self,slug=None):
try:
linkcheck= self.request.get_all('checks')
for key in linkcheck:
author=User.get(key)
author.delete()
finally:
self.redirect('/admin/authors')
class admin_author(base.BaseRequestHandler):
def __init__(self):
base.BaseRequestHandler.__init__(self)
self.current='authors'
@base.requires_admin
def get(self,slug=None):
action=self.param("action")
author=None
if action and action=='edit':
try:
key=self.param('key')
author=User.get(key)
except:
pass
else:
action='add'
vals={'action':action,'author':author}
self.render2('views/admin/author.html',vals)
@base.requires_admin
def post(self):
action=self.param("action")
name=self.param("name")
slug=self.param("email")
vals={'action':action,'postback':True}
if not (name and slug):
vals.update({'result':False,'msg':_('Please input dispname and email.')})
self.render2('views/admin/author.html',vals)
else:
if action=='add':
author= User(dispname=name,email=slug )
author.user=db.users.User(slug)
author.put()
vals.update({'result':True,'msg':'Saved ok'})
self.render2('views/admin/author.html',vals)
elif action=='edit':
try:
key=self.param('key')
author=User.get(key)
author.dispname=name
author.email=slug
author.user=db.users.User(slug)
author.put()
if author.isadmin:
g_blog.author=name
self.redirect('/admin/authors')
except:
vals.update({'result':False,'msg':_('Error:Author can''t been saved.')})
self.render2('views/admin/author.html',vals)
class admin_plugins(base.BaseRequestHandler):
def __init__(self):
base.BaseRequestHandler.__init__(self)
self.current='plugins'
@base.requires_admin
def get(self,slug=None):
vals={'plugins':self.blog.plugins}
self.render2('views/admin/plugins.html',vals)
@base.requires_admin
def post(self):
action=self.param("action")
name=self.param("plugin")
ret=self.param("return")
self.blog.plugins.activate(name,action=="activate")
if ret:
self.redirect(ret)
else:
vals={'plugins':self.blog.plugins}
self.render2('views/admin/plugins.html',vals)
class admin_plugins_action(base.BaseRequestHandler):
def __init__(self):
base.BaseRequestHandler.__init__(self)
self.current='plugins'
@base.requires_admin
def get(self,slug=None):
plugin=self.blog.plugins.getPluginByName(slug)
if not plugin :
self.error(404)
return
plugins=self.blog.plugins.filter('active',True)
if not plugin.active:
pcontent=_('''<div>Plugin '%(name)s' havn't actived!</div><br><form method="post" action="/admin/plugins?action=activate&plugin=%(iname)s&return=/admin/plugins/%(iname)s"><input type="submit" value="Activate Now"/></form>''')%{'name':plugin.name,'iname':plugin.iname}
plugins.insert(0,plugin)
else:
pcontent=plugin.get(self)
vals={'plugins':plugins,
'plugin':plugin,
'pcontent':pcontent}
self.render2('views/admin/plugin_action.html',vals)
@base.requires_admin
def post(self,slug=None):
plugin=self.blog.plugins.getPluginByName(slug)
if not plugin :
self.error(404)
return
plugins=self.blog.plugins.filter('active',True)
if not plugin.active:
pcontent=_('''<div>Plugin '%(name)s' havn't actived!</div><br><form method="post" action="/admin/plugins?action=activate&plugin=%(iname)s&return=/admin/plugins/%(iname)s"><input type="submit" value="Activate Now"/></form>''')%{'name':plugin.name,'iname':plugin.iname}
plugins.insert(0,plugin)
else:
pcontent=plugin.post(self)
vals={'plugins':plugins,
'plugin':plugin,
'pcontent':pcontent}
self.render2('views/admin/plugin_action.html',vals)
class WpHandler(base.BaseRequestHandler):
@base.requires_admin
def get(self,tags=None):
try:
all=self.param('all')
except:
all=False
if(all):
entries = Entry.all().order('-date')
filename='micolog.%s.xml'%datetime.now().strftime('%Y-%m-%d')
else:
str_date_begin=self.param('date_begin')
str_date_end=self.param('date_end')
try:
date_begin=datetime.strptime(str_date_begin,"%Y-%m-%d")
date_end=datetime.strptime(str_date_end,"%Y-%m-%d")
entries = Entry.all().filter('date >=',date_begin).filter('date <',date_end).order('-date')
filename='micolog.%s.%s.xml'%(str(str_date_begin),str(str_date_end))
except:
self.render2('views/admin/404.html')
return