def rebuild_doc_dict(cls, db, docDict): docDict = super(Job, cls).rebuild_doc_dict(db, docDict) if 'worker' in docDict: workerId = docDict['worker'] try: workerId = ObjectId(workerId) except InvalidId: return docDict worker = db.Worker.find_one({'_id':workerId}) docDict['worker'] = worker if 'device' in docDict: deviceId = docDict['device'] device = db.Device.find_one({'udid':deviceId}) if device: docDict['device'] = device else: logger.debug('given string %s is not a device udid' % deviceId) try: deviceId = ObjectId(deviceId) except InvalidId: logger.debug('given string %s is not a deviceId' % deviceId) device = db.Device.find_one({'_id':deviceId}) if device: docDict['device'] = device return docDict
def knightReply(oid, content): id = session['id'] oid = str(oid) oid = ObjectId(oid) todaydata = datetime.today() time = todaydata.strftime('%y-%m-%d %H:%M:%S') try: if (content == ""): return 0 od = db.itxia_database.orders.Order.one({'_id': oid}) rep = { 'username': session['account'], 'created_time': time, 'content': content } if od['comments']: od['comments'].append(rep) else: od['comments'] = [ rep, ] if session['status'] == 'helper': od['replybool'] = 1 else: od['replybool'] = 0 od.save() return 1 except Exception, e: print e return 0
def get_current_user(self): try: _u = json.loads(self.get_secure_cookie('authed_user')) # query memcache here return self.db.User.find_one({'_id': ObjectId(_u['user'])}) except: return None
def find_one(id): f = fs.get(ObjectId(id)) if f: blob = Blob(f.read()) return blob else: return None
def post_data(): # JSON or GTFO if request.headers['Content-Type'] != 'application/json': abort(406) objectid = request.get_json().get('id', None) install_type = request.get_json().get('install-type', None) ksdata = request.get_json().get('ks-data', None) userdata = request.get_json().get('user-data', None) metadata = request.get_json().get('meta-data', None) if (install_type is not None and install_type not in [u'cloud-init', u'kickstart']): abort(400) # Create new document if None if objectid is None: status = u'ok' doc = db.Configdata() doc['created_at'] = datetime.utcnow() doc['install_type'] = install_type doc['phonehome_status'] = False else: status = u'updated' try: doc = db.Configdata.fetch_one({'_id': ObjectId(objectid)}) if doc is None: abort(404) except pymongo_exceptions.InvalidId: doc = None abort(404) try: if ksdata is not None: doc['ksdata'] = unicode(ksdata) if userdata is not None: doc['userdata'] = unicode(userdata) if metadata is not None: doc['metadata'] = unicode(metadata) doc.save() except Exception as ex: app.logger.error("Couldn't save document: {}".format(ex)) abort(500) docid = unicode(doc['_id']) zeroconf_url = "{}://{}{}".format(app.config['URL_SCHEME'], app.config['ZEROCONF_IP'], url_for('get_data', objectid=docid)) ipv4_url = "{}://{}{}".format(app.config['URL_SCHEME'], app.config['HOSTNAME'], url_for('get_data', objectid=docid)) return jsonify(id=docid, created_at=doc['created_at'].strftime('%c'), install_type=doc['install_type'], ipv4_url=ipv4_url, zeroconf_url=zeroconf_url, ttl=app.config['DOC_LIFETIME'], status=status, phonehome_status=doc['phonehome_status'])
def post(self, cid): oid = ObjectId(cid) comic = self.conn.Comic.one({'_id': oid}) captions = [] changed = False # process deletes FIRST for k in self.request.arguments: if k.startswith('delcaption_'): index = 0 for cc in comic.captions: if cc.get('title') == self.get_argument(k): del comic.captions[index] changed = True index += 1 # process additions for k in self.request.arguments: if k.startswith('caption_'): ckey = k.replace('caption_', '') newc = self.conn.Caption() newc.title = unicode(ckey) newc.coords = self.get_argument(k) newc.save() captions.append(newc) if captions: comic.captions.extend(captions) comic.enabled = True comic.save() elif changed: comic.save() self.redirect(self.reverse_url('admin-comic', unicode(comic._id)))
def get(self, cid): oid = ObjectId(cid) comic = self.conn.UserComic.one({'_id': oid}) self.render(USER_TPL, **{ 'comic': comic, })
def phone_home(objectid=None): try: try: doc = db.Configdata.fetch_one({'_id': ObjectId(objectid)}) if doc is None: abort(404) except pymongo_exceptions.InvalidId: doc = None abort(404) if request.method == 'POST': doc['phonehome_time'] = datetime.utcnow() doc['phonehome_status'] = True doc['phonehome_data'] = request.form.to_dict() doc.save() return jsonify( phonehome_time=doc['phonehome_time'], phonehome_status=doc['phonehome_status'], phonehome_data=doc['phonehome_data'], ) except werkzeug_exceptions.HTTPException as ex: raise ex # keep raising except Exception as ex: app.logger.error(ex) abort(500)
def read_file(self, key): from mongokit import ObjectId fs = self.app.services['fs'] f = fs.get(ObjectId(key)) data = f.read() f.close() return data
def delOrder(oid): oid = ObjectId(oid) if session['account'] == getOrderPhone(oid): try: db.itxia_database.orders.remove({'_id': oid}) return 1 except Exception, e: print e return None
def changeFinish(oid): try: oid = ObjectId(oid) od = db.itxia_database.orders.Order.one({'_id': oid}) od['status'] = -1 od.save() return 1 except Exception, e: print e return None
def new(structure_id=None): #get user's role user_role = get_user_role() if user_role in video_permission_list: hl_obj = db.HighlightItem() hl_obj['_id'] = ObjectId() #set default for platforms and geoip attribute platforms = db.Platform.get_all() p_default = [] p_choices = [] for p in platforms: p_default.append(p.type) p_choices.append((p.type, p.type)) hl_obj.type = 'vod' #set default to vod hl_obj.platform = p_default hl_obj.GEOIP_COUNTRY_CODE_availability = ['VN', 'REST_OF_THE_WORLD'] #set default image_type hl_obj.image_type = 'standing_image' if structure_id=='55c42f6417dc1344d5012f5a' else 'small_image' hl_obj.publish = int(1) form = HighlightItemForm(request.form, obj=hl_obj) #init highlight form if request.method == 'POST': form.populate_obj(hl_obj) #Populates the attributes of the passed obj with data from the form’s fields #------------------manually validating fields------------------------ if len(hl_obj.structure_id) == 0: flash("You must select atleast 1 structure for this highlight!", "fail") return redirect(url_for("highlight_views.index")) #check available platform and GEOIP_COUNTRY_CODE_availability base on its parent structure check_platform_n_publish_country(hl_obj) #check priority highlight structure when save check_structure_and_priority_video(hl_obj) #-------------------------------------------------------------------- #check if the type is livetv, the highlight should auto turn off after the endtime of event. Call iapi to do the appointment check_set_appointment_tv_event(hl_obj) hl_obj.save() #logging log_activity(hl_obj, hl_obj, 'highlight', 'add', 'add highlight', 'add a highlight') flash("Highlight has been added!", "success") return redirect(url_for("highlight_views.index", structure_id=hl_obj.structure_id[0])) #get choices for structure select box set_structure_id_choice(form, [structure_id]) #get choices for platform select box form.platform.choices = p_choices return render_template('highlight/highlight_newedit.html',form=form, hl_obj=hl_obj, user_role=user_role, current_app=current_app) return 'permission denied', 403
def changeWait(oid): try: oid = ObjectId(oid) od = db.itxia_database.orders.Order.one({'_id': oid}) od['status'] = 0 od['handler'] = None od.save() return 1 except Exception, e: print e return None
def changeWork(oid): try: oid = ObjectId(oid) od = db.itxia_database.orders.Order.one({'_id': oid}) od['status'] = 1 od['handler'] = session['account'] od.save() return 1 except Exception, e: print e return None
def rebuild_doc_dict(cls, db, docDict): # print "rebuild_doc_dict!" if not docDict: return {} if '_id' in docDict: idVal = docDict['_id'] # print "cls: " + str(cls) + " id: " + str(idVal) if isinstance(idVal, basestring): idVal = ObjectId(idVal) if idVal: docDict['_id'] = idVal return docDict
def rebuild_doc_dict(cls, db, docDict): docDict = super(Result, cls).rebuild_doc_dict(db, docDict) if 'run' in docDict: runId = docDict['run'] try: runId = ObjectId(runId) except InvalidId: return docDict run = db.Run.find_one({'_id':runId}) docDict['run'] = run return docDict
def modifyOrder(oid, bbs, local, model, os, desc, name): try: oid = ObjectId(oid) od = db.itxia_database.orders.Order.one({'_id': oid}) od['lilybbs_id'] = bbs od['campus'] = local od['machine_model'] = model od['operate_system'] = os od['description'] = desc od['name'] = name od.save() return 1 except Exception, e: print e return None
def verify_token(): token = request.headers.get('Authorization', None) if token is None: raise AuthenticationFailed( 'Authorization Required, Authorization header was missing') uid = extract_token(token) ObjectIdStructure(uid) user_id = ObjectId(uid) current_user = current_app.mongodb_conn.User.find_one({'_id': user_id}) if current_user is None: raise AuthenticationFailed("User Not Exist") g.current_user = current_user return
def rebuild_doc_dict(cls, db, docDict): docDict = super(Run, cls).rebuild_doc_dict(db, docDict) if 'app' in docDict: appId = docDict['app'] try: appId = ObjectId(appId) except InvalidId: return docDict app = db.AppStoreApp.find_one({'_id':appId}) if not app: app = db.CydiaApp.find_one({'_id':appId}) docDict['app'] = app return docDict
def encoding_status(): ''' get status of job encoded. input: - job_id: a string object returns the status of job ''' if not request.json or not 'job_id' in request.json: return abort(400) job_id = request.json.get('job_id') valid = ObjectId.is_valid(job_id) if valid == False: return jsonify({'result':'False', 'info':'job id is not valid, cannot get status of video!'}) job = mdb.EncodingJob.get_job(job_id) if job: return jsonify({'result': 'Done', 'status': job['status'], 'progressive': job['progressive']}) return jsonify({'result': 'False', 'info': 'job not found'})
def get_data(objectid=None, field=None): try: # Get doc try: doc = db.Configdata.fetch_one({'_id': ObjectId(objectid)}) if doc is None: abort(404) except pymongo_exceptions.InvalidId: doc = None abort(404) # url is based on the request so we can handle zeroconf and ipv4 url = "{}://{}{}".format(app.config['URL_SCHEME'], request.headers['host'], url_for('get_data', objectid=str(doc['_id']))) # cloud-init resp = None if doc['install_type'] == u'cloud-init': if field is None: resp = render_template('base.jinja2', url=url) elif field == u'meta-data': resp = render_template('data.jinja2', data=doc['metadata']) elif field == u'user-data': resp = render_template('data.jinja2', data=doc['userdata']) else: abort(404) return Response(resp, mimetype='text/plain') # kickstart elif doc['install_type'] == u'kickstart': resp = render_template('data.jinja2', data=doc['ksdata']) return Response(resp, mimetype='text/plain') else: abort(500) except werkzeug_exceptions.HTTPException as ex: raise ex # keep raising except Exception as ex: app.logger.error(ex) abort(500)
def post(self, cid): oid = ObjectId(cid) comic = self.conn.Comic.one({'_id': oid}) usercomic = self.conn.UserComic() captions = [] # process captions for k in self.request.arguments: if k.startswith('caption_'): title = k.replace('caption_', '') newc = self.conn.UserCaption() newc.title = unicode(title) newc.text = self.get_argument(k) newc.save() captions.append(newc) usercomic.comic = comic usercomic.captions.extend(captions) usercomic.save() self.redirect(self.reverse_url('comic-user', unicode(usercomic._id)))
def encode_video(): ''' encoding video when user clicked encode from inside site input: none returns status of encoding process ''' if not request.json or not 'origin_id' in request.json or not 'user' in request.json: return abort(400) origin_id = request.json.get('origin_id') valid = ObjectId.is_valid(origin_id) if valid == False: return jsonify({'result':'False', 'info':'origin id is not valid, cannot encode video!', 'error': int(0)}) sub_url = request.json.get('sub_url') logo_url = request.json.get('logo_url') user = request.json.get('user') profiles_id = request.json.get('profiles_id') #if profiles_id not null, follow on this priority = request.json.get('priority') ''' for croping video ''' crop = request.json.get('crop') crop_arr = [] if crop: crop_arr = [int(c) for c in crop.split(',')] profile_array = [] is_new_profiles = int(0) if profiles_id: profile_array = [p.strip(' ') for p in profiles_id.split(',')] for profile_id in profile_array: valid = ObjectId.is_valid(profile_id) if valid == False: return jsonify({'result':'False', 'info':'profile id is not valid, cannot add origin video!', 'error': int(1)}) is_new_profiles = int(1) origin_video = mdb.OriginVideo.get_origin_video(origin_id) if origin_video: if len(crop_arr) != 4: crop_arr = origin_video['crop'] #prevent spam form bad user #if origin_video['status_encode'] == 1: #return jsonify({'result':'False', 'info': 'cannot encode video, because the same process encoding is working', 'error': int(2)}) if priority: origin_video['priority'] = int (priority) if origin_video['priority']: conn = BrokerConnection(current_app.config['CELERY_BROKER_URL_PRI'], heartbeat=int(10)) else: conn = BrokerConnection(current_app.config['CELERY_BROKER_URL'], heartbeat=int(10)) update_origin_video(origin_id, 5) encode_video_task.apply_async([origin_video['video_url'], sub_url, logo_url, profile_array if profiles_id else origin_video['profiles_id'], origin_video['priority'], user, origin_id, is_new_profiles, crop_arr], connection=conn) conn.release() if profiles_id: if not mdb.EncodingJob.get_jobs_with_origin_video_id(origin_id).count(): origin_video['profiles_id'] = profile_array else: profiles_id_old = origin_video['profiles_id'] for p in profile_array: if p not in profiles_id_old: profiles_id_old.append(p) origin_video['profiles_id'] = profiles_id_old origin_video['status_encode'] = int(5) #video have been pushed to queue origin_video['sub_url'] = sub_url if sub_url else '' origin_video['logo_url'] = logo_url if logo_url else '' origin_video['user'] = user origin_video['priority'] = int(priority) if priority else int(0) origin_video['crop'] = crop_arr origin_video.save() return jsonify({'result':'Done', 'info': 'encoding video successfull!'}) return jsonify({'result':'False', 'info': 'cannot encode video, origin id not found', 'error': int(4)})
def get_and_set_worker(workerId, deviceUDID): jobFound = False worker = db.Worker.get_or_404(workerId) workerRef = DBRef(collection=worker.__collection__, id=workerId, database=worker.__database__) # use this DBRef instead of the worker doc due to $set is unable to set documents # logger.debug('worker found: %s' % str(worker['_id'])) device = db.Device.find_one_or_404({'udid': deviceUDID}) deviceRef = DBRef(collection=device.__collection__, id=device['_id'], database=device.__database__) # use this DBRef instead of the device doc due to $set is unable to set documents # logger.debug('device found: %s' % str(device['_id'])) # check for jobs with worker and device set but unfinished job = db.Job.find_one({ "worker.$id": workerId, # IDEA: should the worker be ignored to allow the job to switch the worker? "device.$id": device["_id"], "state": { "$nin": [Job.STATE.FINISHED, Job.STATE.FAILED] } }) if job: jobFound = True logger.debug('found an unfinished job <%s> for device <%s>' % (str(job['_id']), str(device['_id']))) query = { "$or": [ { "worker": { "$type": 10 } }, # worker has to be NULL { "worker.$id": workerId } # or the current worker ], "$or": [ { "device": { "$type": 10 } }, # device has to be NULL { "device.$id": device["_id"] } # or the current device ], "state": { "$nin": [Job.STATE.FINISHED, Job.STATE.FAILED] }, "_id": { "$nin": [] } # job blacklist for jobs unable to run on the given device } while not jobFound: # gat a job and set the worker within an atomic operation to guarantee consistency job = db.Job.find_and_modify( query=query, update={'$set': { 'worker': workerRef, 'device': deviceRef }}, sort=[('date_added', -1)], new=True) if not job or not '_id' in job: return response( data={'message': 'Currently no free job available'}, code=204) if job.can_run_on_device(device): jobFound = True else: print "DEBUG: job is unable to run on device \njob: %s\ndevice: %s" % ( job, device) # reset the jobs worker entry db.Job.collection.update( {"_id": ObjectId(job['_id'])}, {"$set": { "worker": None, "device": None }}) # add the current job to the blacklist query['_id']['$nin'].append(ObjectId(job['_id'])) return response_doc(job)
def _get_media(self, mid): oid = ObjectId(mid) return self.conn.Media.one({'_id': oid})
def _get_movie(self, mid): oid = ObjectId(mid) return self.conn.Movie.one({'_id': oid})
def _get_series(self, sid): oid = ObjectId(sid) return self.conn.Series.one({'_id': oid})
def _get_episode(self, eid): oid = ObjectId(eid) return self.conn.Episode.one({'_id': oid})
def _get_user(self, uid): oid = ObjectId(uid) return self.conn.User.one({'_id': oid})
def undbref(ref): from db import connection from mongokit import ObjectId db = getattr(connection, ref.database) col = getattr(db, ref.collection) return col.find_one({'_id': ObjectId(ref.id)})