def _wrapped_view(request, *args, **kwargs): filters = {} for kwarg, kwvalue in list(kwargs.items()): if kwarg in ignore: continue filters[kwarg] = kwvalue obj = get_object_or_404(klass, **filters) response = get_403_or_None( request, perms=[perm], obj=obj, return_403=True, ) if response: return response return view_func(request, *args, **kwargs)
def _wrapped_view(request, *args, **kwargs): # if more than one parameter is passed to the decorator we try to # fetch object for which check would be made obj = None if lookup_variables: model, lookups = lookup_variables[0], lookup_variables[1:] # Parse model if isinstance(model, str): splitted = model.split('.') if len(splitted) != 2: raise GuardianError( "If model should be looked up from " "string it needs format: 'app_label.ModelClass'") model = apps.get_model(*splitted) elif issubclass(model.__class__, (Model, ModelBase, QuerySet)): pass else: raise GuardianError( "First lookup argument must always be " "a model, string pointing at app/model or queryset. " "Given: %s (type: %s)" % (model, type(model))) # Parse lookups if len(lookups) % 2 != 0: raise GuardianError( "Lookup variables must be provided " "as pairs of lookup_string and view_arg") lookup_dict = {} for lookup, view_arg in zip(lookups[::2], lookups[1::2]): if view_arg not in kwargs: raise GuardianError("Argument %s was not passed " "into view function" % view_arg) lookup_dict[lookup] = kwargs[view_arg] obj = get_object_or_404(model, **lookup_dict) response = get_403_or_None(request, perms=[perm], obj=obj, login_url=login_url, redirect_field_name=redirect_field_name, return_403=return_403, accept_global_perms=accept_global_perms) if response: return response return view_func(request, *args, **kwargs)
def check_permissions(self, request): """ Checks if *request.user* has all permissions returned by *get_required_permissions* method. :param request: Original request. """ obj = hasattr(self, 'get_object') and self.get_object() or getattr(self, 'object') or None forbidden = get_403_or_None(request, perms=self.get_required_permissions(request), obj=obj, login_url=self.login_url, redirect_field_name=self.redirect_field_name, return_403=self.return_403, ) if forbidden: self.on_permission_check_fail(request, forbidden, obj=obj) if forbidden and self.raise_exception: raise PermissionDenied() return forbidden
def _wrapped_view(request, *args, **kwargs): # if more than one parameter is passed to the decorator we try to # fetch object for which check would be made obj = None if lookup_variables: model, lookups = lookup_variables[0], lookup_variables[1:] # Parse model if isinstance(model, basestring): splitted = model.split('.') if len(splitted) != 2: raise GuardianError("If model should be looked up from " "string it needs format: 'app_label.ModelClass'") model = apps.get_model(*splitted) elif issubclass(model.__class__, (Model, ModelBase, QuerySet)): pass else: raise GuardianError("First lookup argument must always be " "a model, string pointing at app/model or queryset. " "Given: %s (type: %s)" % (model, type(model))) # Parse lookups if len(lookups) % 2 != 0: raise GuardianError("Lookup variables must be provided " "as pairs of lookup_string and view_arg") lookup_dict = {} for lookup, view_arg in zip(lookups[::2], lookups[1::2]): if view_arg not in kwargs: raise GuardianError("Argument %s was not passed " "into view function" % view_arg) lookup_dict[lookup] = kwargs[view_arg] obj = get_object_or_404(model, **lookup_dict) response = get_403_or_None(request, perms=[perm], obj=obj, login_url=login_url, redirect_field_name=redirect_field_name, return_403=return_403, accept_global_perms=accept_global_perms) if response: return response return view_func(request, *args, **kwargs)
def _wrapped_view(request, *args, **kwargs): obj = get_object_or_404(klass, **kwargs) get_403_or_None(request, perms=[perm], obj=obj, return_403=True) return view_func(request, *args, **kwargs)
def get(self, request, *args, **kwargs): try: activityprofile = ActivityProfile.objects.get(url_title=kwargs["activity_url_title"]) except ActivityProfile.DoesNotExist: return redirect(reverse("url_home")) cansee = False try: organization_user = OrganizationUser.objects.get(organization=kwargs["organization_pk"], user=request.user) organization = Organization.objects.get(pk=kwargs["organization_pk"]) except OrganizationUser.DoesNotExist: return get_403_or_None(request, "administer_activity", return_403=True) if (request.user.is_superuser or request.user.profile.is_brand_supervisor() or request.user.has_perm("administer_activity", activityprofile) or organization_user.is_moderator): cansee = True if not cansee: return get_403_or_None(request, "administer_activity", return_403=True) users_in_group = [ unicode(user["user_id"]) for user in OrganizationUser.objects.filter(organization_id=kwargs["organization_pk"]).values("user_id")] xdata = ["0-25%", "25-50%", "50-75%", "75-100%"] tcprofile = kwargs["ap_repo"].GetSingleActivityProfile({ "profileId": "outline", "activityId": activityprofile.url }) stmts = 0 testees = [] test_bucket_hash = {} test_bucket_info = {} ctx = dict() if tcprofile: for x in tcprofile["objects"]: stmts += 1 for v in x.get("verbs"): if v.get("has_score"): testees.append(x["id"]) test_bucket_hash[x["id"]] = [0, 0, 0, 0] test_bucket_info[x["id"]] = { "name": x["name"] } res = kwargs["db"].activitystates.find({ "stateId": "progress-by-id", "activityId": activityprofile.url, "agent.account.name": {"$in": users_in_group} }) buckets = [0, 0, 0, 0] test_buckets = [0, 0, 0, 0] if stmts > 0: for x in res: if not x.get("state"): continue s = len(x["state"]) if float(s) / stmts < 0.25: buckets[0] += 1 elif float(s) / stmts < 0.5: buckets[1] += 1 elif float(s) / stmts < 0.75: buckets[2] += 1 else: buckets[3] += 1 if test_buckets: pass for s in x["state"]: if s["id"] in testees: for v in s["verbs"]: if v["result"]: score = v["result"]["score"]["scaled"] if score < 0.25: test_bucket_hash[s["id"]][0] += 1 elif score < 0.5: test_bucket_hash[s["id"]][1] += 1 elif score < 0.75: test_bucket_hash[s["id"]][2] += 1 else: test_bucket_hash[s["id"]][3] += 1 chartdata1 = {'x': xdata, 'name1': 'Participants', 'y1': buckets} chartdata2 = {'x': xdata} for x in range(0, len(test_bucket_hash.keys())): chartdata2.update({ "name{0}".format(x + 1): test_bucket_info[test_bucket_hash.keys()[x]]["name"], "y{0}".format(x + 1): test_bucket_hash[test_bucket_hash.keys()[x]] }) charttype1 = "discreteBarChart" charttype2 = "multiBarChart" ctx = { "charttype1": charttype1, "chartdata1": chartdata1, "chartcontainer1": "discretebarchart_container", 'extra1': { 'x_is_date': False, 'x_axis_format': '', }, "charttype2": charttype2, "chartdata2": chartdata2, "chartcontainer2": "multibarchart_container", "extra2": { 'x_is_date': False, 'x_axis_format': '', } } end = datetime(datetime.now().year, datetime.now().month, 1) start = end - relativedelta(months=12) year = list(utils.daterange(start, end)) xdata = map(lambda y: 1000 * int(calendar.timegm(y.timetuple())), year) tcp = kwargs["ap_repo"].GetSingleActivityProfile({ "profileId": "outline", "activityId": activityprofile.url }) brand = Site.objects.get_current() stats = [utils.GetStatistics( tcProfile=tcp, activity=activityprofile, organisation=organization, brand=brand, monthly=True, start=month) for month in year] ydata = [[stat.num_started for stat in stats], [stat.average_statements for stat in stats], [stat.average_visit_time for stat in stats], [stat.test_passed_percent for stat in stats]] data = \ { 'charttype3': "stackedAreaChart", 'chartdata3': { 'x': xdata, 'name1': ugettext("Active users"), }, "chartcontainer3": "stackedareachart_container3", 'extra3': { 'x_is_date': True, 'x_axis_format': "%b", 'show_controls': False, 'show_legend': False, 'chart_attr': { 'color': ['#afd000'] } }, 'charttype4': "stackedAreaChart", 'chartdata4': { 'x': xdata, 'name1': ugettext("Avg. statements/user"), }, "chartcontainer4": "stackedareachart_container4", 'extra4': { 'x_is_date': True, 'x_axis_format': "%b", 'show_controls': False, 'show_legend': False, 'chart_attr': { 'color': ['#d44f57'] } }, 'charttype5': "stackedAreaChart", 'chartdata5': { 'x': xdata, 'name1': ugettext("Avg. page visit"), }, "chartcontainer5": "stackedareachart_container5", 'extra5': { 'x_is_date': True, 'x_axis_format': "%b", 'show_controls': False, 'show_legend': False, 'chart_attr': { 'color': ['#18d4fe'] } }, 'charttype6': "stackedAreaChart", 'chartdata6': { 'x': xdata, 'name1': ugettext("Tests passed"), }, "chartcontainer6": "stackedareachart_container6", 'extra6': { 'x_is_date': True, 'x_axis_format': "%b", 'show_controls': False, 'show_legend': False, 'chart_attr': { 'color': ['#ffc213'] } }, "charttype7": "discreteBarChart", "chartdata7": { "name1": ugettext("Page activity"), }, "chartcontainer7": "discretebarchart_container2", 'extra7': { 'x_is_date': False, 'x_axis_format': '', 'margin_bottom': 200, 'xAxis_rotateLabel': -80, }, } if any(ydata[0]): data["chartdata3"]["y1"] = ydata[0] if any(ydata[1]): data["chartdata4"]["y1"] = ydata[1] if any(ydata[2]): data["chartdata5"]["y1"] = ydata[2] if any(ydata[3]): data["chartdata6"]["y1"] = ydata[3] pagestats = utils.get_number_stmts_activity_page(activityprofile, organisation=organization, brand=brand) data["chartdata7"]["x"] = [stat[0]["name"] for stat in pagestats] data["chartdata7"]["y1"] = [stat[1] for stat in pagestats] ctx.update(data) context = RequestContext(request, ctx) context["activity"] = activityprofile return self.render_to_response(context)