def test_model_form(self): db = self.db class BlogPost(db.Document): title = db.StringField(required=True, max_length=200) posted = db.DateTimeField(default=datetime.datetime.now) tags = db.ListField(db.StringField(max_length=50)) class TextPost(BlogPost): content = db.StringField(required=True) class LinkPost(BlogPost): url = db.StringField(required=True) # Create a text-based post TextPostForm = model_form(TextPost) form = TextPostForm(**{ 'title': 'Using MongoEngine', 'tags': ['mongodb', 'mongoengine']}) self.assertFalse(form.validate()) form = TextPostForm(**{ 'title': 'Using MongoEngine', 'content': 'See the tutorial', 'tags': ['mongodb', 'mongoengine']}) self.assertTrue(form.validate()) form.save() self.assertEquals(BlogPost.objects.count(), 1)
def user_add(): import bcrypt PostForm = model_form(User) form = PostForm(request.form) if request.method == 'POST': #and form.validate(): #the following is described in flask-uploads documentation filename = "" #store blank photo filename #initialize default user photo i = Image(image_path = 'default_image', is_profile=True) if 'photo' in request.files: filename = photos.save(request.files['photo']) i = Image(image_path = filename, is_profile=True) flash("Photo saved.") i.save() #hash password stored in database so that it is not viewable by people in Romania hashed = bcrypt.hashpw(request.form["password"], bcrypt.gensalt()) u = User( email = request.form["email"], first_name = request.form["first_name"], last_name =request.form["last_name"], password = hashed, authenticated = False, philosophy = request.form['philosophy'], tags = request.form['tags'].split(','), ) u.profile_picture= i u.save() return "done:" + request.form["email"] + " " + request.form["first_name"]+ " " + request.form["last_name"] + " " + '/_uploads/photos/'+i.image_path else: return render_template('add.html', form=form, cities = OccupyCity.objects.all())
def test_model_form(self): db = self.db class BlogPost(db.Document): title = db.StringField(required=True, max_length=200) posted = db.DateTimeField(default=datetime.datetime.now) tags = db.ListField(db.StringField(max_length=50)) class TextPost(BlogPost): content = db.StringField(required=True) class LinkPost(BlogPost): url = db.StringField(required=True) # Create a text-based post TextPostForm = model_form(TextPost) form = TextPostForm(**{ 'title': 'Using MongoEngine', 'tags': ['mongodb', 'mongoengine'] }) self.assertFalse(form.validate()) form = TextPostForm( **{ 'title': 'Using MongoEngine', 'content': 'See the tutorial', 'tags': ['mongodb', 'mongoengine'] }) self.assertTrue(form.validate()) form.save() self.assertEquals(BlogPost.objects.count(), 1)
def login(): import bcrypt PostForm = model_form(User) form = PostForm(request.form) if request.method == 'POST': #and form.validate(): email = request.form['email'] password = request.form['password'] # the password as entered in the form u = User.objects(email=email)[0] hashed = u.password # the stored, hashed password # login and validate the user... if bcrypt.hashpw(password, hashed) == hashed: login_user(u) flash("Logged in successfully.") return redirect(request.args.get("next") or url_for("logged_in")) else: return "password does not match email" return render_template("login.html", form=form)
super(QuestionForm, self).__init__(*args, **kwargs) cat_choices = [(str(c.id), c.name) for c in cdw.categories.all()] self.category.choices = cat_choices def to_question(self): try: user = cdw.users.with_id(self.author.data) except: user = None return Question( category=cdw.categories.with_id(self.category.data), author=user, text = self.text.data) MongoQuestionForm = model_form(Question) class PostForm(Form): yesno = TextField(validators=[AnyOf(["1","0"])]) text = TextField(validators=[ Length(min=1, max=140, message="Post must be between 2 and 140 characters"), Required(), does_not_have_bad_words]) author = TextField(validators=[Required(), check_if_user_does_not_exist]) origin = TextField(validators=[Required(), AnyOf(["web","kiosk","cell"])]) responseto = TextField(validators=[check_if_post_exists, Optional()]) follow_sms = TextField(validators=[AnyOf(["on","start","yes"]), Optional()]) follow_email = TextField(validators=[AnyOf(["on","start","yes"]), Optional()])
super(QuestionForm, self).__init__(*args, **kwargs) cat_choices = [(str(c.id), c.name) for c in cdw.categories.all()] self.category.choices = cat_choices def to_question(self): try: user = cdw.users.with_id(self.author.data) except: user = None return Question(category=cdw.categories.with_id(self.category.data), author=user, text=self.text.data) MongoQuestionForm = model_form(Question) class PostForm(Form): yesno = TextField(validators=[AnyOf(["1", "0"])]) text = TextField(validators=[ Length(min=1, max=140, message="Post must be between 2 and 140 characters"), Required(), does_not_have_bad_words ]) author = TextField(validators=[Required(), check_if_user_does_not_exist]) origin = TextField( validators=[Required(), AnyOf(["web", "kiosk", "cell"])])