def initialize(): from flask_migrate import upgrade from app.models import Role, Ad upgrade() db.create_all() # Create the materialized view Role.insert_roles() Canton.insert() District.insert() Location.insert() Ad.insert_initial_xml() User.insert_default_user()
def details(id): ad = Ad.objects(id=id).first() return render_template( 'Details.html', title='Details', ad=ad, )
def advertising(request): num = Ad.objects.all().aggregate(Count('aid')) num = num['aid__count'] order = request.POST.get("排序") link = request.POST.get("地址") state = request.POST.get("form-field-radio1") photo = request.FILES.get("upload") time = datetime.datetime.now() xd = request.POST.getlist("check_box_list") for id in xd: pi = Ad.objects.get(aid=id) pi.delete() if photo: path = os.path.join(os.getcwd(), 'static\\upload') pathname = os.path.join(path, photo.name) path1 = default_storage.save(pathname, ContentFile(photo.read())) pathname2 = os.path.join('static/upload', photo.name).replace('\\', '/') u1 = Ad(path=pathname2, order=order, link=link, state=state, time=time) u1.save() shuju = Ad.objects.values().all() tt = request.POST.get("aa") norder = request.POST.get("norder") if tt == "显示": id = request.POST.get("xuan") pi = Ad.objects.get(aid=id) pi.state = 1 pi.save() if tt == "隐藏": id = request.POST.get("xuan") pi = Ad.objects.get(aid=id) pi.state = 0 pi.save() if tt == "修改": id = request.POST.get("xuan") pi = Ad.objects.get(aid=id) pi.order = norder pi.save() return render(request, 'advertising.html', context={ 'shuju': shuju, 'num': num })
def test_follow_ads(self): # create four users u1 = User(username='******', email='*****@*****.**') u2 = User(username='******', email='*****@*****.**') u3 = User(username='******', email='*****@*****.**') u4 = User(username='******', email='*****@*****.**') db.session.add_all([u1, u2, u3, u4]) # create four ads now = datetime.utcnow() p1 = Ad(description="ad from john", author=u1, timestamp=now + timedelta(seconds=1)) p2 = Ad(description="ad from susan", author=u2, timestamp=now + timedelta(seconds=4)) p3 = Ad(description="ad from mary", author=u3, timestamp=now + timedelta(seconds=3)) p4 = Ad(description="ad from david", author=u4, timestamp=now + timedelta(seconds=2)) db.session.add_all([p1, p2, p3, p4]) db.session.commit() # setup the followers u1.follow(u2) # john follows susan u1.follow(u4) # john follows david u2.follow(u3) # susan follows mary u3.follow(u4) # mary follows david db.session.commit() # check the followed ads of each user f1 = u1.followed_ads().all() f2 = u2.followed_ads().all() f3 = u3.followed_ads().all() f4 = u4.followed_ads().all() self.assertEqual(f1, [p2, p4, p1]) self.assertEqual(f2, [p2, p3]) self.assertEqual(f3, [p3, p4]) self.assertEqual(f4, [p4])
def create(): if request.method == 'POST': title = request.form.get('title') price = request.form.get('price') description = request.form.get('description') category = request.form.get('category') phone = request.form.get('phone') imageURL = '' file = request.files['imageFile'] if file: try: imageURL = CreateAdBlob(file) app.logger.info(imageURL) except Exception as e: app.logger.error(e) ad = Ad(title=title, price=price, description=description, imageURL=imageURL, category=category, phone=phone) try: ad.save() if ad.imageURL: msg = Message(str(ad.id).encode("utf-8")) bus_service.send_queue_message('adqueue', msg) return redirect(url_for('details', id=ad.id)) except Exception as e: app.logger.error(e) return redirect('create') return render_template('Create.html', title='Create', categoryList=CategoryList)
def post(self): ''' 添加 ''' args = parse_base.parse_args() space_id = args.get('space_id') url = args.get('url') info = args.get('info') img = args.get('img') name = args.get('name') sort = args.get('sort') model_data = Ad() model_data.space_id = space_id model_data.img = img model_data.url = url model_data.info = info model_data.name = name model_data.sort = sort model_data.last_editor = g.admin.username if model_data.add(): data = {'status': RET.Created, 'msg': '添加成功', 'data': model_data} return marshal(data, sing_fields) abort(RET.BadRequest, msg='添加失败,请重试')
def search(): if not g.search_form.validate(): return redirect(url_for('main.explore')) page = request.args.get('page', 1, type=int) ads, total = Ad.search(g.search_form.q.data, page, current_app.config['ADS_PER_PAGE']) next_url = url_for('main.search', q=g.search_form.q.data, page=page + 1) \ if total > page * current_app.config['ADS_PER_PAGE'] else None prev_url = url_for('main.search', q=g.search_form.q.data, page=page - 1) \ if page > 1 else None return render_template('search.html', title=_('Search'), ads=ads, next_url=next_url, prev_url=prev_url)
def new_ad(): form = AdForm() if form.validate_on_submit(): language = guess_language(form.description.data) if language == 'UNKNOWN' or len(language) > 5: language = '' new = Ad(title=form.title.data, category=form.category.data, description=form.description.data, language=language, author=current_user) db.session.add(new) db.session.commit() flash(_('New ad posted!')) return redirect(url_for('main.index')) return render_template('new_ad.html', title=_('New ad'), form=form)
def delete(id): ad = Ad.objects(id=id).first() if ad: app.logger.info(ad.id) try: DeleteAdBlob(ad) except Exception as e: app.logger.error(e) try: ad.delete() except Exception as e: app.logger.error(e) return redirect('index')
def edit(id): ad = Ad.objects(id=id).first() if request.method == 'POST': ad.title = request.form.get('title') ad.price = request.form.get('price') ad.description = request.form.get('description') ad.category = request.form.get('category') ad.phone = request.form.get('phone') file = request.files['imageFile'] if file: try: DeleteAdBlob(ad) ad.imageURL = CreateAdBlob(file) app.logger.info(ad.imageURL) except Exception as e: app.logger.error(e) try: ad.save() if ad.imageURL: msg = Message(str(ad.id).encode("utf-8")) bus_service.send_queue_message('adqueue', msg) except Exception as e: app.logger.error(e) finally: return redirect(url_for('details', id=ad.id)) return render_template('Edit.html', title='Edit', ad=ad, categoryList=CategoryList)
def import_xml(file_id, strongest_site_id, user_id=None, url=None): """ Import data from xml file into our ads database :param file_id: The id of the downloaded file :param strongest_site_id: The id of the company which we do not overwrite if we have multiple entries of the same property :param user_id: The user id. Should only be set when the user startet the import from hand :param url: The url to send the update :return: """ import datetime from app.models import Time, Location, Ad, File from app import db logger.info("Start importing file") today = datetime.datetime.today() xml_file = db.session.query(File).filter(File.id == file_id).first() # Ignore not founded file if not xml_file: logger.error( "The file with id {} was not found in the database".format( file_id)) return logger.debug("Read XML file with id {}".format(xml_file.path)) try: tree = ET.parse(xml_file.path) ads = tree.getroot() except Exception as e: logger.error("Could not read XML file " + e) xml_file.error = True xml_file.error_message = "{}".format(e) db.session.add(xml_file) db.session.commit() return # insert the actual date: logger.info("Prepare Time object") t = db.session.query(Time).filter(Time.date == today.date()).first() if t is None: business_day = True if today.weekday() < 5 else False quarter = (today.month - 1) // 3 + 1 t = Time(day=today.day, month=today.month, year=today.year, business_day=business_day, quarter=quarter, date=today) db.session.add(t) db.session.commit() # Prepare locations for fast import locations = {} db_locations = db.session.query(Location).all() for loc in db_locations: locations[loc.plz] = loc updated_ads = 0 inserted_ads = 0 not_updated_ads = set() # Prepare once the data dict data = { 'userid': user_id, 'file_id': file_id, 'current': None, 'total': len(ads) } logger.info("Start inserting ads") for i, ad in enumerate(ads): if ad[10] and ad[11].text != strongest_site_id: not_updated_ads.add((ad, "DUPLICATE")) continue # Check if this advertisement already exists in our database advertisement = db.session.query(Ad).filter( Ad.id == ad[0].text).first() if advertisement is None: # insert new add try: location = locations[int(ad[6].text)] except KeyError: # We could not find this location in our database not_updated_ads.add((ad, "LOCATION")) continue inserted_ads += 1 advertisement = Ad(id=ad[0].text, title=ad[1].text, type=ad[2].text, area=ad[3].text, price=ad[4].text.replace(',', '.'), rooms=ad[5].text.replace(',', '.'), location=location, created=t, ended=t, site_id=ad[11].text) else: # Update ad if its newer if advertisement.ended.date > t.date: # Ignore this ad cause we have newer data logger.info("Outdated add") not_updated_ads.add((ad, "OUTDATED")) continue updated_ads += 1 advertisement.ended = t # Replace price advertisement.price = ad[4].text.replace(',', '.') # Add advertisement to session db.session.add(advertisement) # Commit after 10000 entries if i % 5000 is 0: db.session.commit() # Only send update if user started import if user_id and url: data['current'] = i post(url, json=data) db.session.commit() logger.info("There are {} ads which were not inserted. " "{} new and {} updated".format(len(not_updated_ads), inserted_ads, updated_ads)) # update file xml_file.imported = datetime.date.today() db.session.add(xml_file) db.session.commit() # Only send update if user started import if user_id and url: data['current'] = len(ads) post(url, json=data)
def import_xml(file_id, strongest_site_id, user_id=None, url=None): """ Import data from xml file into our ads database :param file_id: The id of the downloaded file :param strongest_site_id: The id of the company which we do not overwrite if we have multiple entries of the same property :param user_id: The user id. Should only be set when the user startet the import from hand :param url: The url to send the update :return: """ import datetime from app.models import Time, Location, Ad, File from app import db logger.info("Start importing file") today = datetime.datetime.today() xml_file = db.session.query(File).filter(File.id == file_id).first() # Ignore not founded file if not xml_file: logger.error("The file with id {} was not found in the database".format(file_id)) return logger.debug("Read XML file with id {}".format(xml_file.path)) try: tree = ET.parse(xml_file.path) ads = tree.getroot() except Exception as e: logger.error("Could not read XML file " + e) xml_file.error = True xml_file.error_message = "{}".format(e) db.session.add(xml_file) db.session.commit() return # insert the actual date: logger.info("Prepare Time object") t = db.session.query(Time).filter(Time.date == today.date()).first() if t is None: business_day = True if today.weekday() < 5 else False quarter = (today.month - 1) // 3 + 1 t = Time(day=today.day, month=today.month, year=today.year, business_day=business_day, quarter=quarter, date=today) db.session.add(t) db.session.commit() # Prepare locations for fast import locations = {} db_locations = db.session.query(Location).all() for loc in db_locations: locations[loc.plz] = loc updated_ads = 0 inserted_ads = 0 not_updated_ads = set() # Prepare once the data dict data = {'userid': user_id, 'file_id': file_id, 'current': None, 'total': len(ads)} logger.info("Start inserting ads") for i, ad in enumerate(ads): if ad[10] and ad[11].text != strongest_site_id: not_updated_ads.add((ad, "DUPLICATE")) continue # Check if this advertisement already exists in our database advertisement = db.session.query(Ad).filter(Ad.id == ad[0].text).first() if advertisement is None: # insert new add try: location = locations[int(ad[6].text)] except KeyError: # We could not find this location in our database not_updated_ads.add((ad, "LOCATION")) continue inserted_ads += 1 advertisement = Ad(id=ad[0].text, title=ad[1].text, type=ad[2].text, area=ad[3].text, price=ad[4].text.replace(',', '.'), rooms=ad[5].text.replace(',', '.'), location=location, created=t, ended=t, site_id=ad[11].text) else: # Update ad if its newer if advertisement.ended.date > t.date: # Ignore this ad cause we have newer data logger.info("Outdated add") not_updated_ads.add((ad, "OUTDATED")) continue updated_ads += 1 advertisement.ended = t # Replace price advertisement.price = ad[4].text.replace(',', '.') # Add advertisement to session db.session.add(advertisement) # Commit after 10000 entries if i % 5000 is 0: db.session.commit() # Only send update if user started import if user_id and url: data['current'] = i post(url, json=data) db.session.commit() logger.info("There are {} ads which were not inserted. " "{} new and {} updated".format(len(not_updated_ads), inserted_ads, updated_ads)) # update file xml_file.imported = datetime.date.today() db.session.add(xml_file) db.session.commit() # Only send update if user started import if user_id and url: data['current'] = len(ads) post(url, json=data)
def add_ad(): a = Ad(name='first test name', href='tst.com.ua', id=23145) db.session.add(a) db.session.commit()