def preference(): form = PeferForm() user = g.user if form.validate_on_submit() and user.role == 0: pref = Preference.query.filter_by(user_id=user.id).first() if pref is None: pref = Preference(style=form.style.data, bedroom_no=form.bedroom_no.data, bathroom_no=form.bathroom_no.data, garage_no=form.garage_no.data, location=form.location.data, price=form.price.data, user_id=user.id, notify=form.notify.data) else: pref.style = form.style.data pref.bedroom_no = form.bedroom_no.data pref.bathroom_no = form.bathroom_no.data pref.garage_no = form.garage_no.data pref.location = form.location.data pref.price = form.price.data pref.notify = form.notify.data db.session.add(pref) db.session.commit() flash('Your preference is set! ') return redirect(url_for('user', nickname=user.nickname)) elif request.method != "POST" and user.pref is not None: form = PeferForm(obj=user.pref) return render_template('preference.html', form=form)
def list_post(page=1): form = PeferForm() user = g.user posts = Post.query.filter(Post.id == Post.id).order_by( Post.timestamp.desc()).paginate(page, POSTS_PER_PAGE, False) if form.validate_on_submit() and user.role == 0: pref = Preference(style=form.style.data, bedroom_no=form.bedroom_no.data, bathroom_no=form.bathroom_no.data, garage_no=form.garage_no.data, location=form.location.data, price=form.price.data) results = Post.query.filter(Post.style == pref.style).filter( Post.location == pref.location).filter( Post.price >= 0.8 * pref.price).filter( Post.price <= 1.2 * pref.price).filter( Post.bedroom_no >= pref.bedroom_no - 1).filter( Post.bedroom_no <= pref.bedroom_no + 1).order_by( Post.timestamp.desc()) posts = results.paginate(page, POSTS_PER_PAGE, False) flash('Find ' + str(results.count()) + ' matching results') return render_template('list_post.html', title='All the Houses', posts=posts, form=form)
def preferences_auctions(request): shop = request.shop preferences = Preference.get_preference(shop) if request.method == "POST": form = AuctionsPreferenceForm(request.POST, instance=preferences) if form.is_valid(): preferences = form.save(commit=False) preferences.shop = shop request.flash["message"] = unicode(_("Auctions preferences successfully saved.")) request.flash["severity"] = "success" return HttpResponseRedirect(reverse("preferences_auctions")) else: form = AuctionsPreferenceForm(instance=preferences) return render_to_response("preferences/preferences_auctions.html", {"form": form}, RequestContext(request))
def test_invalid_user_id(self): """Test for invalid user id in prefs""" u3 = User.register("testprefs", "prefsword") uid3 = 333 u3.id = uid3 db.session.add(u3) db.session.commit() p3 = Preference(user_id=99999, fav_team_id=2) db.session.add(p3) with self.assertRaises(exc.IntegrityError) as context: db.session.commit()
def test_no_user_id(self): """Test for None value user id in prefs""" u2 = User.register("testprefs", "prefsword") uid2 = 222 u2.id = uid2 db.session.add(u2) db.session.commit() p2 = Preference(user_id=None, fav_team_id=2) db.session.add(p2) with self.assertRaises(exc.IntegrityError) as context: db.session.commit()
def test_prefs_models(self): """Test basic preference functionality""" u = User.register("testprefs", "prefsword") uid = 222 u.id = uid db.session.add(u) db.session.commit() p = Preference(user_id=u.id, fav_team_id=2) db.session.add(p) db.session.commit() ptest = Preference.query.get(p.user_id) self.assertEqual(ptest.fav_team_id, 2)
def preferences_auctions(request): shop = request.shop preferences = Preference.get_preference(shop) if request.method == 'POST': form = AuctionsPreferenceForm(request.POST, instance=preferences) if form.is_valid(): preferences = form.save(commit=False) preferences.shop = shop request.flash['message'] = unicode( _("Auctions preferences successfully saved.")) request.flash['severity'] = "success" return HttpResponseRedirect(reverse('preferences_auctions')) else: form = AuctionsPreferenceForm(instance=preferences) return render_to_response('preferences/preferences_auctions.html', {'form': form}, RequestContext(request))
def setUp(self): """Create test client, add sample data""" db.drop_all() db.create_all() u1 = User.register("test1", "passwordtest1") uid1 = 1111 u1.id = uid1 db.session.commit() p1 = Preference(user_id=u1.id, fav_team_id=25) db.session.commit() self.p1 = p1 self.client = app.test_client()
def preferences_general(request): shop = request.shop profile = shop.admin.get_profile() preferences = Preference.get_preference(shop) form = GeneralPreferenceForm(request.POST or None, instance=preferences) profile_form = ProfileForm(request.POST or None, instance=profile) if form.is_valid() and profile_form.is_valid(): preferences = form.save(commit=False) preferences.shop = shop preferences.save() profile = profile_form.save(commit=True) shop.update_geolocation() request.flash["message"] = unicode(_("General preferences successfully saved.")) request.flash["severity"] = "success" return HttpResponseRedirect(reverse("preferences_general")) return render_to_response( "preferences/preferences_general.html", {"form": form, "profile_form": profile_form}, RequestContext(request) )
def register(): """Register a user: receive JSON form data and submit to DB""" success = {'user':{}} form = RegisterForm() if form.validate_on_submit(): username = request.json["username"] password = request.json["password"] try: user = User.register(username, password) db.session.add(user) db.session.commit() prefs = Preference(user_id=user.id) log = Login.record_login(user.id) db.session.add(prefs) db.session.commit() session['username'] = user.username session['user_id'] = user.id success['user']['login'] = True success['user']['username'] = session['username'] success['user']['userId'] = session['user_id'] return jsonify(success), 201 except exc.IntegrityError: db.session.rollback() success['login'] = False success['message'] = "taken" return jsonify(success), 400 else: success['login'] = False success['message'] = "invalid" return jsonify(success), 400
def preferences_general(request): shop = request.shop profile = shop.admin.get_profile() preferences = Preference.get_preference(shop) form = GeneralPreferenceForm(request.POST or None, instance=preferences) profile_form = ProfileForm(request.POST or None, instance=profile) if form.is_valid() and profile_form.is_valid(): preferences = form.save(commit=False) preferences.shop = shop preferences.save() profile = profile_form.save(commit=True) shop.update_geolocation() request.flash['message'] = unicode( _("General preferences successfully saved.")) request.flash['severity'] = "success" return HttpResponseRedirect(reverse('preferences_general')) return render_to_response('preferences/preferences_general.html', { 'form': form, 'profile_form': profile_form }, RequestContext(request))