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 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 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 get(self, cid): oid = ObjectId(cid) comic = self.conn.UserComic.one({'_id': oid}) self.render(USER_TPL, **{ 'comic': comic, })
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 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 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 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 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 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 _get_movie(self, mid): oid = ObjectId(mid) return self.conn.Movie.one({'_id': oid})
def find_one_by_oid_and_id(self, open_id, id): return self.find_one({"open_id": open_id, "_id": ObjectId(id)})
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 post(self, series_id): series = self._get_series(series_id) season = self.db.Season() seasonform = SeasonForm() seriesform = SeriesForm(TornadoMultiDict(self.request.arguments)) #episodes = self.db.Episode.find({'series':None}) episodesform = None updated = 'Thanks for posting' try: torn_post_args = TornadoMultiDict(self.request.arguments) if self.get_argument('seasonform', None): seasonform = SeasonForm(torn_post_args) self._validate_or_exception(seasonform) self._update_doc_from_form(season, seasonform) season.save() series.seasons.append(season) series.save() updated = 'Season has been added' elif self.get_argument('seriesform', None): self._validate_or_exception(seriesform) self._update_doc_from_form(series, seriesform) if seriesform.poster_url.data: poster_url = seriesform.poster_url.data poster_fn = self._get_new_poster_file(series.title) self._download_poster_image(poster_url, poster_fn) series.poster = poster_fn series.save() updated = 'Series has been updated' elif self.get_argument('seasonid', None): seasonid = self.get_argument('seasonid') # Adding Episodes to a season episodesform = EpisodesSeasonForm(torn_post_args) count = 0 episode_add_list = [] for eform in episodesform.media: if eform.selected.data: form = eform.form self._validate_or_exception(form) episode_add_list.append(form) count += 1 season = self.db.Season.one({'_id': ObjectId(seasonid)}) for eform in episode_add_list: eid = eform.episode_id.data e = self.db.Episode.one({'_id': ObjectId(eid)}) self._update_doc_from_form(e, eform) e.series = ObjectId(series_id) e.save() season.episodes.append(e) season.save() except Exception, e: if season.has_key('_id'): season.delete() self.render( self.tpl, **{ 'error': e, 'nav_active': 'admin', 'menu_active': 'series', 'series': series, 'episodes': episodes, 'episodesform': episodesform, 'seriesform': seriesform, 'seasonform': seasonform, })
def _get_media(self, mid): oid = ObjectId(mid) return self.conn.Media.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)})
def _get_user(self, uid): oid = ObjectId(uid) return self.conn.User.one({'_id': oid})
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)