def home(): """Generates the home page view template. Returns: Template with context. """ form = HiddenNameDescriptionForm() sketches = Sketch.all_with_acl().filter( not_(Sketch.Status.status == 'deleted'), Sketch.Status.parent).order_by(Sketch.updated_at.desc()) # Only render upload button if it is configured. upload_enabled = current_app.config['UPLOAD_ENABLED'] # Handle form for creating a new sketch. if form.validate_on_submit(): sketch = Sketch( name=form.name.data, description=form.description.data, user=current_user) sketch.status.append(sketch.Status(user=None, status='new')) db_session.add(sketch) db_session.commit() # Give the requesting user permissions on the new sketch. sketch.grant_permission(permission='read', user=current_user) sketch.grant_permission(permission='write', user=current_user) sketch.grant_permission(permission='delete', user=current_user) return redirect(url_for('sketch_views.overview', sketch_id=sketch.id)) return render_template( 'home/home.html', sketches=sketches, form=form, upload_enabled=upload_enabled)
def home(): """Generates the home page view template. Returns: Template with context. """ form = HiddenNameDescriptionForm() sketches = Sketch.all_with_acl().filter( not_(Sketch.Status.status == u'deleted'), Sketch.Status.parent).order_by(Sketch.updated_at.desc()) query_filter = request.args.get(u'filter', u'') query = request.args.get(u'q', u'') # Only render upload button if it is configured. upload_enabled = current_app.config[u'UPLOAD_ENABLED'] last_sketch = View.query.filter_by( user=current_user, name=u'').order_by( View.updated_at.desc()).first() if query_filter: if query_filter == u'user': sketches = sketches.filter(Sketch.user == current_user) elif query_filter == u'shared': sketches = sketches.filter(not_(Sketch.user == current_user)) # TODO: Figure out a better way to handle this. if query: if query.startswith(u'*'): query = u'' else: sketches = sketches.filter(Sketch.name.contains(query)).limit(100) # Handle form for creating a new sketch. if form.validate_on_submit(): sketch = Sketch( name=form.name.data, description=form.description.data, user=current_user) sketch.status.append(sketch.Status(user=None, status=u'new')) # Give the requesting user permissions on the new sketch. sketch.grant_permission(current_user, u'read') sketch.grant_permission(current_user, u'write') sketch.grant_permission(current_user, u'delete') db_session.add(sketch) db_session.commit() return redirect(url_for(u'sketch_views.overview', sketch_id=sketch.id)) return render_template( u'home/home.html', sketches=sketches, form=form, query=query, upload_enabled=upload_enabled, last_sketch=last_sketch)
def get(self): """Handles GET request to the resource. Returns: List of sketches (instance of flask.wrappers.Response) """ # TODO: Handle offset parameter sketches = Sketch.all_with_acl() paginated_result = sketches.paginate(1, 10, False) meta = { u'next': paginated_result.next_num, u'previous': paginated_result.prev_num, u'offset': paginated_result.page, u'limit': paginated_result.per_page } if not paginated_result.has_prev: meta[u'previous'] = None if not paginated_result.has_next: meta[u'next'] = None result = self.to_json(paginated_result.items, meta=meta) return result
def get(self): """Handles GET request to the resource. Returns: List of sketches (instance of flask.wrappers.Response) """ filtered_sketches = Sketch.all_with_acl().filter( not_(Sketch.Status.status == 'deleted'), Sketch.Status.parent).order_by(Sketch.updated_at.desc()).all() # Just return a subset of the sketch objects to reduce the amount of # data returned. sketches = [] for sketch in filtered_sketches: sketches.append({ 'name': sketch.name, 'updated_at': str(sketch.updated_at), 'user': sketch.user.username, 'id': sketch.id }) meta = {'current_user': current_user.username} return jsonify({'objects': sketches, 'meta': meta})
def get(self): """Handles GET request to the resource. Returns: List of sketches (instance of flask.wrappers.Response) """ args = self.parser.parse_args() scope = args.get('scope') page = args.get('page') per_page = args.get('per_page') search_query = args.get('search_query') include_archived = args.get('include_archived') if current_user.admin and scope == 'admin': sketch_query = Sketch.query else: sketch_query = Sketch.all_with_acl() base_filter = sketch_query.filter( not_(Sketch.Status.status == 'deleted'), not_(Sketch.Status.status == 'archived'), Sketch.Status.parent).order_by(Sketch.updated_at.desc()) base_filter_with_archived = sketch_query.filter( not_(Sketch.Status.status == 'deleted'), Sketch.Status.parent).order_by(Sketch.updated_at.desc()) filtered_sketches = base_filter_with_archived sketches = [] return_sketches = [] has_next = False has_prev = False next_page = None prev_page = None current_page = 1 total_pages = 0 total_items = 0 if scope == 'recent': # Get list of sketches that the user has actively searched in. # TODO: Make this cover more actions such as story updates etc. # TODO: Right now we only return the top 3, make this configurable. views = View.query.filter_by( user=current_user, name='').order_by( View.updated_at.desc()).limit(3) sketches = [view.sketch for view in views] total_items = len(sketches) elif scope == 'admin': if not current_user.admin: abort(HTTP_STATUS_CODE_FORBIDDEN, 'User is not an admin.') if include_archived: filtered_sketches = base_filter_with_archived else: filtered_sketches = base_filter elif scope == 'user': filtered_sketches = base_filter.filter_by(user=current_user) elif scope == 'archived': filtered_sketches = sketch_query.filter( Sketch.status.any(status='archived')) elif scope == 'shared': filtered_sketches = base_filter.filter(Sketch.user != current_user) elif scope == 'search': filtered_sketches = base_filter_with_archived.filter( or_( Sketch.name.ilike(f'%{search_query}%'), Sketch.description.ilike(f'%{search_query}%') ) ) if not sketches: pagination = filtered_sketches.paginate( page=page, per_page=per_page) sketches = pagination.items has_next = pagination.has_next has_prev = pagination.has_prev next_page = pagination.next_num prev_page = pagination.prev_num current_page = pagination.page total_pages = pagination.pages total_items = pagination.total for sketch in sketches: # Return a subset of the sketch objects to reduce the amount of # data sent to the client. return_sketches.append({ 'id': sketch.id, 'name': sketch.name, 'description': sketch.description, 'created_at': str(sketch.created_at), 'last_activity': utils.get_sketch_last_activity(sketch), 'user': sketch.user.username, 'status': sketch.get_status.status }) meta = { 'current_user': current_user.username, 'has_next': has_next, 'has_prev': has_prev, 'next_page': next_page, 'prev_page': prev_page, 'current_page': current_page, 'total_pages': total_pages, 'total_items': total_items } return jsonify({'objects': return_sketches, 'meta': meta})
def get(self): """Handles GET request to the resource. Returns: List of sketches (instance of flask.wrappers.Response) """ args = self.parser.parse_args() scope = args.get('scope', None) page = args.get('page', 1) search_query = args.get('search_query', None) if current_user.admin: sketch_query = Sketch.query else: sketch_query = Sketch.all_with_acl() base_filter = sketch_query.filter( not_(Sketch.Status.status == 'deleted'), not_(Sketch.Status.status == 'archived'), Sketch.Status.parent).order_by(Sketch.updated_at.desc()) base_filter_with_archived = sketch_query.filter( not_(Sketch.Status.status == 'deleted'), Sketch.Status.parent).order_by(Sketch.updated_at.desc()) filtered_sketches = base_filter_with_archived sketches = [] return_sketches = [] num_hits = 0 if scope == 'recent': # Get list of sketches that the user has actively searched in. # TODO: Make this cover more actions such as story updates etc. # TODO: Right now we only return the top 3, make this configurable. views = View.query.filter_by(user=current_user, name='').order_by( View.updated_at.desc()).limit(3) sketches = [view.sketch for view in views] num_hits = len(sketches) elif scope == 'user': filtered_sketches = base_filter.filter_by(user=current_user) elif scope == 'archived': filtered_sketches = sketch_query.filter( Sketch.status.any(status='archived')) elif scope == 'shared': filtered_sketches = base_filter.filter(Sketch.user != current_user) elif scope == 'search': filtered_sketches = base_filter_with_archived.filter( or_(Sketch.name.ilike(f'%{search_query}%'), Sketch.description.ilike(f'%{search_query}%'))) # If no scope is set, fall back to returning all sketches. if not scope: sketches = filtered_sketches.all() if not sketches: pagination = filtered_sketches.paginate(page=page, per_page=20) sketches = pagination.items num_hits = pagination.total for sketch in sketches: # Last time a user did a query in the sketch, indicating activity. try: last_activity = View.query.filter_by( sketch=sketch, name='').order_by( View.updated_at.desc()).first().updated_at except AttributeError: last_activity = '' # Return a subset of the sketch objects to reduce the amount of # data sent to the client. return_sketches.append({ 'id': sketch.id, 'name': sketch.name, 'description': sketch.description, 'created_at': str(sketch.created_at), 'last_activity': str(last_activity), 'user': sketch.user.username, 'status': sketch.get_status.status }) meta = {'current_user': current_user.username, 'num_hits': num_hits} return jsonify({'objects': return_sketches, 'meta': meta})
def get(self): """Handles GET request to the resource. Returns: List of sketches (instance of flask.wrappers.Response) """ args = self.parser.parse_args() scope = args.get("scope") page = args.get("page") per_page = args.get("per_page") search_query = args.get("search_query") include_archived = args.get("include_archived") if current_user.admin and scope == "admin": sketch_query = Sketch.query else: sketch_query = Sketch.all_with_acl() base_filter = sketch_query.filter( not_(Sketch.Status.status == "deleted"), not_(Sketch.Status.status == "archived"), Sketch.Status.parent, ).order_by(Sketch.updated_at.desc()) base_filter_with_archived = sketch_query.filter( not_(Sketch.Status.status == "deleted"), Sketch.Status.parent).order_by(Sketch.updated_at.desc()) filtered_sketches = base_filter_with_archived sketches = [] return_sketches = [] has_next = False has_prev = False next_page = None prev_page = None current_page = 1 total_pages = 0 total_items = 0 if scope == "recent": # Get list of sketches that the user has actively searched in. # TODO: Make this cover more actions such as story updates etc. # TODO: Right now we only return the top 3, make this configurable. views = (View.query.filter_by(user=current_user, name="").order_by( View.updated_at.desc()).limit(3)) sketches = [view.sketch for view in views] total_items = len(sketches) elif scope == "admin": if not current_user.admin: abort(HTTP_STATUS_CODE_FORBIDDEN, "User is not an admin.") if include_archived: filtered_sketches = base_filter_with_archived else: filtered_sketches = base_filter elif scope == "user": filtered_sketches = base_filter.filter_by(user=current_user) elif scope == "archived": filtered_sketches = sketch_query.filter( Sketch.status.any(status="archived")) elif scope == "shared": filtered_sketches = base_filter.filter(Sketch.user != current_user) elif scope == "search": filtered_sketches = base_filter_with_archived.filter( or_( Sketch.name.ilike(f"%{search_query}%"), Sketch.description.ilike(f"%{search_query}%"), )) if not sketches: pagination = filtered_sketches.paginate(page=page, per_page=per_page) sketches = pagination.items has_next = pagination.has_next has_prev = pagination.has_prev next_page = pagination.next_num prev_page = pagination.prev_num current_page = pagination.page total_pages = pagination.pages total_items = pagination.total for sketch in sketches: # Return a subset of the sketch objects to reduce the amount of # data sent to the client. return_sketches.append({ "id": sketch.id, "name": sketch.name, "description": sketch.description, "created_at": str(sketch.created_at), "last_activity": utils.get_sketch_last_activity(sketch), "user": sketch.user.username, "status": sketch.get_status.status, }) meta = { "current_user": current_user.username, "has_next": has_next, "has_prev": has_prev, "next_page": next_page, "prev_page": prev_page, "current_page": current_page, "total_pages": total_pages, "total_items": total_items, } return jsonify({"objects": return_sketches, "meta": meta})