def detail_view(request): item_id = request.url.split('/')[-1] user = request.authenticated_userid root = firstpyramid.root_factory(request) item = root['images'].get(int(item_id)) if user in item['voters']: css_classes = get_css_classes(user, item['voters'][user]) else: css_classes = get_css_classes(user) return { 'item': { 'title': item['title'], 'image': b64(item['image']), 'id': item_id, 'uploader_name': root['users'][item['uploader']]['name'], 'uploader_id': item['uploader'], 'votes': votes_presage(item['votes']), 'desc': item['description'], 'tags': item['tags'], 'css_classes': css_classes } }
def add_user(self, new_user, request): root = firstpyramid.root_factory(request) root['users'].update({ new_user['username']: { 'password': hash_password(new_user['password']), 'name': new_user['name'] } }) transaction.commit()
def add_item(self, new_item, request): root = firstpyramid.root_factory(request) storage = root['images'] if storage: new_key = sorted(storage.keys())[-1] + 1 else: new_key = 1 new_item['image'] = Blob(new_item['image'].file.read()) self.update_item(new_key, new_item, request) return new_key
def get_users(self, request): root = firstpyramid.root_factory(request) return dict(root['users'])
def update_item(self, key, value, request): root = firstpyramid.root_factory(request) root['images'].update({key: value}) transaction.commit()
def get_items(self, request): root = firstpyramid.root_factory(request) return dict(root['images'])
def get_item(self, item_id, request): root = firstpyramid.root_factory(request) return root['images'][item_id]
def full_image_view(request): root = firstpyramid.root_factory(request) item_id = request.url.split('/')[-1] item = root['images'].get(int(item_id)) return {'image': b64(item['image'])}