Skip to content

Commit

Permalink
增加头信息的管理能力
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrun13 committed Mar 22, 2016
1 parent 366a85e commit f2195c2
Show file tree
Hide file tree
Showing 5 changed files with 313 additions and 30 deletions.
97 changes: 82 additions & 15 deletions routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,77 @@ def Raw(url):
return template('error', params=params, config=config)
return template('archive', params=params, config=config)

@route('/get_head', method='POST')
def GetHead():
session = get_current_session()
username = session.get('username','')
if not username:
return {'code': -2, 'msg': '未登录'}

raw_url = request.POST.get("raw_url",'')
url = raw_url.replace(config.raw_url, '').replace(config.raw_suffix, config.url_suffix)
url = config.entry_url + url
params = entryService.find_by_url(entryService.types.entry, url)
entry = params.entry
if entry:
tags = [e for e in entry.tags if not e.startswith('__')]
data = {
'title': entry.name,
'cat': entry.categories and ','.join(entry.categories) or '',
'tag': tags and ','.join(tags) or '',
'private': entry.private
}
ret = {'code': 0, 'msg': 'ok', 'data': data}
else:
ret = {'code': -1, 'msg': 'can not find'}
return ret

@route('/save_head', method='POST')
def SaveHead():
session = get_current_session()
username = session.get('username','')
if not username:
return {'code': -2, 'msg': '未登录'}

title = request.POST.get("title", '').strip()
cat = request.POST.get("cat",'').strip().replace(',', ',')
tag = request.POST.get("tag",'').strip().replace(',', ',')
private = request.POST.get("private",'')
content = request.POST.get("content",'').strip()
raw_url = request.POST.get("raw_url",'')

if not title:
return {'code': -3, 'msg': '请填写完整'}

url = raw_url.replace(config.raw_url, '').replace(config.raw_suffix, config.url_suffix)
url = config.entry_url + url
params = entryService.find_by_url(entryService.types.entry, url)
entry = params.entry
if not entry:
return {'code': -4, 'msg': '对象不存在'}

tag = '__%s,%s'%(username, tag) if tag else '__'+username

head = '''---
layout: post
title: %s
category: %s
tags: [%s]
---
'''%(title, cat, tag)

m_file = open(entry.path, 'w+')
m_file.write('%s\n%s'%(head,content) )
m_file.close()

entryService.update_entry(entry, {
'title': title,
'tags': tag and tag.split(',') or [],
'cats': cat and cat.split(',') or [],
'private': bool(private)
})
return {'code': 0, 'msg': '修改成功'}

@route('/private_raw:url#.*#')
@auth_required()
def PrivateRaw(url):
Expand Down Expand Up @@ -170,16 +241,17 @@ def publish():
if not username:
return {'code': -2, 'msg': '未登录'}

name = request.POST.get("name",None)
title = request.POST.get("title",None)
cat = request.POST.get("cat",'')
tag = request.POST.get("tag",'')
private = request.POST.get("private",'')
print '---------',private,type(private)

name = request.POST.get("name", '').strip()
title = request.POST.get("title", '').strip()
cat = request.POST.get("cat",'').strip().replace(',', ',')
tag = request.POST.get("tag",'').strip().replace(',', ',')
private = request.POST.get("private",'') # '' or 'on'
content = request.POST.get("content",'').strip()

tag = '__%s,%s'%(username, tag) if tag else username
if not (name and title):
return {'code': -3, 'msg': '请填写完整'}

tag = '__%s,%s'%(username, tag) if tag else '__'+username

head = '''---
layout: post
Expand All @@ -188,18 +260,13 @@ def publish():
tags: [%s]
---
'''%(title, cat, tag)
import datetime
m_date = datetime.datetime.now().strftime('%Y-%m-%d')
path = 'raw/entry/%s-%s.md'%(m_date, name)
m_file = open(path, 'w+')
m_file.write('%s\n%s'%(head,content) )
m_file.close()
if private:
entryService.add_entry(False, path, True)
entryService.update_urls()
entryService.add_private(path)
else:
entryService.add_entry(True, path)

entryService.add_entry(True, path, True if private else False)
return {'code': 0, 'msg': '发布成功'}

@route('/auth/login', method=['GET','POST'])
Expand Down
63 changes: 53 additions & 10 deletions service.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,52 @@ def add_entry(self, inotified, path, private=False):
if not entry == None:
self.entries[entry.url] = entry
if inotified:
self._init_miscellaneous(self.types.add, [entry])
if private:
self.update_urls()
self.add_private(path)
else:
self._init_miscellaneous(self.types.add, [entry])
else:
self.update_urls()

def update_entry(self, entry, data):
tar = self.entries[entry.url]
if 'title' in data:
tar.name = data.get('title')
#entry.name = data.get('title')
old_private = entry.private
new_private = bool(data.get('private', ''))
changed = False

if 'tags' in data:
old_tags = entry.tags
new_tags = data.get('tags')
if not old_private:
self._init_tag(self.types.delete, entry.url, old_tags)
if not new_private:
self._init_tag(self.types.add, entry.url, new_tags)
tar.tags = new_tags
if old_tags!=new_tags:changed=True
if 'cats' in data:
old_cats = entry.categories
new_cats = data.get('cats')
if not old_private:
self._init_category(self.types.delete, entry.url, old_cats)
if not new_private:
self._init_category(self.types.add, entry.url, new_cats)
tar.categories = new_cats
if old_cats!=new_cats:changed = True
tar.private = new_private
if old_private!=new_private:
self.update_urls()
if new_private:
self.add_private(entry.path)
else:
self.del_private(entry.path)
changed = True

