def show_profile(request): (profile, permissions, is_default_user) = get_profile_and_permissions(request) if is_default_user: return render(request, "core/please_login.html") tags = [t.name for t in profile.tags.all()[:100]] profile_apps = [] for app in cm.get_registered_participation_apps(): perm = cm.get_provider_permission(app) if app.label + "." + perm.codename in permissions: profile_app = dict() profile_app["label"] = app.label.replace("_", " ").title() profile_app["existing_projects"] = [] profile_app[ "new_project_link"] = "/apps/" + app.label + "/new_project/-1" existing_projects = cm.get_app_project_models( app)[0].objects.filter(owner_profile=profile, is_active=True) for ep in existing_projects: proj = dict() proj["name"] = ep.name proj[ "administer_project_link"] = "/apps/" + app.label + "/administer_project/" + str( ep.id) profile_app["existing_projects"].append(proj) profile_apps.append(profile_app) content = {'profile_apps': profile_apps, 'tags': tags} # add "core" if the user has core permission app = cm.get_core_app() perm = cm.get_provider_permission(app) if app.label + "." + perm.codename in permissions: content["core"] = True return render(request, 'core/profile.html', content)
def journalist_home(request): (profile, permissions, is_default_user) = get_profile_and_permissions(request) if is_default_user: return render(request, "core/please_login.html") profile.role = cm.UserProfile.JOURNALIST profile.save() journalist_apps = [] for app in cm.get_registered_participation_apps(): if not profile.role in app.creator_user_roles_allowed: continue perm = cm.get_provider_permission(app) if app.label + "." + perm.codename in permissions: journalist_app = dict() journalist_app["label"] = app.label.replace("_", " ").title() journalist_app["existing_projects"] = [] journalist_app[ "new_project_link"] = "/apps/{}/new_project/-1".format( app.label) existing_projects = cm.get_app_project_models( app)[0].objects.filter(owner_profile=profile, is_active=True) for ep in existing_projects: proj = dict() proj["name"] = ep.name proj[ "administer_project_link"] = "/apps/" + app.label + "/administer_project/" + str( ep.id) journalist_app["existing_projects"].append(proj) journalist_apps.append(journalist_app) context = dict() context['journalist_apps'] = journalist_apps return render(request, 'core/journalist_home.html', context)
def handle(self, *args, **options): username = options["username"] app_name = options["participationapp"] app = cm.get_app_by_name(app_name.lower()) user = User.objects.get(username=username) perm = cm.get_provider_permission(app) user.user_permissions.add(perm)
def moderate_issues(request): (profile, permissions, is_default_user) = get_profile_and_permissions(request) app = cm.get_core_app() perm = cm.get_provider_permission(app) if not app.label + "." + perm.codename in permissions: return HttpResponseForbidden() recent_events = cm.Event.objects.order_by('-timestamp')[:100] recent_events = [(str(i.path), str(i.timestamp)) for i in recent_events] recent_issues = cm.IssueReport.objects.order_by('-event__timestamp')[:100] recent_issues = [(str(i.title), str(i.issue_type), str(i.event.path), str(i.event.timestamp)) for i in recent_issues] return render(request, 'core/moderate_issues.html', { "recent_issues": recent_issues, "recent_events": recent_events })
def tags(request): (profile, permissions, is_default_user) = get_profile_and_permissions(request) app = cm.get_core_app() perm = cm.get_provider_permission(app) if not app.label + "." + perm.codename in permissions: return HttpResponseForbidden() reports = [] all_tags = cm.Tag.objects.all() reports.append({ "label": "Tags", "num": all_tags.distinct().count(), "top10": all_tags[:10] }) cities = cm.GeoTag.objects.filter(feature_type=cm.GeoTag.CITY) reports.append({ "label": "Cities", "num": cities.distinct().count(), "top10": cities[:10] }) states = cm.GeoTag.objects.filter(feature_type=cm.GeoTag.STATE_OR_PROVINCE) reports.append({ "label": "States", "num": states.distinct().count(), "top10": states[:10] }) countries = cm.GeoTag.objects.filter(feature_type=cm.GeoTag.COUNTRY) reports.append({ "label": "Countries", "num": countries.distinct().count(), "top10": countries[:10] }) other = cm.GeoTag.objects.filter(feature_type=cm.GeoTag.OTHER) reports.append({ "label": "Other", "num": other.distinct().count(), "top10": other[:10] }) unk = cm.GeoTag.objects.filter(feature_type=cm.GeoTag.UNKNOWN) reports.append({ "label": "Unknown", "num": unk.distinct().count(), "top10": unk[:10] }) return render(request, 'core/tags.html', {'reports': reports})
def upload_dataset(request): (profile, permissions, is_default_user) = get_profile_and_permissions(request) if is_default_user: return render(request, "core/please_login.html") app = cm.get_core_app() perm = cm.get_provider_permission(app) if not app.label + "." + perm.codename in permissions: return HttpResponseForbidden() if request.method == 'POST': form = UploadDataset(request.POST, request.FILES) if form.is_valid(): if form.cleaned_data["format_id"] == "uscitieslist_csv_v0": tasks.insert_uscitieslist_v0.delay( form.cleaned_data["small_test"]) return render( request, "core/thanks.html", {"action_description": "uploading this data file"}) elif form.cleaned_data["format_id"] == "states_v1": tasks.insert_states.delay(form.cleaned_data["small_test"]) return render( request, "core/thanks.html", {"action_description": "uploading this data file"}) elif form.cleaned_data["format_id"] == "openstates_subjects_v1": tasks.insert_openstates_subjects.delay( form.cleaned_data["small_test"]) return render( request, "core/thanks.html", {"action_description": "uploading this data file"}) else: return HttpResponse("Sorry, this format is not known") else: return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path, "enctype_data": True }) else: form = UploadDataset() return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path, "enctype_data": True })
def create_shortcut(request): (profile, permissions, is_default_user) = get_profile_and_permissions(request) if is_default_user: return render(request, "core/please_login.html") app = cm.get_core_app() perm = cm.get_provider_permission(app) if not app.label + "." + perm.codename in permissions: return HttpResponseForbidden() if request.method == 'POST': form = CreateShortcutForm(request.POST) if form.is_valid(): # delete any existing shortcuts with the same string num_deleted = cm.Shortcut.objects.filter( shortcut_string=form.cleaned_data["shortcut_string"]).delete() deleted_string = "this many existing shortcuts were deleted: " + str( num_deleted) # create new shortcut s = cm.Shortcut() item = get_object_or_404(cm.ParticipationItem, id=form.cleaned_data["item_id"], is_active=True) s.shortcut_string = form.cleaned_data["shortcut_string"] s.target_item = item s.save() return render(request, "core/thanks.html", { "action_description": "creating this shortcut. " + deleted_string }) else: return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path, "enctype_data": True }) else: form = CreateShortcutForm() return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path, "enctype_data": True })
def portal_stats(request): (profile, permissions, is_default_user) = get_profile_and_permissions(request) app = cm.get_core_app() perm = cm.get_provider_permission(app) if not app.label + "." + perm.codename in permissions: return HttpResponseForbidden() num_users = cm.UserProfile.objects.count() num_projects = cm.ParticipationProject.objects.filter( is_active=True).count() num_items = cm.ParticipationItem.objects.filter(is_active=True).count() num_tags = cm.Tag.objects.count() num_visits = cm.ParticipationItem.objects.aggregate( Sum('visits'))["visits__sum"] return render( request, 'core/portal_stats.html', { "num_users": num_users, "num_projects": num_projects, "num_items": num_items, "num_tags": num_tags, "num_visits": num_visits })
def manage_group(request, group_id): (profile, permissions, is_default_user) = get_profile_and_permissions(request) if is_default_user: return render(request, "core/please_login.html") group = get_object_or_404(cm.UserGroup, id=group_id, owner=profile) edu_apps = [] for app in cm.get_registered_participation_apps(): if not profile.role in app.creator_user_roles_allowed: continue perm = cm.get_provider_permission(app) if app.label + "." + perm.codename in permissions: edu_app = dict() edu_app["label"] = app.label.replace("_", " ").title() edu_app["existing_projects"] = [] edu_app["new_project_link"] = "/apps/{}/new_project/{}".format( app.label, group_id) existing_projects = cm.get_app_project_models( app)[0].objects.filter(owner_profile=profile, group=group, is_active=True) for ep in existing_projects: proj = dict() proj["name"] = ep.name proj[ "administer_project_link"] = "/apps/" + app.label + "/administer_project/" + str( ep.id) edu_app["existing_projects"].append(proj) edu_apps.append(edu_app) context = dict() context["group"] = group context["action_path"] = request.path context['edu_apps'] = edu_apps context[ "content_class_word"] = "content" if not group.group_type == cm.UserGroup.COURSE else "assignments" context[ "member_word"] = "member" if not group.group_type == cm.UserGroup.COURSE else "student" if request.method == 'POST': form = ManageGroupForm(request.POST) if form.is_valid(): name = form.cleaned_data["new_invitation_name"] email = form.cleaned_data["new_invitation_email"] num_invitations = cm.GroupMembership.objects.filter( group=group).count() if num_invitations >= group.max_invitations: return HttpResponse( "You have reached the maximum number of invitations for this {}. Contact admin to increase this" .format(group.group_type)) try: existing_invitation = cm.GroupMembership.objects.get( member_name=name, group=group) return HttpResponse( "You already have an invitation for {} in the {}: {}.". format(name, group.group_type, group.name)) except cm.GroupMembership.DoesNotExist: inv = cm.GroupMembership() inv.member_name = name inv.group = group inv.invitation_email = email inv.invitation_code = uuid.uuid1() inv.save() return HttpResponseRedirect( "/manage_group/{}".format(group_id)) else: sys.stderr.write("Form errors: {}\n".format(form.errors)) sys.stderr.flush() return render(request, 'core/manage_group.html', context) return render(request, 'core/manage_group.html', context)
def app_view_relay(request, app_name, action_name, object_id): """ The primary routing view for the major portal actions involving participation projects and items: creating projects, administering project, participating in items, and deleting projects This view checks permissions, then routes the request to app-specific views. """ (profile, permissions, is_default_user) = get_profile_and_permissions(request) if not app_name in [ app.label for app in cm.get_registered_participation_apps() ]: raise Exception("no such app") else: app = [ a for a in cm.get_registered_participation_apps() if a.name == app_name ][0] perm = cm.get_provider_permission(app) has_app_perm = app.label + "." + perm.codename in permissions if action_name == "new_project": if has_app_perm: group = None if object_id != "-1": group = get_object_or_404(cm.UserGroup, pk=object_id, owner=profile) return app.views_module.new_project(request, group) else: return render( request, 'core/no_permissions.html', { "title": "No Permission", "app_name": app_name, "action_description": "create a new project" }) elif action_name == "administer_project": if has_app_perm: get_object_or_404(cm.ParticipationProject, pk=object_id, owner_profile=profile, is_active=True) return app.views_module.administer_project(request, object_id) else: return render( request, 'core/no_permissions.html', { "title": "No Permission", "app_name": app_name, "action_description": "administer a project" }) elif action_name == "participate": get_object_or_404(cm.ParticipationItem, pk=object_id, is_active=True) cm.ParticipationItem.objects.filter( pk=object_id).distinct().update(visits=F('visits') + 1) return app.views_module.participate(request, object_id) elif action_name == "overview": return app.views_module.overview(request, object_id) elif action_name == "item_info": if not request.is_ajax() or not request.method == "POST": return HttpResponse(status=500) # default item info item = get_object_or_404(cm.ParticipationItem, pk=object_id) ans = { "item": { "id": item.id, "img_url": settings.STATIC_URL + item.display_image_file, "link": item.participate_link(), "title": item.name, "label": item.get_inline_display(), "tags": [t.name for t in item.tags.all()] }, "site": os.environ["SITE"] } if "item_info" in app.views_module.__dict__: return app.views_module.item_info(request, object_id, ans) else: return JsonResponse(ans) elif action_name == "delete_project": if has_app_perm: project = get_object_or_404(cm.ParticipationProject, pk=object_id, is_active=True, owner_profile=profile) if request.method == 'POST': form = DeleteProjectConfirmationForm(request.POST) if form.is_valid(): project.participationitem_set.update(is_active=False) project.is_active = False project.save() return render( request, "core/thanks.html", {"action_description": "removing " + project.name}) else: return HttpResponse(status=500) else: form = DeleteProjectConfirmationForm() items = cm.ParticipationItem.objects.filter( participation_project=project, is_active=True) items = [get_item_details(i, True) for i in items] return render( request, 'core/delete_project_confirmation.html', { 'form': form, 'action_path': request.path, "items": items, 'site': os.environ["SITE"] }) else: return render( request, 'core/no_permissions.html', { "title": "No Permission", "app_name": app_name, "action_description": "delete a project" }) elif action_name == "edit_project": if not app.are_projects_editable: return HttpResponse(status=500) if has_app_perm: project = get_object_or_404(cm.ParticipationProject, pk=object_id, is_active=True, owner_profile=profile) return app.views_module.edit_project(request, object_id) else: return render( request, 'core/no_permissions.html', { "title": "No Permission", "app_name": app_name, "action_description": "delete a project" }) elif action_name.startswith("customAction") and action_name.isalpha( ) and len( action_name) < 50 and action_name in app.views_module.__dict__: return app.views_module.__dict__[action_name](request, object_id) else: raise Exception("invalid action:" + str(action_name))