def user_listing(self): form = Form(self.request) path = self.request.matchdict['prefix'] user = Users.get_by_path(path) page = int(self.request.params.get("page", 1)) page_url = PageURL_WebOb(self.request) active_listings = DBSession.query(Listings).filter(Listings.user == user).filter(Listings.approved==True).\ filter(Listings.declined==False).filter(Listings.status==True).all() past_sales = DBSession.query(Listings).filter( Listings.user == user).filter(Listings.status == False).all() active_paginator = Page(active_listings, page=int(self.request.params.get("page", 1)), items_per_page=10, url=page_url) pastsales_paginator = Page(past_sales, page=int(self.request.params.get("page", 1)), items_per_page=10, url=page_url) title = user.fullname + "'s Listings" return dict(user=user, pastsales_paginator=pastsales_paginator, lis="d", form=FormRenderer(form), active_paginator=active_paginator, title=title)
def my_sub(request): user = request.user plan = None active_sub = None total_premium_listings = 0 total_premium_blogs = 0 if user.active_subscription: plan = user.active_subscription[0].plan active_sub = user.active_subscription[0] subscriptions = DBSession.query(Subscription).filter_by( user=user).order_by(Subscription.id.desc()).all() premium = DBSession.query(Featured_Content).filter( Featured_Content.name == 'Premium').first() if premium: premium_lis = premium.featured_properties if premium_lis: for item in premium_lis: if item.user == user: total_premium_listings += 1 premium_blogs = premium.featured_blogs if premium_blogs: for item in premium_blogs: if item.user == user: total_premium_blogs += 1 return dict(title="My subscriptions", total_premium_listings=total_premium_listings, total_premium_blogs=total_premium_blogs, user=user, subscriptions=subscriptions, plan=plan, active_sub=active_sub)
def from_category(request): c_id = request.matchdict['category_id'] category = PropertyCategory.get_by_id(c_id) if category.parent: title = "%s in Nigeria" % (category.name) ptype = category.id listings=DBSession.query(Listings).filter(Listings.approved==True).filter(Listings.declined==False).\ filter(Listings.category==category).order_by(Listings.modified.desc()).all() else: ptype = '' title = "%s Properties in Nigeria" % (category.name) category_children = [x.name for x in category.children] listings = DBSession.query(Listings).join(PropertyCategory).filter(Listings.approved==True).\ filter(Listings.declined==False).filter(PropertyCategory.name.in_(category_children)).order_by(Listings.modified.desc()).all() page_url = PageURL_WebOb(request) page = int(request.params.get("page", 1)) paginator = Page(listings, page=page, items_per_page=10, url=page_url) form = Form(request) return dict(title=title, paginator=paginator, category=category, form=FormRenderer(form), type='', min_price='', max_price='', state_id='', lga_id='', lgas=[], ptype=ptype, beds='', baths='', area_size='', covered_area='', transaction_type='')
def edit_blog(self): from buddy.models.properties_model import State def get_states(): query = [(s.id, s.name) for s in DBSession.query(State).all()] return query name = self.request.matchdict['name'] categories = get_categories() blog = Blogs.get_by_name(name) if not blog: self.session.flash("danger; No such blog post") return HTTPFound(location=self.request.route_url('blog_list')) title = "Editing " + blog.title form = Form(self.request, schema=BlogPost, obj=blog) if "form_submitted" in self.request.POST and form.validate(): form.bind(blog, exclude=['category_id']) for i, cat in enumerate(blog.categories): if cat.id not in form.data['category_id']: del blog.categories[i] catids = [cat.id for cat in blog.categories] for cate in form.data['category_id']: if cate not in catids: t = DBSession.query(BlogCategory).get(cate) blog.categories.append(t) blog.status = True DBSession.add(blog) DBSession.flush() return HTTPFound( location=self.request.route_url('blog_view', name=blog.name)) elif "draft" in self.request.POST and form.validate(): form.bind(blog, exclude=['category_id']) for i, cat in enumerate(blog.categories): if cat.id not in form.data['category_id']: del blog.categories[i] catids = [cat.id for cat in blog.categories] for cate in form.data['category_id']: if cate not in catids: t = DBSession.query(BlogCategory).get(cate) blog.categories.append(t) blog.status = 0 DBSession.add(blog) DBSession.flush() return HTTPFound( location=self.request.route_url('blog_view', name=blog.name)) return { 'form': FormRenderer(form), 'blog_nav_cat': get_navcategories(), 'title': title, 'categories': categories, 'blog': blog, 'wb': 'wb', 'states': get_states() }
def from_area(request): area_id = request.matchdict['area_id'] district = District.get_by_id(area_id) listings=DBSession.query(Listings).filter(Listings.approved==True).\ filter(Listings.declined==False).filter(Listings.district==district).order_by(Listings.modified.desc()).all() page_url = PageURL_WebOb(request) page = int(request.params.get("page", 1)) paginator = Page(listings, page=page, items_per_page=5, url=page_url) form = Form(request) title = "Properties in %s, %s,%s" % (district.name, district.lga.name, district.lga.state.name) return dict(title=title, paginator=paginator, district=district, form=FormRenderer(form), type='', min_price='', max_price='', state_id=district.lga.state.id, lga_id=district.lga.id, lgas=[(a.id, a.name) for a in district.lga.state.lga.all()], ptype='', beds='', baths='', area_size='', covered_area='', transaction_type='')
def from_state(request): state_name = request.matchdict['state_name'] state = State.get_by_name(state_name) listings=DBSession.query(Listings).filter(Listings.approved==True).\ filter(Listings.declined==False).filter(Listings.state==state).order_by(Listings.modified.desc()).all() page_url = PageURL_WebOb(request) page = int(request.params.get("page", 1)) paginator = Page(listings, page=page, items_per_page=10, url=page_url) title = "Properties in %s" % (state_name) form = Form(request) return dict(title=title, form=FormRenderer(form), paginator=paginator, state=state, type='', min_price='', max_price='', state_id=state.id, lga_id='', lgas=[(a.id, a.name) for a in state.lga.all()], ptype='', beds='', baths='', area_size='', covered_area='', transaction_type='')
def buy(self): form = Form(self.request) usertypes = userTypes() title = "Properties for sale in Nigeria" listing = DBSession.query(Listings).filter(Listings.listing_type=='For sale').\ filter_by(approved = True).filter(Listings.declined==False).\ filter_by(status=True).order_by(Listings.modified.desc()).all() page = int(self.request.params.get("page", 1)) page_url = PageURL_WebOb(self.request) paginator = Page(listing, page=page, item_count=len(listing), items_per_page=10, url=page_url) return dict(paginator=paginator, title=title, buy='buy', form=FormRenderer(form), usertypes=usertypes, type='Buy', min_price='', max_price='', state_id='', lga_id='', lgas=[], ptype='', beds='', baths='', area_size='', covered_area='', transaction_type='')
def all_listing(self): form = Form(self.request) usertypes = userTypes() title = 'Properties for sale and rent in Nigeria' page = int(self.request.params.get('page', 1)) paginator = Listings.get_paginator(self.request, page) pastsales = DBSession.query(Listings).filter(Listings.approved==True).\ filter(Listings.declined==False).filter(Listings.status==False).all() page_url = PageURL_WebOb(self.request) pastsales_paginator = Page(pastsales, page=int(self.request.params.get("page", 1)), items_per_page=5, url=page_url) self.request.response.cache_control.prevent_auto = True return dict(paginator=paginator, find='all', form=FormRenderer(form), usertypes=usertypes, title=title, pastsales_paginator=pastsales_paginator, type='', min_price='', max_price='', state_id='', lga_id='', lgas=[], ptype='', beds='', baths='', area_size='', covered_area='', transaction_type='')
def _to_python(self, value, user): 'Check whether the value is unique' if DBSession.query(Users).filter( getattr(Users, self.fieldName) == value).first(): raise Invalid(self.errorMessage, value, user) # Return return value
def index(self): form = Form(self.request) params = self.request.params protype = params.get('type','') location = params.get('cities_auto','') name = params.get('name','') users = DBSession.query(Users) if protype: users = users.join(User_types).filter(User_types.id==protype) if location and name: st = State.get_by_name(location) name="%"+name+"%" filters = or_(Users.fullname.like(name),Users.company_name.like(name)) users = users.filter(Users.state_id==st.id).filter(filters) if location and not name: st = State.get_by_name(location) users = users.filter(Users.state==st) if name and not location: namelike="%"+name+"%" filters = or_(Users.fullname.like(namelike),Users.company_name.like(namelike)) users = users.filter(filters) users = users.filter(and_(Users.email!=u'*****@*****.**',Users.email!=u'*****@*****.**')).order_by((Users.photo!=None).desc()).all() page_url = PageURL_WebOb(self.request) paginator = Page(users, page=int(self.request.params.get("page", 1)), items_per_page=12, url=page_url) return dict(title="Nairabricks Real Estate Professionals", name = name, location = location, protype= protype, form = FormRenderer(form),paginator=paginator)
def autoc(self): term = self.request.params.get('cities_auto') if term: term = '%' + term + '%' data = [] states = DBSession.query(State).filter(State.name.like(term)).all() lga = DBSession.query(LGA).filter(LGA.name.like(term)).all() district = DBSession.query(District).filter( District.name.like(term)).all() for state in states: data.append(state.name) for lg in lga: data.append(lg.name) for d in district: data.append(d.name) return data
def make_premium(self): for r in self.request.params: opts = r params = json.loads(opts) id = params['id'] user_id = params['user_id'] user = Users.get_by_id(user_id) positive = params['positive'] listing = Listings.get_by_id(id) if listing: if int(positive) == 1: #we already have it as premium, remove premium = DBSession.query(Featured_Content).filter( Featured_Content.name == 'Premium').first() premium.featured_properties.remove(listing) DBSession.flush() return dict(isOk=1, message="listing removed from premium") else: active_sub = user.active_subscription if not active_sub: return dict( isOk=0, message="Upgrade your account to feature this listing") premium = DBSession.query(Featured_Content).filter( Featured_Content.name == 'Premium').first() if premium.featured_properties: user_total_premium = 0 for item in premium.featured_properties: if item.user == user: user_total_premium += 1 if user_total_premium < active_sub[ 0].plan.max_premium_listings: premium.featured_properties.append(listing) DBSession.flush() return dict(isOk=1, message="listing given a premium identity") return dict( isOk=0, message= "You have exceeded your allocation. Upgrade for more") premium.featured_properties.append(listing) DBSession.flush() return dict(isOk=1, message="listing given a premium identity") return dict(isOk=0, message="No such listing")
def account_listing(self): form = Form(self.request) user = self.request.user page = int(self.request.params.get("page", 1)) page_url = PageURL_WebOb(self.request) active_listings = DBSession.query(Listings).filter(Listings.user == user).filter(Listings.approved == True). \ filter(Listings.declined == False).filter(Listings.status == True).all() onreview_listings = DBSession.query(Listings).filter(Listings.user == user).filter(Listings.approved == False). \ filter(Listings.declined == False).filter(Listings.status == True).all() declined_listings = DBSession.query(Listings).filter(Listings.user == user). \ filter(Listings.declined == True).filter(Listings.status == True).all() past_sales = DBSession.query(Listings).filter( Listings.user == user).filter(Listings.status == False).all() favourites = user.favourites active_paginator = Page(active_listings, page=int(self.request.params.get("page", 1)), items_per_page=10, url=page_url) declined_paginator = Page(declined_listings, page=int(self.request.params.get("page", 1)), items_per_page=10, url=page_url) pastsales_paginator = Page(past_sales, page=int(self.request.params.get("page", 1)), items_per_page=10, url=page_url) onreview_paginator = Page(onreview_listings, page=int(self.request.params.get("page", 1)), items_per_page=10, url=page_url) favourite_paginator = Page(favourites, page=int(self.request.params.get("page", 1)), items_per_page=10, url=page_url) title = user.fullname + "'s Listings" if page > 1: title = title + ' page ' + str(page) return dict(user=user, pastsales_paginator=pastsales_paginator, onreview_paginator=onreview_paginator, form=FormRenderer(form), active_paginator=active_paginator, declined_paginator=declined_paginator, title=title, favourites_paginator=favourite_paginator)
def urlify_name(title): contents = DBSession.query(Content).all() excluded = [u'add',u'buy',u'rentals',u'submit-for-sale',u'submit-for-rent', u'subcategories',u'fileuploads',u'start',u'details',u'uploads',u'finish',u'sendmailtoagents', u'save_favourites',u'voices',u'blogs',u'edit_step1',u'details_edit',u'upload_edit',u'add-answer',u'plans', u'pricing'] blacklist = [x.name for x in contents]+excluded return title_to_name(title,blacklist)
def get_categories(): p = DBSession.query(BlogCategory).all() d = [] for category in p: if category.children: f = [(child.id, child.name) for child in category.children] f = f, category.name d.append(f) return d
def listplans(request): plans = DBSession.query(Plans).order_by(Plans.id).all() form = Form(request, schema=PlanSchema()) if 'form_submitted' in request.POST and form.validate(): plan = form.bind(Plans()) DBSession.add(plan) DBSession.flush() return HTTPFound(location=request.route_url('list_plans')) return dict(form=FormRenderer(form), plans=plans, title="List of plans")
def get_property_features(): p = DBSession.query(Feature_types).all() d = [] for feature in p: if feature.features: f = [(child.id, child.name) for child in feature.features] f = f, feature.name d.append(f) return d
def liststate(request): title = "State List" page = int(request.params.get('page', 1)) paginator = State.get_paginator(request, page) total = DBSession.query(State).count() return dict( title=title, total=total, paginator=paginator, )
def editplans(request): pid = request.matchdict.get('id') plan = DBSession.query(Plans).get(pid) form = Form(request, schema=PlanSchema(), obj=plan) if 'form_submitted' in request.POST and form.validate(): form.bind(plan) DBSession.add(plan) DBSession.flush() return HTTPFound(location=request.route_url('list_plans')) return dict(form=FormRenderer(form), plan=plan, title=plan.name + " Edit")
def userlog(request): users = DBSession.query(AuthUserLog).order_by( AuthUserLog.time.desc()).all() title = 'Users logs' page_url = PageURL_WebOb(request) page = int(request.params.get("page", 1)) paginator = Page(users, page=page, items_per_page=10, url=page_url) if page > 1: title = title + ' page ' + str(page) return dict(paginator=paginator, title=title)
def add_g(request): title = 'Add group' groups = DBSession.query(Groups).all() form = Form(request, schema=AddGroup) if 'form_submitted' in request.POST and form.validate(): group = form.bind(Groups()) DBSession.add(group) DBSession.flush() redir_url = request.route_url('dashboard') return HTTPFound(location=redir_url) return dict(form=FormRenderer(form), groups=groups, title=title)
def get_categories_inside(): p = DBSession.query(BlogCategory).all() c = [] for category in p: if category.children: g = [category.name] d = [(child.id, child.name) for child in category.children] for i in d: g.append(i) c.append(OptGroup(*tuple(g))) return c
def accountblog(self): user = self.request.user blogs = DBSession.query(Blogs).filter(Blogs.user == user).filter( Blogs.status == True).all() drafts = DBSession.query(Blogs).filter(Blogs.user == user).filter( Blogs.status == False).all() form = Form(self.request) title = "My Blog post" page_url = PageURL_WebOb(self.request) page = int(self.request.params.get('page', 1)) postpaginator = Page(blogs, page=page, item_per_page=10, url=page_url) draftpaginator = Page(drafts, page=page, item_per_page=10, url=page_url) return dict(user=user, postpaginator=postpaginator, draftpaginator=draftpaginator, form=FormRenderer(form), title=title)
def verify(request): prefix = request.matchdict['prefix'] user = Users.get_by_path(prefix) if not user: request.session.flash('danger; user not found') user.is_verified = True user_parent = user.parent silver_plan = DBSession.query(Plans).filter(Plans.name == 'Silver').first() if user_parent: if not user_parent.earned_benefit: if user.active_subscription: active_sub = user.active_subscription[0] subscription = Subscription(user=user, plan=silver_plan, amount=0, no_of_months=1, discount=u"100%", status=u"Active") subscription.start_date = active_sub.end_date subscription.end_date = active_sub.end_date + timedelta( days=30) else: subscription = Subscription(user=request.user, plan=silver_plan, amount=0, no_of_months=1, discount=u"100%", start_date=datetime.today(), end_date=datetime.today() + timedelta(days=30), status=u"Active") DBSession.add(subscription) DBSession.flush() body = """<html><head><title>Verified on nairabricks.com</title></head><body> <p>Dear %s,<p><br> <p>You are now verified as a professional in Nairabricks</p> <p>However, please note that we still have the right to decline your listings if they violate our property listing policy</p> <p>Moreso, ensure that your listings are not duplicated. Instead of having duplicate listings, update your listing frequently to keep it at the top</p> <p>Yours sincerely,</p> <p>The Happy Nairabricks Info Robot</p> </body> </html> """ % user.fullname html_email_sender(request, subject="Congratulations", recipients=user.email, body=body) request.session.flash('success; user verified') return HTTPFound(location=request.route_url('user_list'))
def listing_category_list(request): """listing category list """ title = 'List of property categories' page = int(request.params.get('page', 1)) paginator = PropertyCategory.get_paginator(request, page) listings = DBSession.query(Listings).join(Photos).all() #a = [] #for lis in listings: # if len(lis.pictures.all())>0: # for p in lis.pictures.all(): #fname = os.path.splitext(p.filename)[0] #p.thumbnail = fname +'_t.jpg' # a.append(p.filename) return {'paginator': paginator, 'title': title}
def adminlistsearch(self): title = 'Unapproved Listings' listings = DBSession.query(Listings).filter( Listings.approved == False).filter( Listings.declined == False).all() page_url = PageURL_WebOb(self.request) paginator = Page(listings, page=int(self.request.params.get("page", 1)), items_per_page=5, url=page_url) return dict(paginator=paginator, title=title)
def promo_sub(request): id = request.matchdict['id'] user = Users.get_by_id(id) if not user: return HTTPNotFound() form = Form(request, schema=PromoSubSchema) if 'submit' in request.POST and form.validate(): plan_id = form.data['plan'] plan = DBSession.query(Plans).get(plan_id) if plan: subscription = Subscription(user=user, plan=plan, amount=0, no_of_months=1, discount="100%", status="Active", start_date=datetime.today(), end_date=datetime.today() + timedelta(days=30)) DBSession.add(subscription) DBSession.flush() if request.is_xhr: html = """<div class="alert alert-success alert-dismissable col-xs-12"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> User subscription </div>""" return Response(html) request.session.flash('success; User subscribed') return HTTPFound( location=request.route_url('profile', prefix=user.prefix)) if request.is_xhr: html = """<div class="alert alert-danger alert-dismissable col-xs-12"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> An error occured, user Not subscribed </div>""" return Response(html) request.session.flash('danger; An error occured, user subscribed %s' % form.all_errors()) return HTTPFound( location=request.route_url('profile', prefix=user.prefix)) if request.is_xhr: html = """<div class="alert alert-danger alert-dismissable col-xs-12"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> An error occured, user Not subscribed </div>""" return Response(html) request.session.flash('danger; An error occured, user subscribed %s' % form.all_errors()) return HTTPFound(location=request.route_url('profile', prefix=user.prefix))
def make_admin(request): id = request.matchdict['id'] user = Users.get_by_id(id) pos = request.matchdict['pos'] group = DBSession.query(Groups).filter(Groups.name == pos).first() if user and group: try: user.mygroups.append(group) request.session.flash('success; %s added to group %s' % (user.fullname, group.name)) except: request.session.flash('danger; request not completed') return HTTPFound(location=request.route_url('search_user_list')) request.session.flash('danger; Not successfull') return HTTPFound(location=request.route_url('user_list'))
def viewstate(request): state_id = request.matchdict['state_id'] state = State.get_by_id(state_id) title = "Viewing %s" %(state.name) lgas = DBSession.query(LGA).filter(LGA.state_id ==state_id).order_by(LGA.name).all() page_url = PageURL_WebOb(request) paginator = Page(lgas, page=int(request.params.get("page", 1)), items_per_page=20, url=page_url) return dict( state=state, paginator = paginator, title=title )
def userblog(self): path = self.request.matchdict['prefix'] user = Users.get_by_path(path) blogs = DBSession.query(Blogs).filter(Blogs.user == user).filter( Blogs.status == True).all() form = Form(self.request) title = user.fullname + "'s Blogs" page_url = PageURL_WebOb(self.request) page = int(self.request.params.get('page', 1)) paginator = Page(blogs, page=page, url=page_url) return dict(user=user, paginator=paginator, form=FormRenderer(form), bl='bl', title=title)