def get_activities_for_user(request, tp=None): activities = get_item_list(request, Activity, user=request.user) if tp: start_date = tp.start_date end_date = tp.end_date filter_stack = [(">= {}".format(start_date), "date", False), ("<= {}".format(end_date), "date", False)] activities.filter(filter_stack) activities.sort(field="date", order="asc") return activities.items
def rest_list(request): """Returns a JSON objcet with all item of a clazz. The list does not have any capabilities for sorting or filtering :request: Current request. :returns: JSON object. """ clazz = request.context.__model__ listing = get_item_list(request, clazz, user=request.user) return JSONResponse(True, listing)
def itemlist(self): clazz = self.get_class() if self.showall == 'true': itemlist = get_item_list(self._field._form._request, clazz) else: itemlist = get_item_list(self._field._form._request, clazz, user=self._field._form._request.user) config = get_table_config(itemlist.clazz, self._field._config.renderer.table) sort_field = config.get_default_sort_column() sort_order = config.get_default_sort_order() itemlist.sort(sort_field, sort_order) # Warning filtering items here can cause loosing relations to # the filtered items. This is esspecially true if the item which # was related to the item before now gets filtered because of a # changes value in an attribute e.g. In this case the filtered # item is not in the list at all and will not be sent on a POST # request. This will result in removing the relation! search = config.get_default_search() itemlist.filter(search) return itemlist
def update_strava(request): """Will upload all training entries which are not yet uploaded to strava to strava.""" client = Client(access_token=get_access_token(request)) activities = get_item_list(request, Activity) for activity in activities: # If the strava_id is None than the activity as not uploaded # before. if activity.strava_id is None: log.debug("Upload of traing {id} to strava".format(id=activity.id)) strava = client.create_activity(activity.title, get_strava_activity_type(activity), activity.date, activity.duration.seconds, activity.description, activity.distance) activity.strava_id = strava.id
def itemlist(self): clazz = self.get_class() itemlist = get_item_list(self._field._form._request, clazz) config = get_table_config(itemlist.clazz, self._field._config.renderer.table) sort_field = config.get_default_sort_column() sort_order = config.get_default_sort_order() itemlist.sort(sort_field, sort_order) # Warning filtering items here can cause loosing relations to # the filtered items. This is esspecially true if the item which # was related to the item before now gets filtered because of a # changes value in an attribute e.g. In this case the filtered # item is not in the list at all and will not be sent on a POST # request. This will result in removing the relation! search = config.get_default_search() itemlist.filter(search) return itemlist
def test_get_item_list(apprequest): from ringo.model.modul import ModulItem from ringo.model.base import BaseList, get_item_list result = get_item_list(apprequest, ModulItem, user=None) assert isinstance(result, BaseList)
def preload_modules(event): """Preload all modules on each request and put them into the request cache for the modules to save additional SQL queries as the modules are needed anyway.""" for modul in get_item_list(event.request, ModulItem): event.request.cache_item_modul.set(modul.id, modul)
def index_view(request): handle_history(request) values = {} sprints = get_item_list(request, Sprint, request.user) values['sprints'] = [item for item in sprints.items if item.sprint_state_id == 2] return values
def get_trainingplans_for_user(request): """TODO: Docstring for get_trainingplans_for_user. :returns: TODO """ plans = get_item_list(request, Trainingplan, user=request.user) return plans.items
def get_base_list(clazz, request, user, table): """Helper function in views to get a BaseList instance for the given clazz. In contrast to the known "get_item_list" function which function will also handle searching, sorting and pagination based on the parameter privided within the current request. This function makes the search, pagination and sorting feature (as used in the advanced search) available in other external views. :clazz: Class of item which will be loaded. :request: Current request. :user: Current user. If None, than all items of a class will be loaded. :table: Name of the table configuration which is used for the listing. :returns: BaseList instance """ # If the user enters the overview page of an item we assume that the # user actually leaves any context where a former set backurl is # relevant anymore. So delete it. backurl = request.session.get('%s.backurl' % clazz) if backurl: # Redirect to the configured backurl. del request.session['%s.backurl' % clazz] request.session.save() search = get_search(clazz, request) sorting = handle_sorting(clazz, request) pagination_page, pagination_size = handle_paginating(clazz, request) list_params = {} list_params["search"] = search list_params["sorting"] = sorting list_params["pagination"] = (pagination_page, pagination_size) # Try to do an optimized loading of items. If the loading succeeds # the loaded items will be used to build an item list. If for some # reasone the loading was'nt successfull items will be None and # loading etc will be done completely in application. if request.ringo.feature.dev_optimized_list_load: items, total = load_items(request, clazz, list_params) listing = get_item_list(request, clazz, user=user, items=items) # Ok no items are loaded. We will need to do sorting and filtering # on out own. if items is None: listing.sort(sorting[0], sorting[1]) listing.filter(search, request, table) else: listing = get_item_list(request, clazz, user=user) listing.sort(sorting[0], sorting[1]) listing.filter(search, request, table) total = len(listing.items) listing.paginate(total, pagination_page, pagination_size) # Only save the search if there are items if len(listing.items) > 0: request.session['%s.list.search' % clazz.__tablename__] = search if (request.params.get('form') == "search"): if "save" in request.params: query_name = request.params.get('save') user = BaseFactory(User).load(request.user.id) searches_dic = user.settings.get('searches', {}) searches_dic_search = searches_dic.get(clazz.__tablename__, {}) # Check if there is already a search saved with the name found = False for xxx in searches_dic_search.values(): if xxx[1] == query_name: found = True break if not found: searches_dic_search[str(uuid.uuid1())] = (search, sorting, query_name) searches_dic[clazz.__tablename__] = searches_dic_search user.settings.set('searches', searches_dic) request.db.flush() elif "delete" in request.params: query_key = request.params.get('delete') user = BaseFactory(User).load(request.user.id) searches_dic = user.settings.get('searches', {}) searches_dic_search = searches_dic.get(clazz.__tablename__, {}) try: del searches_dic_search[query_key] except: pass searches_dic[clazz.__tablename__] = searches_dic_search user.settings.set('searches', searches_dic) request.db.flush() request.session.save() return listing
def list_(request): clazz = request.context.__model__ # Important! Prevent any write access on the database for this # request. This is needed as transform would modify the items values # else. handle_history(request) # If the user enters the overview page of an item we assume that the # user actually leaves any context where a former set backurl is # relevant anymore. So delete it. backurl = request.session.get('%s.backurl' % clazz) if backurl: # Redirect to the configured backurl. del request.session['%s.backurl' % clazz] request.session.save() rvalue = {} search = get_search(clazz, request) sorting = handle_sorting(clazz, request) pagination_page, pagination_size = handle_paginating(clazz, request) listing = get_item_list(request, clazz, user=request.user) listing.sort(sorting[0], sorting[1]) listing.filter(search) listing.paginate(pagination_page, pagination_size) # Only save the search if there are items if len(listing.items) > 0: request.session['%s.list.search' % clazz.__tablename__] = search if (request.params.get('form') == "search"): if "save" in request.params: query_name = request.params.get('save') user = BaseFactory(User).load(request.user.id) searches_dic = user.settings.get('searches', {}) searches_dic_search = searches_dic.get(clazz.__tablename__, {}) # Check if there is already a search saved with the name found = False for xxx in searches_dic_search.values(): if xxx[1] == query_name: found = True break if not found: searches_dic_search[str(uuid.uuid1())] = (search, sorting, query_name) searches_dic[clazz.__tablename__] = searches_dic_search user.settings.set('searches', searches_dic) request.db.flush() elif "delete" in request.params: query_key = request.params.get('delete') user = BaseFactory(User).load(request.user.id) searches_dic = user.settings.get('searches', {}) searches_dic_search = searches_dic.get(clazz.__tablename__, {}) try: del searches_dic_search[query_key] except: pass searches_dic[clazz.__tablename__] = searches_dic_search user.settings.set('searches', searches_dic) request.db.flush() request.session.save() renderer = get_list_renderer(listing, request) rendered_page = renderer.render(request) rvalue['clazz'] = clazz rvalue['listing'] = rendered_page rvalue['itemlist'] = listing return rvalue