def updateImage(): object_id = request.form['object_id'] image_id = request.form['image_id'] if object_id is None: abort(404) if is_valid_id(image_id) and is_valid_id(object_id): Blibb.add_picture({'_id': ObjectId(object_id)}, image_id) return 'ok'
def newItem(): bid = request.form['blibb_id'] key = request.form['login_key'] tags = request.form['tags'] app_token = request.form['app_token'] user = get_user_name(key) current_app.logger.info('labels: ' + str(user)) if is_valid_id(bid): b = Blibb.get_object({'_id': ObjectId(bid)}, {'u': 1, 't.i': 1}) controls = Blibb.get_controls_as_dict(b.get('t')) current_app.logger.info(controls) if Blibb.can_write(user, app_token, bid): # labels = Blibb.get_labels(b.get('t')) # current_app.logger.info('labels: ' + str(labels)) bitems = Blitem.get_items_from_request(controls, request) current_app.logger.info('items from request: ' + str(bitems)) blitem_id = Blitem.insert(bid, user, bitems, tags) if blitem_id: cond = {'_id': ObjectId(bid)} Blibb.inc_num_item(cond) Blitem.post_process(blitem_id, bitems) return blitem_id else: abort(401) return jsonify(Message.get('id_not_valid'))
def get_template_view(self, obj_id): if is_valid_id(obj_id): fields = {'t.v': 1, 'n': 1, 'd': 1, 'c': 1, 'u': 1, 'tg': 1, 's': 1, 'img': 1, 'ni': 1, 'st': 1} res = self.get_object({'_id': ObjectId(obj_id)}, fields) if '_id' in res: return self.flat_object(res)
def newBlibb(): name = request.form['bname'] desc = request.form['bdesc'] template = request.form['btemplate'] key = request.form['bkey'] user = get_user_name(key) image_id = request.form['bimage'] slug = request.form['slug'] write_access = request.form['write_access'] read_access = request.form['read_access'] # check if a blibb with that slug already exists blibb = Blibb.get_by_slug(user, slug) # return jsonify(blibb) if not blibb: res = {'error': 'None'} if is_valid_id(image_id): image = Picture.dump_image(image_id) else: image = 'blibb.png' new_id = Blibb.insert(user, name, slug, desc, template, image, read_access, write_access) res = {'id': new_id} else: res = {'error': 'Blibb with that slug already exists'} return jsonify(res)
def getWebhooks(bid=None): if is_valid_id(bid): b = Blibb() fields = b.get_webhooks(bid) return jsonify({'webhooks': fields}) else: return jsonify({'error': 'Object id not valid'})
def can_write(self, user, app_token, blibb_id): """ This method checks if a write operation can be performed. The method fallsback to the lower writing preference. First app_token, if it's valid, the method returns True. Second, the user is the owner. Third, the user belongs to a group with writing permission Last, it checks if WORLD has writing capabilities """ if is_valid_id(blibb_id): blibb = self.get_object({'_id': ObjectId(blibb_id)}, { 'a': 1, 'u': 1, 'g': 1, 'at': 1, 'n': 1, 'd': 1}) current_app.logger.info(str(blibb)) # atoken = blibb.get('at', 0) atoken = hashlib.sha1(blibb['n'] + blibb['d']).hexdigest() current_app.logger.debug(str(atoken)) owner = blibb['u'] acl = blibb.get('a', {}) if atoken == app_token: return self.is_valid_token(atoken) if user == owner: return True if acl.get('write', 0) == 5: group = blibb['g'] if user in group: return True if acl.get('write', 0) == 11: return True # logging.warning('say') return False
def setImageUser(): user_id = request.form['user_id'] image_url = request.form['image_url'] if is_valid_id(user_id): User.add_picture({'_id': ObjectId(user_id)}, image_url) return jsonify({'response': 'ok'}) return abort(404)
def can_write(cls, user, blitem_id): if is_valid_id(blitem_id): doc = cls.get({'_id': ObjectId(blitem_id)}) blibb_id = str(doc['b']) current_app.logger.info('can_write' + user + ' ' + blibb_id) return Blibb.can_write(user, '', blibb_id) return False
def add_controls(self, template, controls, user): items = [] # current_app.logger.info("add_controls " + str(controls)) controls = json.loads(controls) for control in controls: item = {} cid = control.get('cid', '') if is_valid_id(cid): item['c'] = ObjectId(cid) item['n'] = control['name'] item['h'] = control['help'] item['tx'] = control['type'] item['o'] = int(control['order']) item['s'] = slugify(control['name']) item['m'] = True if control['multi'] == 'true' else False if 'items' in control: item['i'] = control.get('items') items.append(item) # else: # current_app.logger.info('Control ID' + cid) # current_app.logger.info(items) now = datetime.utcnow() doc = { "n": template, "u": user, "c": now, "s": slugify(template), 'q': 'draft', 'i': items} newId = objects.insert(doc) return str(newId)
def getBlitemFields(blibb_id=None): if is_valid_id(blibb_id): b = Blibb.get_object( {'_id': ObjectId(blibb_id)}, {'u': 1, 't.i.n': 1, 't.i.s': 1}) res = Blibb.getLabels(b.get('t')) else: res = Message.get('id_not_valid') return jsonify(res)
def get_comments_by_id(cls, obj_id): if is_valid_id(obj_id): docs = objects.find({'p': ObjectId(obj_id)}).sort("c", -1) ddocs = [] for d in docs: ddocs.append(cls.flatObject(d)) return ddocs else: return {'error': 'Object Id is not valid'}
def get_fields(self, obj_id): if is_valid_id(obj_id): doc = self.get_object({'_id': ObjectId(obj_id)}, {'t.i': 1}) template = doc.get('t').get('i') fields = [] for elem in template: fields.append(str(elem.get('tx')) + '-' + elem.get('s')) return fields
def getBlibbView(blibb_id=None, view_name='null'): if is_valid_id(blibb_id): r = Blibb.get_template_view(blibb_id) if r != 'null': return jsonify(r) else: abort(404) else: abort(400)
def get_picture_data(picture_id=None): e = Event('getImage.get_picture_data') r = None if is_valid_id(picture_id): r = Picture.dump_image(picture_id) e.save() if r is not None: return jsonify(r) else: abort(404)
def get_label_from_template(self, obj_id): if is_valid_id(obj_id): result = self.get_object({'_id': ObjectId(obj_id)}, {'t.i.n': 1, 't.i.s': 1}) if result is not None: return self.get_labels(result['t']) else: return {'count': 0} else: return {'error': 'Object id not valid'}
def get_by_id_params(self, obj_id, params): p = dict() if is_valid_id(obj_id): listparams = params.split(",") for param in listparams: p[param] = 1 doc = self.get_object({'_id': ObjectId(obj_id)}, p) return self.flat_object(doc) return Message.get('id_not_valid')
def add_webhook(self, object_id, webhook): if is_valid_id(object_id): self.objects.update( {'_id': ObjectId(object_id)}, {'$pull': {'wh': {'a': webhook['a']}}} ) self.objects.update( {'_id': ObjectId(object_id)}, {'$addToSet': {'wh': webhook}}) else: return Message.get('id_not_valid')
def deleteBlitem(blitem_id=None, login_key=None): user = get_user_name(login_key) if user: if is_valid_id(blitem_id): if Blitem.can_write(user, blitem_id): current_app.logger.error(blitem_id) Blitem.remove({'_id': ObjectId(blitem_id)}) return jsonify({'response': 'deleted'}) abort(404) abort(401)
def vote(cls, item_id, user, vote, att): if is_valid_id(item_id): current_app.logger.info(item_id + ' ' + user + ' ' + str(vote) + ' ' + att) elem = db['votes'].find_one({'i': ObjectId(item_id), 'u': user}) if elem is None: objects.update({"_id": ObjectId(item_id)}, {"$inc": {att: vote}}) db['votes'].insert({'u': user, 'i': ObjectId(item_id), 'v': vote}) return {'vote': 'ok'} else: return {'error': 'User has already voted'} return {'error': 'Object not valid'}
def update(self, item_id, blibb_id, user, items, tags=None): tag_list = [] if is_valid_id(blibb_id) and is_valid_id(item_id): # bid = ObjectId(blibb_id) # b = Blibb.get_object({'_id': bid}, {'s': 1}) # bs = b['s'] if tags is not None: if ',' in tags: tag_list = list(set(tags.lower().split(','))) else: tag_list = list(set(tags.lower().split())) for t in tag_list: Blibb.add_tag(blibb_id, t) # now = datetime.utcnow() doc = {"_id": item_id, "b": blibb_id, "i": items} objects.update({"_id": ObjectId(item_id)}, {'$set': {"i": items}}) post_process.send(doc) return item_id else: return Message.get('id_not_valid')
def updateView(): blibb_id = request.form['blibb_id'] user = get_user_name(request.form['login_key']) view = request.form['viewName'] html = request.form['viewHtml'] # current_app.logger.info(user + ' ' + blibb_id + ' ' + view + ' ' + html) if is_valid_id(blibb_id): if Blibb.can_write(user, '', blibb_id): Blibb.update_view(blibb_id, user, view, html) return jsonify({'result': 'View Updated'}) else: abort(401) abort(404)
def get_item_by_id(username=None, slug=None, id=None): if username is None or slug is None or id is None: abort(404) blibb = Blibb.get_object({'u': username, 's': slug}) if blibb and is_valid_id(id): blibb_id = blibb['_id'] items = Blitem.get_item({'_id': ObjectId(id), 'b': ObjectId(blibb_id)}, {'i': 1, 'tg': 1, 'b': 1, 'c': 1} ) attributes = {'tags': True, 'comments': True} return jsonify(Blitem.flat_object(items, attributes)) else: return jsonify(Message.get('id_not_valid'))
def get_webhooks(self, obj_id): if is_valid_id(obj_id): doc = self.get_object({u'_id': ObjectId(obj_id)}, {'wh': 1}) whs = doc.get('wh', []) # return template webhooks = [] for wh in whs: w = { 'action': wh.get('a'), 'callback': wh.get('u'), 'fields': wh.get('f') } webhooks.append(w) return webhooks
def get_items_by_tag(self, blibb_id, tag): if is_valid_id(blibb_id): docs = self.get_items_page({'b': ObjectId(blibb_id), 'tg': tag}, {'i': 1, 'tg': 1, 'b': 1}) result = dict() blitems = [] attributes = {'tags': True} for d in docs: blitems.append(self.flat_object(d, attributes)) result['count'] = len(blitems) result['items'] = blitems return result return Message.get('id_not_valid')
def create(cls, owner, name, contacts, tags, comments, group=True, public=False): tag_list = [] oi = dict() oi['owner'] = owner now = datetime.now() oi['created_at'] = now oi['name'] = name rnd_id = str(sha1(name + owner + str(now)).hexdigest()) contacts_list = [] if contacts is not None: if ',' in contacts: contacts_list = list(set(contacts.strip().lower().split(','))) else: contacts_list.append(contacts) oi['invited'] = contacts_list oi['push'] = {'when': '', 'who': ''} oi['sent'] = 0 oi['pushes'] = 0 oi['group'] = group oi['public'] = public subscribers = [] if group: subscribers.append(owner) oi['channel'] = '%s-%s-%s' % (cls.parse_string(owner), cls.parse_string(name), rnd_id) oi['senders'] = [owner] oi['subscribers'] = subscribers contacts_list = oi["invited"] for p in contacts_list: cls.add_invited(p, oi) oi['comments'] = comments if tags is not None: if ',' in tags: tag_list = list(set(tags.lower().split(','))) else: tag_list = list(set(tags.lower().split())) oi['tags'] = tag_list new_id = cls.objects.insert(oi) current_app.logger.info(str(oi)) if is_valid_id(new_id): oi['_id'] = new_id full_name = cls.get_full_name(oi['owner']) for email in oi['invited']: queue_mail(str(new_id), full_name, name, email, comments, '/templates/mail.html') return oi return {'error': 'Error creating the Oi'}
def getImage(pict_id=None, size=160): e = Event('web.content.getImage') r = None if is_valid_id(pict_id): try: img = Picture.dump_image(pict_id) g = file(Picture.get_image_by_size(img, size)) return Response(g, direct_passthrough=True) except IOError: abort(404) e.save() if r != 'null': return json.dumps(r) else: abort(404)
def bulk_insert(self, blibb_id, user, items, tags=None): if is_valid_id(blibb_id): bid = ObjectId(blibb_id) b = Blibb.get_object({'_id': bid}, {'s': 1, 'u': 1, 't.i.n': 1, 't.i.s': 1}) blibb_slug = b.get('s') labels = Blibb.get_labels(b.get('t')) count = 0 for item in items: now = datetime.utcnow() i = self.get_blitem_from_dict(item, labels) doc = {"b": bid, "u": user, "bs": blibb_slug, "c": now, "i": i} objects.insert(doc) count = count + 1 return str(count) + 'elements added' else: return Message.get('id_not_valid')
def get_all_items(self, blibb_id, page, attributes={ 'tags': True, 'comments': True}, flat=True): if is_valid_id(blibb_id): docs = self.get_items_page({'b': ObjectId(blibb_id)}, {'i': 1, 'tg': 1, 'b': 1, 'c': 1}, page) result = dict() blitems = [] for d in docs: if flat: blitems.append(self.flat_object(d, attributes)) else: blitems.append(d) result['b_id'] = blibb_id result['count'] = len(blitems) result['items'] = blitems return result return Message.get('id_not_valid')
def add_webhook(): key = request.form['login_key'] bid = request.form['blibb_id'] callback = request.form['callback'] fields = request.form['fields'] action = request.form['action'] user = get_key(key) res = dict() wb = {'a': action, 'u': callback, 'f': fields} if is_valid_id(bid): if Blibb.can_write(user, '', bid): Blibb.add_webhook(bid, wb) res['result'] = 'ok' else: abort(401) else: res['error'] = 'Object Id is not valid' return jsonify(res)
def add_user_to_group(): key = request.form['login_key'] bid = request.form['blibb_id'] username = request.form['username'] user = get_key(key) res = dict() if is_valid_id(bid): user_to_add = User.get_by_name(username) if user_to_add: if Blibb.can_write(user, '', bid): Blibb.add_user_to_group(username, bid) res['result'] = 'ok' else: res['error'] = 'Not permissions' else: res['error'] = 'User not found' else: res['error'] = 'Object Id is not valid' return jsonify(res)