Last active
April 13, 2018 02:55
-
-
Save jstacoder/ce70e3e2044be38259e0 to your computer and use it in GitHub Desktop.
example of integrating mongoengine with flask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% for cat,msg in get_flashed_messages(with_categories=true) %} | |
<div class="alert alert-dissmissable alert-{{ cat }}" id="alert-{{ loop.index }}"> | |
<span id="close-alert-{{ loop.index }}" class=close>x</span> | |
{{ msg }} | |
</div> | |
<script> | |
var alert = document.getElementById('alert-{{ loop.index }}'), | |
closeBtn = document.getElementById('close-alert-{{ loop.index }}'); | |
closeBtn.onClick = function(){ | |
alert.remove(); | |
}; | |
setTimeout(function(){ | |
alert.remove(); | |
},1500) | |
</script> | |
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask,render_template,request,redirect,flash,url_for | |
from flask.views import MethodView | |
from mongoengine import ( | |
StringField,ListField,EmbeddedDocumentField,IntField,DictField, | |
Document,DynamicDocument,EmbeddedDocument,ReferenceField, | |
GenericReferenceField,DateTimeField,connect | |
) | |
from wtforms import Form,fields,validators | |
from datetime import datetime | |
now = lambda: datetime.now() | |
connection = connect('dev2') | |
class Post(DynamicDocument): | |
title = StringField(max_length=255) | |
content = StringField(max_length=255) | |
date = DateTimeField() | |
author = StringField(max_length=255) | |
def get_app(cfg): | |
app = Flask(__name__,template_folder='.') | |
app.config.from_object(cfg) | |
return app | |
class PostForm(Form): | |
title = fields.StringField('title',validators=[validators.Required()]) | |
content = fields.TextAreaField() | |
date = fields.HiddenField(default=now()) | |
author = fields.StringField('name') | |
class BaseView(object): | |
def _generate_post(self): | |
form = PostForm(request.form) | |
title = form.title.data | |
date = form.date.data | |
content = form.content.data | |
author = form.author.data | |
post = Post(content=content,title=title,date=date,author=author).save() | |
flash('Thanks for Adding a new message','warning') | |
return post | |
class IndexView(BaseView,MethodView): | |
def get(self): | |
form = PostForm() | |
messages = Post.objects.all() | |
return render_template('index.html',messages=messages,form=form) | |
def post(self): | |
self._generate_post() | |
return redirect('/') | |
class DeleteView(MethodView): | |
def get(self,post_id): | |
post = Post.objects.get(id=post_id) | |
title = post.title | |
post.delete() | |
flash('Deleted message: {}'.format(title),'danger') | |
return redirect('/') | |
if __name__ == "__main__": | |
class Cfg(object): | |
SECRET_KEY = 'ccc' | |
app = get_app(Cfg()) | |
app.add_url_rule('/','index',view_func=IndexView.as_view('index')) | |
app.add_url_rule('/remove/<post_id>','remove',view_func=DeleteView.as_view('remove')) | |
app.run(host='0.0.0.0',port=8080,debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> | |
</head> | |
<body> | |
<div class=container> | |
<div class=row> | |
<div class=col-md-12> | |
{% include 'alert.html' with context %} | |
{% block body %} | |
{% endblock %} | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'base.html' %} | |
{% block body %} | |
{% if form %} | |
{% include 'post_form.html' with context %} | |
{% endif %} | |
{% include 'posts.html' with context %} | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form method=post action=""> | |
{{ form.date() }} | |
<fieldset> | |
<legend>Add A Post</legend> | |
<div class=row> | |
<div class=col-md-6> | |
<div class=form-group> | |
<label>Author</label> | |
{{ form.author(class_='form-control') }} | |
</div> | |
</div> | |
<div class=col-md-6> | |
<div class=form-group> | |
<label>Title</label> | |
{{ form.title(class_='form-control') }} | |
</div> | |
</div> | |
</div> | |
<div class=form-group> | |
{{ form.content(class_='form-control') }} | |
</div> | |
<input class="btn btn-primary btn-block" type=submit value="add" /> | |
</fieldset> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% macro post(msg,side_class) %} | |
<div class="pull-{{ side_class }} col-md-8 post"> | |
<div class="well well-sm"> | |
<a href="/remove/{{ msg.id }}"> | |
<span class=close> | |
X | |
</span> | |
</a> | |
<p>by: {{ msg.author }}. on {{ msg.date }}</p> | |
<p class=lead>{{ msg.title }}</p> | |
<p>{{ msg.content }}</p> | |
</div> | |
</div> | |
{% endmacro %} | |
{% if messages %} | |
<div class=row> | |
{% for msg in messages %} | |
{{ post(msg,loop.cycle('left','right')) }} | |
{% endfor %} | |
</div> | |
{% else %} | |
<p>No messages have been left yet</p>\ | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
there has been a lot of questions about if its necessary to use flask extensions to integrate third party library's into flask apps, but that is not the case. Here is a version of the flaskr example using wtforms and mongoengine without any flask specific extensions.