Skip to content
This repository has been archived by the owner on Apr 9, 2019. It is now read-only.

Commit

Permalink
Add version 2
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliqiang committed Nov 27, 2016
1 parent a962f3a commit b7d03d8
Show file tree
Hide file tree
Showing 110 changed files with 9,133 additions and 717 deletions.
99 changes: 67 additions & 32 deletions README.md
Expand Up @@ -7,15 +7,15 @@ Another blog system writen by Flask and Redis. Not Mysql or Mongodb. I develop i

Mdpress can fullfill my requirements, and it's blog editor base on [Editor.md](https://pandao.github.io/editor.md/examples/index.html "Editor.md").

### demo:
### Demo:

[MDPress Demo](http://www.mdpress.me)
[Angiris Council](http://liuliqiang.info)

### Solved Problem

- [x] Fast Load Speed
- [x] Beauty Display And Theme Support
- [x] Post Search
- [x] Post Search
- [x] Images Links Manager

### Feature
Expand All @@ -26,9 +26,56 @@ Mdpress can fullfill my requirements, and it's blog editor base on [Editor.md](h
- [x] Convenience images upload
- [x] Unicon images admin

###Dependence
### Usage

1. Create Virtual Enviroment

```bash
virtualenv mdpenv
source mdpenv/bin/activator
```

2. Install Dependences

```bash
pip install -r utils/requirements.txt
```

3. Config Development Configuration

```bash
vim config/development.py
```

make sure your redis config is right and redis-server is running:

```python
REDIS_CONFIG = {
'HOST': 'localhost',
'PORT': 6379,
'DB': 10
}
```

4. Running Server

Now, it is time to run server:

```bash
python manager runserver
```

and access it by browser with url:

```bash
http://localhost:5000
```

you will see the index page as show below...

###Dependence

```bash
Python == 2.7
Reids >= 3.0.0
```
Expand All @@ -40,19 +87,11 @@ and some python lib dependences can be installed by pip.

Want to know more about author, please visit: [liuliqiang.info](http://liuliqiang.info)

###Contact

Please Mailto:liqianglau@outlook.com


###Screen Shoot

**Index**

![](http://ooo.0o0.ooo/2016/07/27/579978371acf9.jpg)

**Archive**

![](http://ooo.0o0.ooo/2016/07/27/5799783689c9f.jpg)

**Admin Login**
Expand All @@ -70,40 +109,36 @@ Please Mailto:liqianglau@outlook.com
### TODOs

- [x] dashboard data correct
- [x] convert markdown to html
- [x] import wordpress html to markdown
- [x] duosuho configs
- [x] convert markdown to html -- 2016-7-30 00:42:08
- [x] import wordpress html to markdown -- 2016-7-30 23:43:16
- [x] import wordpress post title html decode -- 2016-7-31 00:10:31
- [x] code highlight
- [x] template manager
- [x] image admins
- [x] category level manager
- [x] post search
- [x] code highlight
- [x] template manager
- [x] duosuho configs


###Updated History

- v0.1
- 2015-02-18 14:38:03
- v0.1
- 2015-02-18 14:38:03

create project, and implement base feature
- v0.2
- 2015-02-21 11:57:03

- v0.2
- 2015-02-21 11:57:03

add save and view post feature

- v0.3
- 2016-06-04 13:03:31
- v0.3
- 2016-06-04 13:03:31

refactor whole project structure

- v0.4
- 2016-7-28 22:34:36

add redis models support
add theme and jade template support

- v1.0.0
- 2016-10-31 23:01:17

publish version 1
add redis models support
add theme and jade template support
124 changes: 63 additions & 61 deletions application/__init__.py
Expand Up @@ -2,17 +2,21 @@
# encoding: utf-8
import os
import sys
import json
import time
import logging
import logging.handlers
from datetime import datetime

import jinja2
import redisco
from flask import Flask, current_app, jsonify
from flask import Flask, current_app, request

from config import load_config
from application.extensions import jwt, mail, redis, RedisLoader
from application.extensions import (
es, mail, redis, celery, sentry, login_manager)
from application.controllers import all_bp
from application.models import User
from application.services.theme import setup_theme

# convert python's encoding to utf8
try:
Expand All @@ -35,15 +39,25 @@ def create_app(mode):

# Register components
configure_logging(app)
register_extensions(app)
register_blueprint(app)
register_extensions(app)
register_tasks(app)
register_theme(app)

return app


def register_extensions(app):
if app.config.get('ELASTICSEARCH_SUPPORT', False):
es.init_app(app)
mail.init_app(app)
redis.init_app(app)

login_manager.session_protection = 'strong'
login_manager.login_view = '/admin/login'
login_manager.init_app(app)

celery.config_from_object(app.config)
"""init redis connection"""
redisco.connection_setup(host=app.config['REDIS_CONFIG']['HOST'],
port=app.config['REDIS_CONFIG']['PORT'],
Expand All @@ -53,79 +67,63 @@ def filter_func(kv):
print kv

app.jinja_env.add_extension('pyjade.ext.jinja.PyJadeExtension')
from application.services.theme import RedisLoader
app.jinja_env.loader = RedisLoader()

from pyjade import Compiler
Compiler.register_autoclosecode('load')

# jwt config
def jwt_authenticate(email, password):
logging.info("email:{}\npassword:{}\n".format(email, password))
from application.models import User
user = User.objects.filter(email=email).first()
if user and user.password == password:
return user
else:
return None

def jwt_identity(payload):
logging.info("payload:{}".format(payload))
from application.models import User
user_id = payload['identity']
@login_manager.user_loader
def load_user(user_id):
return User.objects.get_by_id(user_id)

def make_payload(identity):
iat = datetime.utcnow()
exp = iat + current_app.config.get('JWT_EXPIRATION_DELTA')
nbf = iat + current_app.config.get('JWT_NOT_BEFORE_DELTA')
identity = str(identity.id)
return {'exp': exp, 'iat': iat, 'nbf': nbf, 'identity': identity}

def response_handler(access_token, identity):
return jsonify({'access_token': access_token.decode('utf-8'),
'refresh_token': access_token.decode('utf-8'),
'expires_in': 24 * 60 * 60,
'token_type': 'Bearer'})

jwt.authentication_handler(jwt_authenticate)
jwt.identity_handler(jwt_identity)
jwt.jwt_payload_handler(make_payload)
jwt.auth_response_handler(response_handler)

jwt.init_app(app)
if not app.config['DEBUG'] and not app.config['TESTING']:
sentry.init_app(app, dsn='https://629e0f9d9a0b474585f3355a640ffa99:d0fe041082384cdfab8f0f6d0846e54c@app.getsentry.com/93431')


def register_blueprint(app):
for bp in all_bp:
app.register_blueprint(bp)

@app.before_first_request
def setup_templates():
theme = current_app.config.get('THEME', 'default')
theme_key = current_app.config.get('THEME_KEY')
redis.set(theme_key, theme)

template_prefix = current_app.config.get('TEMPLATE_PREFIX')
template_path = os.path.join(current_app.config['PROJECT_PATH'],
'application/templates')

# add theme file
for rt, _, fs in os.walk(template_path):
for f in fs:
path = os.path.join(rt, f)
postfix = path[len(template_path) + 1:]
if os.path.isfile(path):
with open(path, 'r') as fp:
data = fp.read()
key = "{}:{}".format(template_prefix, postfix)
print key
redis.set(key, data)
else:
current_app.logger.info("{} is not a file".format(f))
@app.before_request
def log_request():
req_ip = request.headers.get('X-Real-Ip')
req_data = {"timestamp": datetime.utcnow(),
"ip": req_ip if req_ip else request.remote_addr,
"request_method": request.method,
"request_url": request.url,
"request_path": request.path,
"request_data": json.dumps(request.data)}
for k, v in request.headers.iteritems():
req_data["request_header_{}".format(k)] = v
if current_app.config.get('ELASTICSEARCH_SUPPORT'):
rst = es.index(index="mdpress", doc_type="request_log", body=req_data)
current_app.logger.info("before reqeust result: {}".format(rst))
else:
dt = datetime.now()
key = "request_log:{}:{}:{}:{}:{}:{}".format(
dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)
redis.hmset(key, req_data)

if not app.config.get('DEBUG') and not app.config.get('TESTING'):
from application.tasks.slack import log_request
log_request.delay(request.path, req_data)

@app.errorhandler(404)
def page_not_found(error):
# req_ip = request.headers.get('X-Real-Ip') or request.remote_addr
redis.zincrby("mdpress:visit-404", request.path)
return 'Page Not Found!!!', 404


def register_theme(app):
theme = app.config.get('THEME', 'default')
setup_theme(app, theme, True)
setup_theme(app, 'admin', False)


def configure_logging(app):
logging.basicConfig()
# logging.basicConfig()
if app.config.get('TESTING'):
app.logger.setLevel(logging.INFO)
return
Expand All @@ -141,3 +139,7 @@ def configure_logging(app):
'[in %(pathname)s:%(lineno)d]')
)
app.logger.addHandler(logging_handler)


def register_tasks(app):
from application.tasks.slack import *
4 changes: 4 additions & 0 deletions application/controllers/__init__.py
@@ -1,17 +1,21 @@
#!/usr/bin/env python
# encoding: utf-8
import auth
import feed
import user
import post
import admin
import upload
import frontend
import callback

all_bp = [
auth.auth_bp,
user.user_bp,
post.post_bp,
feed.feed_bp,
admin.admin_bp,
upload.upload_bp,
frontend.frontend_bp,
callback.callback_bp,
]

0 comments on commit b7d03d8

Please sign in to comment.