def feed(feed): query = Proposal.public() # FIXME: better sorting community = None if feed == "popular": query = query.order_by(-Proposal.ranking) community = Community(name="popular", description="The most popular posts on Pnyx.") elif feed == "upvote": query = query.order_by(-(Proposal.upvotes - Proposal.downvotes)) community = Community(name="upvote", description="The most upvoted posts on Pnyx.") elif feed == "new": query = query.order_by(Proposal.timestamp.desc()) community = Community( name="new", description=gettext("The newest posts from all of Pnyx. Come here " "to see posts rising and be a part of " "the conversation.")) else: query = query.order_by(-Proposal.ranking, Proposal.timestamp.desc()) return object_list('community.html', query, community=community, check_bounds=False)
def post_submit(user=None, community=None): entry = Proposal(community='', title='', content='') if community is None: community = request.form.get('community') or None if community and community[0:2] == "c/": community = community[2:] if request.method == 'POST': try: entry.title = request.form.get('title') or '' entry.content = request.form.get('content') or '' entry.published = request.form.get('published') or True entry.vote_options = request.form.get('internalvote') or None entry.usepositions = (True if request.form.get('use-positions') == '' else False) entry.author = User.get(User.id == current_user.id) entry.community = Community.get(Community.name == community) if not (entry.title and entry.community and entry.author): flash('Community and Title are required.', 'error') else: entry.update_search_index() # Wrap the call to save in a transaction so we can roll it back # cleanly in the event of an integrity error. try: with database.atomic(): entry.save() except peewee.IntegrityError: flash('This title is already in use.', 'error') else: if entry.published: return redirect( url_for('post_details', community=entry.community.name, slug=entry.slug)) else: abort(404) except peewee.DoesNotExist: flash('Community and Title are required.', 'error') if community is not None: community = Community.get_or_none(Community.name == community) return render_template('submit.html', entry=entry, community=community)
def parse_list(html): bs = BeautifulSoup(html, 'html.parser') ul = bs.find('ul', attrs={'class': 'listContent'}) ui_list = ul.find_all('li') community_list = [] for ui in ui_list: name = ui.find('div', attrs={'class': 'title'}).find('a')['title'] a_ = ui.find('div', attrs={'class': 'houseInfo'}).find_all('a') recent_sale_count = str2num(a_[0].string.replace('90', '')) rent_count = str2num(a_[-1].string) recent_sale_price = ui.find('div', attrs={ 'class': 'xiaoquListItemPrice' }).find('span').string if recent_sale_price != '暂无数据': recent_sale_price = int(recent_sale_price) else: recent_sale_price = 0 sale_count = ui.find('div', attrs={ 'class': 'xiaoquListItemSellCount' }).find('span').string lianjia_id = ui['data-id'] print('小区名称:%s,90天成交量: %s,在租数量:%s,在售价格:%s,在售数量:%s,;lianjiaId:%s' % (name, recent_sale_count, rent_count, recent_sale_price, sale_count, lianjia_id)) community_list.append( Community(name=name, on_rent_num=rent_count, on_sale_num=recent_sale_count, ninety_days_deal_num=recent_sale_count, lianjia_id=lianjia_id, reference_price=recent_sale_price)) return community_list
def community(community): community_ref = Community.get_or_none(Community.name == community) return object_list('community.html', Proposal.from_community(community_ref).order_by( Proposal.timestamp.desc()), community=community_ref, check_bounds=False)
def post_revisions(community, slug): query = Proposal.public() community = Community.get_or_none(Community.name == community) entry = get_object_or_404(query, Proposal.slug == slug, Proposal.community == community) return render_template('post_revisions.html', entry=entry, community=community)
def community_create(): form = CommunityCreateForm() form.tags.choices = [ (t.title, t.title) for t in Tag.select(Tag.title).order_by(Tag.title).distinct() ] if form.validate_on_submit() and current_user.karma >= 50: community = Community() community.name = slugify(form.name.data) community.description = form.description.data community.maintainer = User.get(User.id == current_user.id) community.update_search_index() user = User.get(User.id == current_user.id) user.karma -= 50 user.save() success = True try: community.save() except peewee.IntegrityError: flash(gettext('This name is already in use.'), 'error') success = False else: try: for element in form.tags.data.split(','): if not element: continue tag = Tag() # FIXME: slugify? tag.title = element tag.community = community tag.save() except peewee.IntegrityError: flash(gettext('Unable to add tags.'), 'error') success = False if success: return redirect(url_for('community', community=community.name)) return render_template('community_create.html', form=form)
def test_top_users(self): user1 = User('user1', 'pw') user2 = User('user2', 'pw') user3 = User('user3', 'pw') community = Community('PL', None, user1, None, None) db.session.add_all([user1, user2, user3, community]) db.session.commit() community.users.append(user1) community.users.append(user2) community.users.append(user3) db.session.commit() #user3 will have 2 posts, user1 will have 1, and user2 will have 0 post1 = Posts('Title', 'Body', author=user3, community=community) post2 = Posts('Title', 'Body', author=user3, community=community) post3 = Posts('Title', 'Body', author=user1, community=community) db.session.add_all([post1, post2, post3]) db.session.commit() self.assertEqual(community.find_top_users(), [user3, user1, user2] )
def parse_detail(lianjia_id, html): community = Community(lianjia_id=lianjia_id) item_types = { '建筑类型': 'building_type', '物业费用': 'property_fee', '物业公司': 'property_company', '开发商': 'develop_company', '楼栋总数': 'building_num', '房屋总数': 'house_num' } bs = BeautifulSoup(html, 'html.parser') info_items = bs.find_all('div', attrs={'class', 'xiaoquInfoItem'}) for item in info_items: span_list = item.find_all('span') if len(span_list) == 2 and span_list[0].string in item_types: pro_name = item_types.get(span_list[0].string) pro_value = span_list[-1].string.strip() if 'building_num' == pro_name or 'house_num' == pro_name: pro_value = str2num(pro_value) community.__setattr__(pro_name, pro_value) else: print('解析小区基本信息异常,原始html:[%s]' % span_list) return community
def community_subscribe(community): fu = CommunityUser(community=Community.get(Community.name == community), user=User.get(User.id == current_user.id)) try: with database.atomic(): fu.save() except peewee.IntegrityError as e: print(e) else: return redirect(url_for("community", community=community)) # flash error message return redirect(url_for("community", community=community))
def community_search(): db_query = Community.select(Community.name, Community.description) search_query = request.args.get('q') if search_query: db_query = db_query.where(Community.prefix_name.contains(search_query)) db_query = db_query.order_by(Community.name).limit(30) # hits = [model_to_dict(hit, recurse=True) for hit in db_query] result = [] for hit in db_query: result.append({ "id": hit.name_with_prefix, "text": hit.name_with_prefix }) feeds = [{ "id": "", "text": "Frontpage" }, { "id": "f/popular", "text": "Popular" }, { "id": "f/new", "text": "New" }, { "id": "f/upvote", "text": "Most upvoted" }] # print(json.dumps({"suggestions": feeds + hits}, indent=4)) if request.args.get('s'): return jsonify({"results": result}) return jsonify({ "results": [{ "text": "Feeds", "children": feeds, }, { "text": "Other", "children": result, }] })
def create(self, db_session: Session, community_to_create: CommunityCreate, user_id: int): info = services.vk_service.get_community_info( community_to_create.api_key, community_to_create.community_vk_id) community = Community( community_vk_id=community_to_create.community_vk_id, avatar_url=info["photo_200"], name=info["name"], ) user = db_session.query(User).get(user_id) if user is not None: community.admins.append(user) db_session.add(community) db_session.commit() db_session.refresh(community) return community
def search(): search_query = request.args.get('q').strip() if search_query: try: post_query = Proposal.search(search_query) community_query = Community.search(search_query) return object_list('search.html', post_query, communitys=community_query, search=search_query, check_bounds=False) except (peewee.InternalError, peewee.IntegrityError, peewee.ProgrammingError): # flash("Invalid query '{0}'".format(search_query), 'error') pass return render_template('search.html', search=search_query, pagination=None)
def test_render_all_posts(self): user1 = User('user1', 'pw') user2 = User('user2', 'pw') c1 = Community('c1', None, founder=user1, FAQ=None, description=None) c2 = Community('c2', None, founder=user1, FAQ=None, description=None) db.session.add_all([user1, user2, c1, c2]) db.session.commit() post1 = Posts('T1', 'Body', author=user1, community=c1) post2 = Posts('T2', 'Body', author=user2, community=c1) post3 = Posts('T3', 'Body', author=user1, community=c2) post4 = Posts('T4', 'Body', author=user2, community=c2) db.session.add_all([post1, post2, post3, post4]) c1.join(user1) c2.join(user1) self.assertEqual(user1.render_all_community_posts().all(), [post4, post3, post2, post1])
def addcommunity(request): if not isEmpty(request.POST,"community") and not isEmpty(request.POST,"number") and not isEmpty(request.POST,"nick") : rs = Community.objects.filter(name=request.POST["community"],number=request.POST["number"]).count() if rs > 0 : return HttpResponse(json.dumps({ "valid": False }),content_type='application/json; charset=utf8') community = Community() community.user = request.user community.code = request.POST["code"] community.number = request.POST["number"] community.name = request.POST["community"] community.save() groupProfile = UserGroupProfile() groupProfile.user=request.user groupProfile.community=community groupProfile.nick= request.POST["nick"] groupProfile.save() return HttpResponseRedirect(redirect_to="/") else: return HttpResponseBadRequest()
def community_rss(community): community_ref = Community.get_or_none(Community.name == community) response = make_response(community_ref.rss_feed) response.headers['Content-Type'] = 'application/rss+xml; charset=utf-8' return response
def test_users_and_communities(self): u = User('ben', 'Password1') c1 = Community('community1', None, u, None, None) c2 = Community('community2', 'Password123', u, None, None) db.session.add_all([u, c1, c2]) db.session.commit() c1.join(u) self.assertFalse(c1.is_moderator(u)) c1.assign_moderator(u) self.assertEqual(u"%s" % u.id, u.get_id()) self.assertEqual(u, c1.founder) self.assertEqual(u, c2.founder) self.assertFalse(u.password == 'Password1') self.assertTrue(u in c1.users.all()) self.assertTrue(c1.is_joined(u)) self.assertTrue(c1.is_moderator(u)) c1.remove_moderator(u) self.assertFalse(c1.is_moderator(u)) c1.assign_moderator(u) c1.leave(u) self.assertFalse(c2.password == 'Password123') self.assertFalse(c1.is_joined(u)) self.assertFalse(c1.is_moderator(u)) self.assertFalse(c1.private) self.assertTrue(c2.private)