def participate(request, item_id): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) item = ReadingAssignmentItem.objects.get(pk=item_id) context = cv.get_default_og_metadata(request, item) project = item.participation_project.readingassignmentproject title = project.name if request.method == 'POST': form = SubmitAssignmentForm(project, request.POST ) if form.is_valid(): sub = Submission() sub.user_profile = profile sub.participation_item = item sub.save() for k in form.cleaned_data.keys(): if k.startswith("text_question_"): qid = int(k[14:]) question = TextQuestion.objects.get(id=qid) item_response = TextQuestionResponse(question=question, response=form.cleaned_data[k], submission=sub) item_response.save() # save responses to questions context.update({"action_description": "submitting this reading assignment", "item": item}) return render(request, 'core/thanks_participate.html', context) else: context.update({'form': form, 'action_path' : request.path, 'title' : title, 'item':item}) return render(request, 'reading_assignment/participate.html', context) else: form = SubmitAssignmentForm(project) context.update({'form': form, 'action_path' : request.path, 'title' : title, 'item': item}) return render(request, 'reading_assignment/participate.html', context)
def participate(request, item_id): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) item = None project = None item_type = None context = dict() try: item = LegislatorsItem.objects.get(pk=item_id) project = item.participation_project.legislatorsproject item_type = "legislators" except LegislatorsItem.DoesNotExist: item = BillsItem.objects.get(pk=item_id) state = [ s for s in cm.GeoTag.objects.filter(feature_type="SP") if s.tag_ptr in item.tags.all() ][0] context["state"] = {"id": state.tag_ptr.id, "name": state.name} project = item.participation_project.billsproject item_type = "bills" context.update(cv.get_default_og_metadata(request, item)) context.update({ 'site': os.environ["SITE"], "item": item, "project": project }) return render(request, "legislators/{}_participate.html".format(item_type), context)
def item_info(request, item_id, ans): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) item = cm.ParticipationItem.objects.get( pk=item_id).interactivevisualizationitem project = item.participation_project.interactivevisualizationproject project_keys = { "project_id": "id", "project_name": "name", "switch_variable": "switch_variable", "switch_title": "switch_title", "switch_note": "switch_note", "methodology_note": "methodology_note", "methodology_url": "methodology_url", "bar1_variable": "bar1_variable", "bar1_title": "bar1_title", "bar1_x_label": "bar1_x_label", "bar1_y_label": "bar1_y_label", "pie1_variable": "pie1_variable", "pie1_title": "pie1_title", "bar2_variable": "bar2_variable", "bar2_title": "bar2_title", "bar2_x_label": "bar2_x_label", "bar2_y_label": "bar2_y_label", "pie2_variable": "pie2_variable", "pie2_title": "pie2_title" } for k in project_keys.keys(): ans["item"][k] = project.__dict__[project_keys[k]] return JsonResponse(ans)
def participate(request, item_id): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) item = CityBudgetingItem.objects.get(pk=item_id) context = cv.get_default_og_metadata(request, item) project = item.participation_project.citybudgetingproject data = json.loads(project.budget_json) fund_map = {i["id"]: i for i in data["funds"]} revenue_categories = { i[0]: i[1] for i in enumerate(set([x["category"] for x in data["revenues"]])) } expense_categories = { i[0]: i[1] for i in enumerate(set([x["category"] for x in data["expenses"]])) } context.update({ "project": project, "data": data, "site": os.environ["SITE"], "item": item, "fund_map": fund_map, "revenue_categories": revenue_categories, "expense_categories": expense_categories }) return render(request, "city_budgeting/participate.html", context)
def participate(request, item_id): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) item = LandUseParticipationItem.objects.get(pk=item_id) context = cv.get_default_og_metadata(request, item) project = item.participation_project.landuseproject title = project.name if request.method == 'POST': form = ItemResponseForm(project, request.POST) if form.is_valid(): item_response = ItemResponse() item_response.user_profile = profile item_response.participation_item = item item_response.save() # TODO: instead, use LandUseProject.get_questions() for key in form.cleaned_data: if "field_prf_" in key: question_id = key.lstrip("field_prf_") question = get_object_or_404(Question, pk=question_id) try: tmcq = question.tmcq except: raise Exception( "Invalid question type. Only TMCQ supported") else: qr = TMCQResponse() qr.item_response = item_response qr.question = question qr.option_index = int(form.cleaned_data[key]) qr.save() context.update({ "action_description": "responding to the land use planning project: " + title, "item": item }) return render(request, 'core/thanks_participate.html', context) else: context.update({ 'form': form, 'action_path': request.path, 'title': title, 'item': item }) return render(request, 'core/generic_form_participate.html', context) else: form = ItemResponseForm(project) context.update({ 'form': form, 'action_path': request.path, 'title': title, 'item': item }) return render(request, 'core/generic_form_participate.html', context)
def new_project(request, group=None): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) if request.method == 'POST': form = CreateProjectForm(request.POST) if form.is_valid(): project = ToolReviewProject() project.name = form.cleaned_data["tool_name"] project.url = form.cleaned_data["tool_url"] project.tool_category = form.cleaned_data["tool_category"] project.project_category = form.cleaned_data["project_category"] screenshot_url = form.cleaned_data["screenshot"] path_with_bucket_and_leading_slash = urlsplit(screenshot_url)[2] path_without_bucket = "/".join( path_with_bucket_and_leading_slash.split("/")[2:]) project.screenshot_filename = path_without_bucket if "youtube_video_id" in form.cleaned_data: project.youtube_video_id = form.cleaned_data[ "youtube_video_id"] project.summary = form.cleaned_data["summary"] project.owner_profile = profile project.group = group project.save() t1 = cf.get_best_final_matching_tag(form.cleaned_data["tag1"]) if not t1 is None: project.tags.add(t1) t2 = cf.get_best_final_matching_tag(form.cleaned_data["tag2"]) if not t2 is None: project.tags.add(t2) t3 = cf.get_best_final_matching_tag(form.cleaned_data["tag3"]) if not t3 is None: project.tags.add(t3) ct.finalize_project(project) return render( request, 'core/thanks.html', { "action_description": "creating a new tool review", "link": "/apps/tool_review/administer_project/" + str(project.id) }) else: return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path }) else: form = CreateProjectForm() return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path })
def edit_project(request, project_id): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) project = get_object_or_404(ManualNewsCurationProject, pk=project_id) basic_fields = ["name", "url", "first_paragraph"] if request.method == 'POST': form = EditProjectForm(request.POST) changes = set() if form.is_valid(): for key in basic_fields: if not project.__dict__[key] == form.cleaned_data[key]: project.__dict__[key] = form.cleaned_data[key] changes.add(key) # ignore screenshot, too hard project.save() current_tags = set(project.tags.all()) new_tags = set() for key, val in form.cleaned_data.items(): if key.startswith("tag"): t = cf.get_best_final_matching_tag(val) if not t is None: new_tags.add(t) if len(new_tags.symmetric_difference(current_tags)) > 0: project.tags.clear() project.tags.add(*new_tags) changes.add("tags") if "name" in changes or "tags" in changes: ct.finalize_project(project) return render( request, 'core/thanks.html', { "action_description": "editing your tool review", "link": "/apps/tool_review/administer_project/" + str(project.id) }) else: return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path }) else: data = {k: project.__dict__[k] for k in basic_fields} current_tags = list(project.tags.all()) for i, t in enumerate(current_tags): if i > 2: break data["tag" + str(i + 1)] = t.get_name() form = EditProjectForm(data) return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path })
def participate(request, item_id): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) item = InteractiveVisualizationItem.objects.get(pk=item_id) context = cv.get_default_og_metadata(request, item) project = item.participation_project.interactivevisualizationproject context["project"] = project context["item"] = item return render(request, 'interactive_visualization/participate.html', context)
def participate(request, item_id): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) item = ToolReviewItem.objects.get(pk=item_id) context = cv.get_default_og_metadata(request, item) project = item.participation_project.toolreviewproject context.update({"review": project, 'item': item}) return render(request, 'tool_review/participate.html', context)
def item_info(request, item_id, ans): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) item = cm.ParticipationItem.objects.get(pk=item_id).singlequizitem project = item.participation_project.singlequizproject project_keys = {"option1":"option1", "option2":"option2", "option3":"option3", "option4":"option4", "option5":"option5", "citation_url": "citation_url"} for k in project_keys.keys(): ans["item"][k] = project.__dict__[project_keys[k]] return JsonResponse(ans)
def new_project(request, group=None): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) if request.method == 'POST': form = CreateProjectForm(request.POST) if form.is_valid(): project = CityBudgetingProject() project.city = cf.get_best_final_matching_tag( form.cleaned_data["city"]).geotag simple_fields = [ "fiscal_period_start", "fiscal_period_end", "budget_url", "name", "budget_description", "revenues_description", "funds_description", "expenses_description" ] for k in simple_fields: project.__dict__[k] = form.cleaned_data[k] excel_file_url = form.cleaned_data["budget_excel_file"] path_with_bucket_and_leading_slash = urlsplit(excel_file_url)[2] path_without_bucket = "/".join( path_with_bucket_and_leading_slash.split("/")[2:]) project.budget_excel_file = path_without_bucket project.owner_profile = profile project.group = group project.save() ct.finalize_project(project) return render( request, 'core/thanks.html', { "action_description": "creating a new city budget project", "link": "/apps/city_budgeting/administer_project/" + str(project.id) }) else: return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path }) else: initial = { "download_link_1": default_storage.url("city_budgeting/misc/example.xlsx") } form = CreateProjectForm(initial=initial) return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path })
def new_project(request, group=None): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) AssignmentItemsFormset = forms.formset_factory(AssignmentItemForm, can_delete=True) if request.method == 'POST': formset = AssignmentItemsFormset(request.POST, form_kwargs={'userprofile':profile}) project_form = CreateProjectForm(request.POST) if formset.is_valid() and project_form.is_valid(): sys.stderr.write("formset data:\n{}\n".format(formset.cleaned_data)) sys.stderr.flush() project = ReadingAssignmentProject() project.owner_profile = profile project.name = project_form.cleaned_data["assignment_name"] project.group = group project.save() num = 0 for item in formset.cleaned_data: if item.get("text_question", u'') != u'': num += 1 q = TextQuestion() q.question_text = item["text_question"] q.save() oai = OrderedAssignmentItem() oai.number = num oai.text_question = q oai.assignment=project oai.save() elif not item.get("participation_item", None) is None: num += 1 oai = OrderedAssignmentItem() oai.number = num oai.participation_item = cm.ParticipationItem.objects.get(id=item["participation_item"], is_active=True) oai.assignment=project oai.save() else: # this item is blank, ignore it pass ct.finalize_project(project) return render(request, 'core/thanks.html', {"action_description": "creating a new reading assignment", "link": "/apps/reading_assignment/administer_project/"+str(project.id)}) else: sys.stderr.write("formset errors:\n{}\n".format(formset.errors)) sys.stderr.write("project form errors:\n{}\n".format(project_form.errors)) sys.stderr.flush() raise Exception() else: project_form = CreateProjectForm() formset = AssignmentItemsFormset(form_kwargs={'userprofile':profile}) return render(request, 'reading_assignment/new_project.html', {'project_form': project_form, 'items_formset': formset})
def new_project(request, group=None): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) if request.method == 'POST': form = CreateProjectForm(request.POST) if form.is_valid(): project = ManualNewsCurationProject() project.name = form.cleaned_data["name"] project.url = form.cleaned_data["url"] project.first_paragraph = form.cleaned_data["first_paragraph"] screenshot_url = form.cleaned_data["screenshot"] path_with_bucket_and_leading_slash = urlsplit(screenshot_url)[2] path_without_bucket = "/".join( path_with_bucket_and_leading_slash.split("/")[2:]) project.screenshot_filename = path_without_bucket project.owner_profile = profile project.group = group project.save() # iterate through form adding tags for key, val in form.cleaned_data.items(): if key.startswith("tag"): t = cf.get_best_final_matching_tag(val) if not t is None: project.tags.add(t) ct.finalize_project(project) return render( request, 'core/thanks.html', { "action_description": "creating a new news article project", "link": "/apps/manual_news_article_curation/administer_project/" + str(project.id) }) else: return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path }) else: form = CreateProjectForm() return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path })
def new_project(request, group=None): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) if request.method == 'POST': form = CreateProjectForm(request.POST) if form.is_valid(): project = LandUseProject() project.name = form.cleaned_data["project_name"] project.group = group # !!! Warning, Google lists geo data lat,lon. # everyone else, including better dem portal, does lon, lat poly_data = [(x[1], x[0]) for x in form.cleaned_data["polygon_field"]] poly_data.append( poly_data[0] ) # polygon must be instantiated with a closed ring project.polygon = Polygon(LinearRing(tuple(poly_data))) project.owner_profile = profile project.save() goals = FeedbackGoal.objects.all() for goal in goals: var_name = goal.name + "_pref" if form.cleaned_data[var_name]: project.feedback_goals.add(goal) ct.finalize_project(project) return render( request, 'core/thanks.html', { "action_description": "creating a new land use planning project", "link": "/apps/land_use_planning/administer_project/" + str(project.id) }) else: return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path }) else: form = CreateProjectForm() return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path })
def new_project(request, group=None): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) if request.method == 'POST': form = CreateProjectForm(request.POST) if form.is_valid(): project = InteractiveVisualizationProject() project.owner_profile = profile project.group = group # first time through, create the project for key, val in form.cleaned_data.items(): if key == "visualization_title": project.name = val elif not key.startswith("tag"): project.__dict__[key] = val project.save() # second time, add tags to the project for key, val in form.cleaned_data.items(): if key.startswith("tag"): t = cf.get_best_final_matching_tag(form.cleaned_data[key]) if not t is None: project.tags.add(t) ct.finalize_project(project) return render( request, 'core/thanks.html', { "action_description": "creating a new interactive visualization", "link": "/apps/interactive_visualization/administer_project/" + str(project.id) }) else: return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path }) else: form = CreateProjectForm() return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path })
def new_project(request, group=None): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) if request.method == 'POST': form = CreateProjectForm(request.POST) if form.is_valid(): project = SingleQuizProject() project.name = form.cleaned_data["question_text"] project.group = group project.question_text = form.cleaned_data["question_text"] project.option1=form.cleaned_data["option1"] project.option2=form.cleaned_data["option2"] if "option3" in form.cleaned_data: project.option3=form.cleaned_data["option3"] if "option4" in form.cleaned_data: project.option4=form.cleaned_data["option4"] if "option5" in form.cleaned_data: project.option5=form.cleaned_data["option5"] project.correct_answer_index = form.cleaned_data["correct_answer_index"] project.citation_url = form.cleaned_data["citation_url"] project.explanation = form.cleaned_data["explanation"] project.owner_profile = profile project.save() t1 = cf.get_best_final_matching_tag(form.cleaned_data["tag1"]) if not t1 is None: project.tags.add(t1) t2 = cf.get_best_final_matching_tag(form.cleaned_data["tag2"]) if not t2 is None: project.tags.add(t2) t3 = cf.get_best_final_matching_tag(form.cleaned_data["tag3"]) if not t3 is None: project.tags.add(t3) ct.finalize_project(project) return render(request, 'core/thanks.html', {"action_description": "creating a new single quiz", "link": "/apps/single_quiz/administer_project/"+str(project.id)}) else: return render(request, 'core/generic_form.html', {'form': form, 'action_path' : request.path}) else: form = CreateProjectForm() return render(request, 'core/generic_form.html', {'form': form, 'action_path' : request.path })
def participate(request, item_id): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) item = BeatTheBullshitItem.objects.get(pk=item_id) context = cv.get_default_og_metadata(request, item) project = item.participation_project.beatthebullshitproject context.update({ "project": project, 'site': os.environ["SITE"], "item": item, "fallacies": Fallacy.objects.all() }) if not request.method == "POST": return render(request, 'beat_the_bullshit/participate.html', context) if request.method == 'POST' and request.is_ajax(): submission_data = json.loads(request.body.strip()) if "type" in submission_data and submission_data[ "type"] == "quote_fallacy_quiz_item_submit": qid = submission_data["quote_id"] quote = get_object_or_404(Quote, pk=qid, project=project) fid = submission_data["fallacy_id"] fallacy = get_object_or_404(Fallacy, pk=fid) response = QuoteFallacyQuizItemResponse() response.user_profile = profile response.participation_item = item response.choice = fallacy association = QuoteFallacyAssociation.objects.get( quote=quote ) # assumes there is only one fallacy associated with this quote is_correct = (association.fallacy.id == fallacy.id) response.is_correct = is_correct response.save() content = dict() content["correct_fallacy_name"] = association.fallacy.name content["is_correct"] = is_correct content["explanation"] = association.explanation content["improvement"] = association.improvement return JsonResponse(content) else: return HttpResponse(status=500)
def participate(request, item_id): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) item = SingleQuizItem.objects.get(pk=item_id) context = cv.get_default_og_metadata(request, item) project = item.participation_project.singlequizproject if request.method == 'POST': form = None # populate the form differently depending on whether data is from ajax if request.is_ajax(): submission = request.body.strip() submission = json.loads(submission) assert(len(submission) == 1) data = {"choice":submission.values()[0]} form = ParticipateForm(item, data) else: form = ParticipateForm(item, request.POST) # respond differently if form.is_valid(): if form.cleaned_data["choice"] == str(project.correct_answer_index): content = {"reveal": ["correct", "sources"], "hide": ["incorrect", "single_quiz_ajax_form"], "response": "Correct!", "explanation": project.explanation} if request.is_ajax(): return JsonResponse(content) else: content.update({'action_description': "responding to this mini-quiz", "ans_correct": True, "source": project.citation_url, "item":item}) return render(request, 'single_quiz/thanks.html', content) else: content = {"reveal": ["incorrect", "sources"], "hide": ["correct", "single_quiz_ajax_form"], "response": "Sorry, the correct answer was: "+project.__dict__["option"+str(project.correct_answer_index)], "explanation": project.explanation} if request.is_ajax(): return JsonResponse(content) else: content.update({'action_description': "responding to this mini-quiz", "ans_correct": False, "source": project.citation_url, "item":item}) return render(request, 'single_quiz/thanks.html', content) else: return render(request, 'core/generic_form_participate.html', {'form': form, 'action_path': request.path, "form_title": project.question_text, 'item':item}) else: form = ParticipateForm(item) return render(request, 'core/generic_form_participate.html', {'form': form, 'action_path': request.path, "form_title": project.question_text, "item":item})
def edit_project(request, project_id): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) project = get_object_or_404(BeatTheBullshitProject, pk=project_id) basic_fields = [ "name", "topic_overview", "bullet_1", "bullet_2", "bullet_3" ] if request.method == 'POST': form = EditProjectForm(project, request.POST) changes = set() if form.is_valid(): for key in basic_fields: if not project.__dict__[key] == form.cleaned_data[key]: project.__dict__[key] = form.cleaned_data[key] changes.add(key) project.save() # allow adding more points of view, basics, and effects for i in range(1, 4): quote = form.cleaned_data.get("quote" + str(i), None) if not quote is None and not quote == "": q = Quote() q.quote_string = quote q.speaker_name = form.cleaned_data["speaker_name" + str(i)] q.reference = form.cleaned_data["reference" + str(i)] screenshot_url = form.cleaned_data["screenshot_filename" + str(i)] path_with_bucket_and_leading_slash = urlsplit( screenshot_url)[2] path_without_bucket = "/".join( path_with_bucket_and_leading_slash.split("/")[2:]) q.screenshot_filename = path_without_bucket if "youtube_video_id" + str(i) in form.cleaned_data: q.youtube_video_id = form.cleaned_data[ "youtube_video_id" + str(i)] q.project = project q.save() qfa = QuoteFallacyAssociation() qfa.quote = q qfa.fallacy = form.cleaned_data["fallacy" + str(i)] qfa.explanation = form.cleaned_data[ "fallacy_association_explanation" + str(i)] qfa.improvement = form.cleaned_data[ "fallacy_association_improvement" + str(i)] qfa.save() # Allow deleting quotes for key, val in form.cleaned_data.items(): if key.startswith("delete_quote_") and val: changes.add("del_quotes") quote_id = int(key.replace("delete_quote_", "")) assert (project.quote_set.filter(id=quote_id).exists()) project.quote_set.filter(id=quote_id).delete() Quote.objects.filter(id=quote_id).delete() # quote-filter-assiciation is deleted with cascade current_tags = set(project.tags.all()) new_tags = set() for key, val in form.cleaned_data.items(): if key.startswith("tag"): t = cf.get_best_final_matching_tag(val) if not t is None: new_tags.add(t) if len(new_tags.symmetric_difference(current_tags)) > 0: project.tags.clear() project.tags.add(*new_tags) changes.add("tags") if "name" in changes or "tags" in changes: ct.finalize_project(project) return render( request, 'core/thanks.html', { "action_description": "editing your beat-the-bullshit project", "link": "/apps/beat_the_bullshit/administer_project/" + str(project.id) }) else: return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path }) else: data = {k: project.__dict__[k] for k in basic_fields} current_tags = list(project.tags.all()) for i, t in enumerate(current_tags): if i > 2: break data["tag" + str(i + 1)] = t.get_name() sys.stderr.write(str(data)) sys.stderr.flush() form = EditProjectForm(project, data) return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path })
def new_project(request, group=None): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) if request.method == 'POST': form = CreateProjectForm(request.POST) if form.is_valid(): project = BeatTheBullshitProject() project.name = form.cleaned_data["name"] project.group = group project.owner_profile = profile project.topic_overview = form.cleaned_data["topic_overview"] for i in range(1, 4): k = "bullet_" + str(i) if k in form.cleaned_data: project.__dict__[k] = form.cleaned_data[k] project.save() for i in range(1, 4): q_string = form.cleaned_data.get("quote" + str(i), None) if not q_string is None and not q_string == "": q = Quote() q.quote_string = q_string q.speaker_name = form.cleaned_data["speaker_name" + str(i)] q.reference = form.cleaned_data["reference" + str(i)] screenshot_url = form.cleaned_data["screenshot_filename" + str(i)] path_with_bucket_and_leading_slash = urlsplit( screenshot_url)[2] path_without_bucket = "/".join( path_with_bucket_and_leading_slash.split("/")[2:]) q.screenshot_filename = path_without_bucket if "youtube_video_id" + str(i) in form.cleaned_data: q.youtube_video_id = form.cleaned_data[ "youtube_video_id" + str(i)] q.project = project q.save() qfa = QuoteFallacyAssociation() qfa.quote = q qfa.fallacy = form.cleaned_data["fallacy" + str(i)] qfa.explanation = form.cleaned_data[ "fallacy_association_explanation" + str(i)] qfa.improvement = form.cleaned_data[ "fallacy_association_improvement" + str(i)] qfa.save() # iterate through form adding tags for key, val in form.cleaned_data.items(): if key.startswith("tag"): t = cf.get_best_final_matching_tag(val) if not t is None: project.tags.add(t) ct.finalize_project(project) return render( request, 'core/thanks.html', { "action_description": "creating a new ballot decider project", "link": "/apps/beat_the_bullshit/administer_project/" + str(project.id) }) else: return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path }) else: form = CreateProjectForm() return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path })
def edit_project(request, project_id): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) project = get_object_or_404(InteractiveVisualizationProject, pk=project_id) if request.method == 'POST': form = CreateProjectForm(request.POST) changes = set() if form.is_valid(): for key, val in form.cleaned_data.items(): if key == "visualization_title": if not project.name == val: changes.add("name") project.name = val elif not key.startswith("tag"): if not project.__dict__[key] == val: project.__dict__[key] = val changes.add(key) project.save() current_tags = set(project.tags.all()) new_tags = set() for key, val in form.cleaned_data.items(): if key.startswith("tag"): t = cf.get_best_final_matching_tag(val) if not t is None: new_tags.add(t) if len(new_tags.symmetric_difference(current_tags)) > 0: project.tags.clear() project.tags.add(*new_tags) changes.add("tags") if "name" in changes or "tags" in changes: ct.finalize_project(project) return render( request, 'core/thanks.html', { "action_description": "editing your interactive visualization", "link": "/apps/interactive_visualization/administer_project/" + str(project.id) }) else: return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path }) else: data = { i: project.__dict__[i] for i in [ "methodology_note", "methodology_url", "csv_data", "switch_variable", "switch_title", "switch_note", "pie1_variable", "pie1_title", "pie2_variable", "pie2_title", "bar1_variable", "bar1_title", "bar1_x_label", "bar1_y_label", "bar2_variable", "bar2_title", "bar2_x_label", "bar2_y_label" ] } data["visualization_title"] = project.name current_tags = list(project.tags.all()) for i, t in enumerate(current_tags): if i > 2: break data["tag" + str(i + 1)] = t.get_name() form = CreateProjectForm(data) return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path })
def edit_project(request, project_id): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) project = get_object_or_404(BallotDeciderProject, pk=project_id) basic_fields = { "name": "measure_name", "ballot_text": "ballot_text", "election_date": "election_date", "election_website": "election_website", "basics_notes": "basics_notes", "effects_notes": "effects_notes" } if request.method == 'POST': form = EditProjectForm(project, request.POST) changes = set() if form.is_valid(): for key in basic_fields: if not project.__dict__[key] == form.cleaned_data[ basic_fields[key]]: project.__dict__[key] = form.cleaned_data[ basic_fields[key]] changes.add(key) project.save() # allow adding more points of view, basics, and effects for i in range(1, 4): data = form.cleaned_data.get("basics" + str(i), None) if not data is None and not data == "": item = cm.ParticipationItem.objects.get(id=data, is_active=True) project.basics.add(item) changes.add("basics") for i in range(1, 4): data = form.cleaned_data.get("effects" + str(i), None) if not data is None and not data == "": item = cm.ParticipationItem.objects.get(id=data, is_active=True) project.effects.add(item) changes.add("effects") for i in range(1, 4): quote = form.cleaned_data.get("pov_quote_" + str(i), None) if not quote is None and not quote == "": pov = PointOfView() pov.quote = quote pov.is_favorable = form.cleaned_data["pov_is_favorable_" + str(i)] pov.save() project.points_of_view.add(pov) changes.add("povs") # Allow deleting POVs for key, val in form.cleaned_data.items(): if key.startswith("delete_pov_") and val: changes.add("del_povs") pov_id = int(key.replace("delete_pov_", "")) assert (project.points_of_view.filter(id=pov_id).exists()) project.points_of_view.filter(id=pov_id).delete() PointOfView.objects.filter(id=pov_id).delete() current_tags = set(project.tags.all()) new_tags = set() for key, val in form.cleaned_data.items(): if key.startswith("tag"): t = cf.get_best_final_matching_tag(val) if not t is None: new_tags.add(t) if len(new_tags.symmetric_difference(current_tags)) > 0: project.tags.clear() project.tags.add(*new_tags) changes.add("tags") if "name" in changes or "tags" in changes: ct.finalize_project(project) return render( request, 'core/thanks.html', { "action_description": "editing your ballot decider", "link": "/apps/ballot_decider/administer_project/" + str(project.id) }) else: return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path }) else: data = {i[1]: project.__dict__[i[0]] for i in basic_fields.items()} current_tags = list(project.tags.all()) for i, t in enumerate(current_tags): if i > 2: break data["tag" + str(i + 1)] = t.get_name() form = EditProjectForm(project, data) return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path })
def edit_project(request, project_id): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) project = get_object_or_404(ToolReviewProject, pk=project_id) if request.method == 'POST': form = EditProjectForm(request.POST) changes = set() if form.is_valid(): for k in ["tool_name", "tool_url"]: if not project.__dict__[k.split("_") [1]] == form.cleaned_data[k]: project.__dict__[k.split("_")[1]] = form.cleaned_data[k] changes.add(k.split("_")[1]) for k in ["tool_category", "project_category", "summary"]: if not project.__dict__[k] == form.cleaned_data[k]: project.__dict__[k] = form.cleaned_data[k] changes.add(k) # currently we don't support updating screenshot, too much work if "youtube_video_id" in form.cleaned_data and \ ( project.youtube_video_id is None or \ not project.youtube_video_id == form.cleaned_data["youtube_video_id"] ) : project.youtube_video_id = form.cleaned_data[ "youtube_video_id"] changes.add("video") elif not project.youtube_video_id is None and not "youtube_video_id" in form.cleaned_data: del project.youtube_video_id changes.add("video") project.save() current_tags = set(project.tags.all()) new_tags = set() t1 = cf.get_best_final_matching_tag(form.cleaned_data["tag1"]) if not t1 is None: new_tags.add(t1) t2 = cf.get_best_final_matching_tag(form.cleaned_data["tag2"]) if not t2 is None: new_tags.add(t2) t3 = cf.get_best_final_matching_tag(form.cleaned_data["tag3"]) if not t3 is None: new_tags.add(t3) if len(new_tags.symmetric_difference(current_tags)) > 0: project.tags.clear() project.tags.add(*new_tags) changes.add("tags") if "name" in changes or "tags" in changes: ct.finalize_project(project) return render( request, 'core/thanks.html', { "action_description": "editing your tool review", "link": "/apps/tool_review/administer_project/" + str(project.id) }) else: return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path }) else: data = { "tool_name": project.name, "tool_url": project.url, "summary": project.summary, "tool_category": project.tool_category, "project_category": project.project_category } if not project.youtube_video_id is None: data["youtube_video_id"] = project.youtube_video_id current_tags = list(project.tags.all()) for i, t in enumerate(current_tags): if i > 2: break data["tag" + str(i + 1)] = t.get_name() form = EditProjectForm(data) return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path })
def participate(request, item_id): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) item = BallotDeciderItem.objects.get(pk=item_id) context = cv.get_default_og_metadata(request, item) project = item.participation_project.ballotdeciderproject context.update({ "ballot": project, "basics": [ cv.get_item_details(i, False) for i in project.basics.all() if i.is_active ], "effects": [ cv.get_item_details(i, False) for i in project.effects.all() if i.is_active ], 'site': os.environ["SITE"], "item": item }) if not request.method == "POST": return render(request, 'ballot_decider/participate.html', context) if request.method == 'POST' and request.is_ajax(): submission_data = json.loads(request.body.strip()) form = ParticipateForm(item, submission_data) if form.is_valid(): submission = POVToolResponse() submission.user_profile = profile submission.participation_item = item submission.save() for k in form.cleaned_data.keys(): if "pov_weight" in k: pov_id = int(k.replace("pov_weight_", "")) pov = PointOfView.objects.get(id=pov_id) item_response = POVItemResponse() item_response.point_of_view = pov item_response.score = int(form.cleaned_data[k]) item_response.tool_response = submission item_response.save() decision, explanation = submission.generate_decision() submission.final_decision = decision submission.save() content = dict() content["reveal"] = ["response"] content["hide"] = ["ajax_form"] content["explanation"] = explanation if decision == 0: content["reveal"].append("no-decision") elif decision >= .15: content["reveal"].append("strong-yes") elif decision > 0: content["reveal"].append("lean-yes") elif decision <= -.15: content["reveal"].append("strong-no") elif decision < 0: content["reveal"].append("lean-no") return JsonResponse(content) else: return HttpResponse("sorry, the form isn't valid")
def edit_project(request, project_id): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) project = get_object_or_404(CityBudgetingProject, pk=project_id) simple_fields = [ "fiscal_period_start", "fiscal_period_end", "budget_url", "name", "budget_description", "revenues_description", "funds_description", "expenses_description" ] if request.method == 'POST': form = EditProjectForm(request.POST) changes = set() if form.is_valid(): for k in simple_fields: if not project.__dict__[k] == form.cleaned_data[k]: project.__dict__[k] = form.cleaned_data[k] changes.add(k) form_city = cf.get_best_final_matching_tag( form.cleaned_data["city"]).geotag if not project.city == form_city: project.city = form_city changes.add("city") if "budget_excel_file" in form.cleaned_data and not form.cleaned_data[ "budget_excel_file"] is None and not form.cleaned_data[ "budget_excel_file"] == "": changes.add("data") excel_file_url = form.cleaned_data["budget_excel_file"] path_with_bucket_and_leading_slash = urlsplit( excel_file_url)[2] path_without_bucket = "/".join( path_with_bucket_and_leading_slash.split("/")[2:]) project.budget_excel_file = path_without_bucket project.save() if "name" in changes or "data" in changes: ct.finalize_project(project) return render( request, 'core/thanks.html', { "action_description": "editing your budget transparency project", "link": "/apps/city_budgeting/administer_project/" + str(project.id) }) else: return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path }) else: data = {k: project.__dict__[k] for k in simple_fields} data["city"] = project.city.get_name() initial = { "download_link_1": default_storage.url("city_budgeting/misc/example.xlsx"), "download_link_2": default_storage.url(project.budget_excel_file) } form = EditProjectForm(data, initial=initial) return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path })
def new_project(request, group=None): (profile, permissions, is_default) = cv.get_profile_and_permissions(request) if request.method == 'POST': form = CreateProjectForm(request.POST) if form.is_valid(): project = BallotDeciderProject() project.name = form.cleaned_data["measure_name"] project.owner_profile = profile project.ballot_text = form.cleaned_data["ballot_text"] project.election_date = form.cleaned_data["election_date"] project.election_website = form.cleaned_data["election_website"] if "basics_notes" in form.cleaned_data and not form.cleaned_data[ "basics_notes"] == "": project.basics_notes = form.cleaned_data["basics_notes"] if "effects_notes" in form.cleaned_data and not form.cleaned_data[ "effects_notes"] == "": project.effects_notes = form.cleaned_data["effects_notes"] project.group = group project.save() for i in range(1, 4): data = form.cleaned_data.get("basics" + str(i), None) if not data is None and not data == "": item = cm.ParticipationItem.objects.get(id=data, is_active=True) project.basics.add(item) for i in range(1, 4): data = form.cleaned_data.get("effects" + str(i), None) if not data is None and not data == "": item = cm.ParticipationItem.objects.get(id=data, is_active=True) project.effects.add(item) for i in range(1, 4): quote = form.cleaned_data.get("pov_quote_" + str(i), None) if not quote is None and not quote == "": pov = PointOfView() pov.quote = quote pov.is_favorable = form.cleaned_data["pov_is_favorable_" + str(i)] pov.save() project.points_of_view.add(pov) # iterate through form adding tags for key, val in form.cleaned_data.items(): if key.startswith("tag"): t = cf.get_best_final_matching_tag(val) if not t is None: project.tags.add(t) ct.finalize_project(project) return render( request, 'core/thanks.html', { "action_description": "creating a new ballot decider project", "link": "/apps/ballot_decider/administer_project/" + str(project.id) }) else: return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path }) else: form = CreateProjectForm() return render(request, 'core/generic_form.html', { 'form': form, 'action_path': request.path })