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 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 getBlibbTemplate(blibb_id=None): b = Blibb() r = b.get_template(blibb_id) if r != 'null': return r else: abort(404)
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 newComment(): e = Event('web.newComment') comment = Comment() c_id = None key = request.form['k'] user = utils.getKey(key) logging.error('Processing %s' % user) if user is not None: target_id = None parent = None pObject = None if 'b' in request.form: target_id = request.form['b'] parent = 'b' pObject = Blibb() logging.error('Processing %s' % target_id) elif 'i' in request.form: target_id = request.form['i'] parent = 'i' pObject = Blitem() logging.error('Processing %s' % target_id) else: abort(404) text = request.form['c'] c_id = comment.insert(target_id, user, text, parent) pObject.incCommentsCounter(target_id) e.save() else: d = dict() d['error'] = "user not found" c_id = d return json.dumps(c_id,default=json_util.default)
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 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 getItemsByBlibbAndView(blibb_id=None, view='Default'): # This method returns all the blitems # by blibb and rendered with the view # passed b = Blibb() r = b.getTemplateView(blibb_id, view) if r != 'null': return r else: abort(404)
def getBlibbShort(short_id=None): if short_id is None: abort(404) o = Blibb.get_object({'si': short_id}) r = Blibb.flat_object(o) if r != 'null': return jsonify(r) else: abort(404)
def getObject(bid=None): if bid is not None: params = request.args.get('fields') fields = dict() for p in params.split(','): fields[p] = 1 current_app.logger.info(fields) doc = Blibb.get_object({'_id': ObjectId(bid)}, fields) blibb = Blibb.to_dict(doc) # return jsonify(Blibb.flat_object(blibb)) abort(404)
def deleteBlibb(username=None, slug=None): if username is None or slug is None: abort(404) b = Blibb() dres = b.getBySlug(username, slug) results = dres.get('results', '') count = dres.get('count', 0) if count > 0: jblibb = results[0] bid = jblibb['id'] filter = {'_id': ObjectId(bid)} b.remove(filter)
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 insert(self, blibb, user, items, tags=None): tag_list = [] if tags is not None: tag_list = list(set(tags.split())) # ToDO: this has to go through a queue b = Blibb() for t in tag_list: b.addTag(blibb,t) now = datetime.utcnow() doc = {"b" : ObjectId(blibb), "u": user, "c": now, "i": items, "cc": 0, 'tg': tag_list} newId = self.objects.insert(doc) return str(newId)
def getBlibb(blibb_id=None, params=None): if blibb_id is None: abort(404) if params is None: o = Blibb.get_object(blibb_id) r = Blibb.flat_object(o) else: r = Blibb.get_by_id_params(blibb_id, params) if r != 'null': return jsonify(r) else: abort(404)
def get_items_by_tag(username=None, slug=None, tag=None): if username is None or slug is None or tag is None: abort(404) # ip = request.remote_addr blibb_id = Blibb.get_by_slug(username, slug) if blibb_id: cond = {'s': slug, 'u': username} Blibb.increase_view(cond, 'vt') # return blibb_id b = Blitem() items = b.get_items_by_tag(blibb_id['id'], tag) return jsonify(items) return jsonify({'results': '0'})
def get_by_slug(username=None, slug=None, url=None, attributes={}, flat=True): if username is None or slug is None: return None page = request.args.get('page', 1) # comments = request.args.get('comments', 0) blibb = Blibb.get_by_slug(username, slug) if url: blibb['url'] = url ret = dict() cond = {'s': slug, 'u': username} Blibb.increase_view(cond, 'v') ret['blibb'] = blibb rs_items = Blitem.get_all_items(blibb['id'], int(page), attributes, flat) ret['items'] = rs_items['items'] return ret
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 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 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 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)
def newTag(): target_id = None target = None key = request.form['k'] user = get_user_name(key) target_id = request.form['b'] if Blibb.can_write(target_id, user): tag = request.form['t'] target.addTag(target_id, tag) return json.dumps('ok')
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 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 addItemtoBlibb(username=None, slug=None): if username is None or slug is None: abort(404) user = '' app_token = '' if 'login_key' in request.form: key = request.form['login_key'] user = get_key(key) else: app_token = request.form['app_token'] if \ 'app_token' in request.form else '' tags = request.form['tags'] if 'tags' in request.form else '' blibb = Blibb.get_object({'u': username, 's': slug}, {'_id': 1, 't.i': 1}) if blibb: blibb_id = blibb['_id'] controls = Blibb.get_controls_as_dict(blibb.get('t')) current_app.logger.info(str(user) + ' - ' + str(app_token) + ' - ' + str(blibb_id) + ' - ' + username + ' - ' + slug) if Blibb.can_write(user, app_token, blibb_id): bitems = Blitem.get_items_from_request(controls, request) if len(bitems) > 0: blitem_id = Blitem.insert(blibb_id, user, bitems, tags) if is_valid_id(blitem_id): cond = {'s': slug, 'u': username} Blibb.inc_num_item(cond) Blitem.post_process(blitem_id, bitems) res = {'id': blitem_id} return jsonify(res) else: return jsonify({'error': 'your POST data was not complete'}) else: abort(401) else: abort(404)
def insert(self, blibb_id, user, items, tags=None): tag_list = [] if is_valid_id(blibb_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) num = int(NUM_CHARS) url_id = "".join(sample(digits + ascii_letters, num)) now = datetime.utcnow() doc = {"b": bid, "u": user, "bs": bs, "c": now, "i": items, "tg": tag_list, "st": "active", "si": url_id} newId = objects.insert(doc) post_process.send(doc) return str(newId) else: return Message.get('id_not_valid')
def loader_excel(): event = Event('web.content.loader_excel') key = request.form['login_key'] bid = request.form['blibb_id'] user = get_user_name(key) res = dict() file = request.files['file'] if file and allowed_file(file.filename): try: filename = secure_filename(file.filename) excel_file = os.path.join( get_config_value('UPLOAD_FOLDER'), filename ) file.save(excel_file) if is_valid_id(bid): fields = Blibb.get_fields(bid) excel_data = loader.excel_to_dict(excel_file, fields) current_app.logger.debug(excel_data) if len(excel_data): a = Blitem.bulk_insert(bid, user, excel_data) current_app.logger.debug(a) res['result'] = 'ok' else: res['error'] = 'No data found in file' else: if bid == '-1': res['error'] = 'create new blibb from file' res['error'] = 'Object Id is not valid' # except Exception, e: # current_app.logger.error(e) # res['error'] = 'Error processing spreadsheet' finally: if os.path.isfile(filename): os.unlink(filename) else: res['error'] = 'File was not uploaded' event.save() return jsonify(res)
def getBlibbFields(bid=None): if bid is not None: fields = Blibb.get_fields(bid) return jsonify({'fields': fields})
def get(url_id=None): if url_id: blibb = Blibb.get_object({'si': url_id}) if blibb: return redirect('http://blibb.net/blibb?b=' + str(blibb['_id']), 301) abort(404)
def deleteBlibb(blibb_id=None, login_key=None): user = get_user_name(login_key) if is_valid_id(blibb_id): filter = {'_id': ObjectId(blibb_id), 'u': user} Blibb.remove(filter) return jsonify({'ret': 1})