if changed:
self._init_params()

def delete_entry(self, path):
for entry in self.entries.values():
Expand Down Expand Up @@ -201,7 +244,14 @@ def update_urls(self):
'''
public url noupdate
'''
self.all_urls = sorted(self.entries.keys(), reverse=True)
_list = []
_pub_list = []
for url, entry in self.entries.items():
_list.append(url)
if not entry.private:
_pub_list.append(url)
self.urls = sorted(_pub_list, reverse=True)
self.all_urls = sorted(_list, reverse=True)

def _init_miscellaneous(self,init_type, entries):
'''
Expand All @@ -213,14 +263,7 @@ def _init_miscellaneous(self,init_type, entries):
self._init_tag(init_type, entry.url, entry.tags)
self._init_category(init_type, entry.url, entry.categories)
self._init_monthly_archive(init_type, entry.url)
_list = []
_pub_list = []
for url, entry in self.entries.items():
_list.append(url)
if not entry.private:
_pub_list.append(url)
self.urls = sorted(_pub_list, reverse=True)
self.all_urls = sorted(_list, reverse=True)
self.update_urls()
self._init_params()

def _init_subscribe(self):
Expand Down
156 changes: 156 additions & 0 deletions static/js/change-dialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@

(function() {

var factory = function (exports) {

var pluginName = "change-dialog";

exports.fn.changeDialog = function(raw_url, init) {

var _this = this;
var cm = this.cm;
var lang = this.lang;
var editor = this.editor;
var settings = this.settings;
var cursor = cm.getCursor();
var selection = cm.getSelection();
var classPrefix = this.classPrefix;
var dialogName = classPrefix + pluginName, dialog;
var raw_url = raw_url;

cm.focus();

var loading = function(show) {
var _loading = dialog.find("." + classPrefix + "dialog-mask");
_loading[(show) ? "show" : "hide"]();
};

if (editor.find("." + dialogName).length < 1)
{
var guid = (new Date).getTime();

var dialogContent = ("<div class=\"" + classPrefix + "form\">" ) +
"<label>标题</label>" +
"<input type=\"text\" style=\"height: 33px\" data-title />" + (function(){
return "";
})() +
"<br/>" +
"<label>分类</label>" +
"<input type=\"text\" style=\"height: 33px\" value=\"" + selection + "\" data-cat />" +
"<br/>" +
"<label>标签</label>" +
"<input type=\"text\" style=\"height: 33px\" value=\"\" data-tag />" +
"<br/>" +
"<label></label>" +
"<input id=\"private\" type=\"checkbox\" value=\"1\" data-private /> 私密" +
"<br/>" +
("</div>");
// dialog定义开始
dialog = this.createDialog({
title : '发布',
width : 380,
height : 300,
name : dialogName,
content : dialogContent,
mask : settings.dialogShowMask,
drag : settings.dialogDraggable,
lockScreen : settings.dialogLockScreen,
maskStyle : {
opacity : settings.dialogMaskOpacity,
backgroundColor : settings.dialogMaskBgColor
},
buttons : {
enter : ['保存', function() {
var title = this.find("[data-title]").val();
var cat = this.find("[data-cat]").val();
var tag = this.find("[data-tag]").val();
var private = document.getElementById("private").checked;

if (title === ""){
alert('标题不能为空');
return false;
}
var dia = this;
function change_success(){
dia.hide().lockScreen(false).hideMask();
}
$.ajax( {
url: '/save_head',
data:{
raw_url: raw_url,
title: title,
cat: cat,
tag: tag,
private: private?'on': '',
content: cm.getValue()
},
type: 'post',
success: function(data) {
if(data.code ==0 ){
$.bootstrapGrowl("修改成功!", { type: 'success',align: 'center',width: 'auto',allow_dismiss: false ,offset: {from: 'top', amount: 60}});
change_success();
}else if(data.code ==-2){
href = '/auth/login?pop=1';
var win = window.open(href, 'login_window', 'height=450,width=780,resizable=yes,scrollbars=yes');
alert('请先登录');
}
else{
alert(data.msg);
}
},
error : function() {
alert("服务器异常!请稍后重试");
}
});
}],

cancel : ['取消', function() {
this.hide().lockScreen(false).hideMask();
return false;
}]
}
});
// dialog定义结束
dialog.attr("id", classPrefix + "change-dialog-" + guid);
}

dialog = editor.find("." + dialogName);
dialog.find("[data-title]").val(init.title);
dialog.find("[data-cat]").val(init.cat);
dialog.find("[data-tag]").val(init.tag);
document.getElementById("private").checked = init.private;

this.dialogShowMask(dialog);
this.dialogLockScreen();
dialog.show();

};

};

// CommonJS/Node.js
if (typeof require === "function" && typeof exports === "object" && typeof module === "object")
{
module.exports = factory;
}
else if (typeof define === "function") // AMD/CMD/Sea.js
{
if (define.amd) { // for Require.js

define(["editormd"], function(editormd) {
factory(editormd);
});

} else { // for Sea.js
define(function(require) {
var editormd = require("./../../editormd");
factory(editormd);
});
}
}
else
{
factory(window.editormd);
}

})();
2 changes: 1 addition & 1 deletion static/js/publish-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
}else if(data.code ==-2){
href = '/auth/login?pop=1';
var win = window.open(href, 'login_window', 'height=450,width=780,resizable=yes,scrollbars=yes');
win.focus();
alert('请先登录');
}
else{
alert(data.msg);
Expand Down
Loading

0 comments on commit f2195c2

Please sign in to comment.