Last active
December 21, 2015 05:29
-
-
Save cldershem/6257799 to your computer and use it in GitHub Desktop.
Troubleshooting hg-flask with mongoengine...
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 | |
from flask.ext.mongoengine import MongoEngine | |
from config import basedir | |
from flask.ext.login import LoginManager | |
app = Flask(__name__) | |
app.config.from_object('config') | |
db = MongoEngine(app) | |
lm = LoginManager() | |
lm.init_app(app) | |
from app import routes, models |
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
import os | |
basedir = os.path.abspath(os.path.dirname(__file__)) | |
DEBUG = True | |
CSRF_ENABLED = True | |
SECRET_KEY = 'SomethingSecret' | |
MONGODB_SETTINGS = { 'DB': "homelessgaffer" } |
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
class RegisterUser(Form): | |
firstname = TextField("First name", [validators.Required( | |
"Please enter your first name.")]) | |
lastname = TextField("Last name", [validators.Required( | |
"Please enter your last name.")]) | |
email = TextField("Email", [validators.Required( | |
"Please enter your email address."), | |
validators.Email( | |
"Please engter a valid email address.")]) | |
password = PasswordField('Password', [validators.Required( | |
"Please enter a password.")]) | |
submit = SubmitField("Create account") | |
def __init__(self, *args, **kwargs): | |
Form.__init__(self, *args, **kwargs) | |
def validate(self): | |
if not Form.validate(self): | |
return False | |
try: | |
user = User.objects.get(email=self.email.data.lower()) | |
self.email.errors.append("That email already exists.") | |
return False | |
except DoesNotExist: | |
return 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
class User(db.Document): | |
#uid = db.IntField(primary_key = True) | |
firstname = db.StringField(max_length=64) | |
lastname = db.StringField(max_length=100) | |
email = db.StringField(max_length=120, unique = True) | |
pwdhash = db.StringField(max_length=54) | |
def set_password(self, password): | |
self.pwdhash = generate_password_hash(password) | |
def check_password(self, password): | |
return check_password_hash(self.pwdhash, password) |
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
Flask==0.9 | |
Flask-Admin==1.0.6 | |
Flask-Login==0.2.6 | |
Flask-Script==0.6.2 | |
Flask-WTF==0.8.3 | |
Jinja2==2.6 | |
MarkupSafe==0.18 | |
Tempita==0.5.1 | |
WTForms==1.0.3 | |
Werkzeug==0.8.3 | |
argparse==1.2.1 | |
decorator==3.4.0 | |
flask-mongoengine==0.7.0 | |
itsdangerous==0.23 | |
mongoengine==0.8.3 | |
pymongo==2.5.2 | |
uWSGI==1.9.14 | |
wsgiref==0.1.2 |
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
@app.route('/register', methods = ['GET', 'POST']) | |
def register(): | |
form = RegisterUser() | |
if request.method == 'POST': | |
if form.validate() == False: | |
return render_template('register.html', form=form) | |
else: | |
newuser = User(firstname = form.firstname.data.title(), | |
lastname = form.lastname.data.title(), | |
email = form.email.data.lower(), | |
password = form.password.data) | |
newuser.save() | |
session['email'] = newuser.email | |
return "[1] Create a new user [2] sign in the user [3] redirect to the user's profile" | |
elif request.method == 'GET': | |
return render_template('register.html', form=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
Traceback (most recent call last): | |
File "/home/cldershem/Documents/Development/homelessgaffer.com/hg-Python/venv/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__ | |
return self.wsgi_app(environ, start_response) | |
File "/home/cldershem/Documents/Development/homelessgaffer.com/hg-Python/venv/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app | |
response = self.make_response(self.handle_exception(e)) | |
File "/home/cldershem/Documents/Development/homelessgaffer.com/hg-Python/venv/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app | |
response = self.full_dispatch_request() | |
File "/home/cldershem/Documents/Development/homelessgaffer.com/hg-Python/venv/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request | |
rv = self.handle_user_exception(e) | |
File "/home/cldershem/Documents/Development/homelessgaffer.com/hg-Python/venv/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request | |
rv = self.dispatch_request() | |
File "/home/cldershem/Documents/Development/homelessgaffer.com/hg-Python/venv/lib/python2.7/site-packages/flask/app.py", line 1344, in dispatch_request | |
return self.view_functions[rule.endpoint](**req.view_args) | |
File "/home/cldershem/Documents/Development/homelessgaffer.com/hg-Python/app/routes.py", line 91, in register | |
password = form.password.data) | |
File "/home/cldershem/Documents/Development/homelessgaffer.com/hg-Python/app/models.py", line 17, in __init__ | |
self.firstname = firstname.title() | |
File "/home/cldershem/Documents/Development/homelessgaffer.com/hg-Python/venv/lib/python2.7/site-packages/mongoengine/base/document.py", line 145, in __setattr__ | |
super(BaseDocument, self).__setattr__(name, value) | |
File "/home/cldershem/Documents/Development/homelessgaffer.com/hg-Python/venv/lib/python2.7/site-packages/mongoengine/base/fields.py", line 119, in __set__ | |
instance._data[self.name] = value | |
AttributeError: 'User' object has no attribute '_data' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment