def add_page_pair_to_database(from_page, to_page, limit): with db_lock: cou = session.query(Page.id).filter(Page.url == from_page).scalar() cou1 = session.query(Page.id).filter(Page.url == to_page).scalar() if cou is None: new_page_from = Page(url=from_page, text="", rank=0) session.add(new_page_from) session.flush() id0 = new_page_from.id else: id0 = cou if cou1 is None: allowed = limit < 1 or limit > session.query(Page).count() if not allowed: return new_page_to = Page(url=to_page, text="", rank=0) session.add(new_page_to) session.flush() id1 = new_page_to.id else: id1 = cou1 new_relation = Relation(page_id = id0, destination_id = id1) # print(new_relation.page_id.id) session.add(new_relation) session.commit()
def add_page_pair_to_database(from_page, to_page, limit): with db_lock: cou = session.query(Page.id).filter(Page.url == from_page).scalar() cou1 = session.query(Page.id).filter(Page.url == to_page).scalar() if cou is None: new_page_from = Page(url=from_page, text="", rank=0) session.add(new_page_from) session.flush() id0 = new_page_from.id else: id0 = cou if cou1 is None: allowed = limit < 1 or limit > session.query(Page).count() if not allowed: return new_page_to = Page(url=to_page, text="", rank=0) session.add(new_page_to) session.flush() id1 = new_page_to.id else: id1 = cou1 new_relation = Relation(page_id=id0, destination_id=id1) # print(new_relation.page_id.id) session.add(new_relation) session.commit()
def addlocation(): if request.method == 'POST': #.get using the key photo_id to get the value which is the photo_id photo_id = session.get('photo_id') city = request.form['city'] goo = geocoders.GoogleV3() geocodes = goo.geocode(city, exactly_one=False) l2 = str(geocodes) latitude = lat2(l2) longitude = lon2(l2) # query by photo_id and update latlng db_session.query(Photo).filter_by(id=photo_id).update({ "latitude": latitude, "longitude": longitude }) db_session.commit() db_session.flush() del session['photo_id'] return redirect(url_for('userpage')) return render_template("upload2.html")
def setup_feed(feed_name, feed_link): xml = download_feed_xml(feed_link) feed = get_feed_from_db(feed_name) if feed is not None: feed.xml = compare_return_latest(feed.xml, xml) else: feed = Feed(name=feed_name, link=feed_link, xml=xml) session.add(feed) print "Added Feed %s" % feed.name session.flush() session.commit() return feed
def addlocation(): if request.method == 'POST': #.get using the key photo_id to get the value which is the photo_id photo_id = session.get('photo_id') city = request.form['city'] goo = geocoders.GoogleV3() geocodes = goo.geocode(city, exactly_one=False) l2 = str(geocodes) latitude = lat2(l2) longitude = lon2(l2) # query by photo_id and update latlng db_session.query(Photo).filter_by(id=photo_id).update({"latitude": latitude, "longitude": longitude}) db_session.commit() db_session.flush() del session['photo_id'] return redirect(url_for('userpage')) return render_template("upload2.html")
def process_item(self, item, spider): tag = item.get('tag', '') if tag == 'sohu_brand': process_sohu_brand(item, item.get('sohu_id')) elif tag == 'sohu_company': process_sohu_company(item) elif tag == 'sohu_series': process_sohu_series(item, item.get('sohu_id')) elif tag == 'sohu_type': process_sohu_type(item, item.get('sohu_id')) elif tag == 'maintenance_info': process_maintenance_information(item) elif tag == 'basic_data': process_maintain_basic_data(item) else: return try: session.commit() except: session.rollback() finally: session.flush()
def create_task(): """ Create a task with payload """ uid, err = get_oemid(request=request) if err is not None: return jsonify(UNAUTH_RESULT) ctx.current_user = DccaUser.get_by_id(uid) check_json(request) task_info = get_json(request).get("task", {}) device_ids = task_info.get('device_ids') task_type = task_info.get('type') payloads = task_info.get('payload') devices = Host.query_in(device_ids) if len(devices) != len(device_ids): return jsonify({ 'status': 'fail', 'message': 'Not authorized devices exist. Device not exist or cannot access', 'not_authorized_devices': list(set(device_ids) - set([d.id for d in devices])) }) try: inst_list = [] if task_type == TASK_TYPE_NAMES[TASK_TYPE_APP]: unauthoried = EsTask.check_da_payload(payloads) if unauthoried: return jsonify({ 'status': 'fail', 'message': 'Unauthorized application exists, not exist or cannot access. Do you have "version" in payload?', 'unauthorized': unauthoried }) task = EsTask.create_da_task(payloads) task.started_at = datetime.utcnow() session.add(task) session.flush() for pl in payloads: softapp = DccaSoftapp.get(pl['softapp_id']) del pl['softapp_id'] for device in devices: inst = task.da_inst(device, softapp, pl) session.add(inst) inst_list.append(inst) elif task_type == TASK_TYPE_NAMES[TASK_TYPE_SOLUTION]: authed, solution = EsTask.check_ota_payload(payloads) if not authed: return jsonify({ 'status': 'fail', 'message': 'Solution not exist or cannot access.' }) task = EsTask.create_ota_task(payloads) task.started_at = datetime.utcnow() session.add(task) session.flush() for device in devices: inst = task.ota_inst(device, solution) session.add(inst) inst_list.append(inst) else: if not payloads: return jsonify({ 'status': 'fail', 'message': 'payload is null.' }) if not isinstance(payloads, dict): return jsonify({ 'status': 'fail', 'message': 'payload is not dict.' }) p = payloads.copy() p['device_ids'] = device_ids task = EsTask.create_common_task(TASK_TYPE_COMMON, p) task.started_at = datetime.utcnow() session.add(task) session.flush() for device in devices: inst = task.common_inst(device, payloads) session.add(inst) inst_list.append(inst) session.commit() data = [] for inst in inst_list: data.append( dict( { 'mid': inst.id, 'device': inst.device.name, 'issync': True, }, **payloads)) params = None deploy_url = RESOURCE_POST_TASK.format(dns=APPSERVER_HOST, port=APPSERVER_PORT, uid=uid) task.start(deploy_url, params, data=json.dumps(data)) session.commit() for inst in inst_list: if inst.task.type == TASK_TYPE_APP: print('Start da task') try: inst.start(APPSERVER_HOST, APPSERVER_PORT) session.add(inst) except Exception: session.rollback() else: session.commit() elif inst.task.type == TASK_TYPE_SOLUTION: print('Start ota task') try: inst.start(MQTT_HOST) session.add(inst) except Exception: session.rollback() else: session.commit() results = OrderedDict() results['status'] = 'success' results['message'] = 'Success to create the task' results['task'] = task.as_dict(schema=TaskSchema) return jsonify(results) except Exception as e: raise DCCAException(str(e))