def show(request, username=None, id_string=None, uuid=None): if uuid: xform = get_object_or_404(XForm, uuid=uuid) request.session["public_link"] = xform.uuid if MetaData.public_link(xform) else False return HttpResponseRedirect( reverse(show, kwargs={"username": xform.user.username, "id_string": xform.id_string}) ) xform, is_owner, can_edit, can_view = get_xform_and_perms(username, id_string, request) # no access if not (xform.shared or can_view or request.session.get("public_link")): return HttpResponseRedirect(reverse(home)) context = RequestContext(request) context.cloned = ( len(XForm.objects.filter(user__username=request.user.username, id_string=id_string + XForm.CLONED_SUFFIX)) > 0 ) context.public_link = MetaData.public_link(xform) context.is_owner = is_owner context.can_edit = can_edit context.can_view = can_view or request.session.get("public_link") context.xform = xform context.content_user = xform.user context.base_url = "https://%s" % request.get_host() context.source = MetaData.source(xform) context.form_license = MetaData.form_license(xform).data_value context.data_license = MetaData.data_license(xform).data_value context.supporting_docs = MetaData.supporting_docs(xform) context.media_upload = MetaData.media_upload(xform) context.mapbox_layer = MetaData.mapbox_layer_upload(xform) if is_owner: context.sms_support_form = ActivateSMSSupportFom( initial={"enable_sms_support": xform.allows_sms, "sms_id_string": xform.sms_id_string} ) if not xform.allows_sms: context.sms_compatible = check_form_sms_compatibility(None, json_survey=json.loads(xform.json)) else: url_root = request.build_absolute_uri("/")[:-1] context.sms_providers_doc = providers_doc(url_root=url_root, username=username, id_string=id_string) context.url_root = url_root context.form_license_form = FormLicenseForm(initial={"value": context.form_license}) context.data_license_form = DataLicenseForm(initial={"value": context.data_license}) context.doc_form = SupportDocForm() context.source_form = SourceForm() context.media_form = MediaForm() context.mapbox_layer_form = MapboxLayerForm() users_with_perms = [] for perm in get_users_with_perms(xform, attach_perms=True).items(): has_perm = [] if "change_xform" in perm[1]: has_perm.append(_(u"Can Edit")) if "view_xform" in perm[1]: has_perm.append(_(u"Can View")) users_with_perms.append((perm[0], u" | ".join(has_perm))) context.users_with_perms = users_with_perms context.permission_form = PermissionForm(username) if xform.allows_sms: context.sms_support_doc = get_autodoc_for(xform) return render_to_response("show.html", context_instance=context)
def set_xform_owner_context(context, xform, request, username, id_string): context.sms_support_form = ActivateSMSSupportFom( initial={'enable_sms_support': xform.allows_sms, 'sms_id_string': xform.sms_id_string}) if not xform.allows_sms: context.sms_compatible = check_form_sms_compatibility( None, json_survey=json.loads(xform.json)) else: url_root = request.build_absolute_uri('/')[:-1] context.sms_providers_doc = providers_doc( url_root=url_root, username=username, id_string=id_string) context.url_root = url_root context.form_license_form = FormLicenseForm( initial={'value': context.form_license}) context.data_license_form = DataLicenseForm( initial={'value': context.data_license}) context.doc_form = SupportDocForm() context.source_form = SourceForm() context.media_form = MediaForm() context.mapbox_layer_form = MapboxLayerForm() users_with_perms = [] for perm in get_users_with_perms(xform, attach_perms=True).items(): has_perm = [] if 'change_xform' in perm[1]: has_perm.append(_(u"Can Edit")) if 'view_xform' in perm[1]: has_perm.append(_(u"Can View")) if 'report_xform' in perm[1]: has_perm.append(_(u"Can submit to")) users_with_perms.append((perm[0], u" | ".join(has_perm))) context.users_with_perms = users_with_perms context.permission_form = PermissionForm(username)
def clone_xlsform(request, username): """ Copy a public/Shared form to a users list of forms. Eliminates the need to download Excel File and upload again. """ to_username = request.user.username context = RequestContext(request) context.message = {'type': None, 'text': '....'} def set_form(): form_owner = request.POST.get('username') id_string = request.POST.get('id_string') xform = XForm.objects.get(user__username=form_owner, id_string=id_string) if len(id_string) > 0 and id_string[0].isdigit(): id_string = '_' + id_string path = xform.xls.name if default_storage.exists(path): xls_file = upload_to(None, '%s%s.xls' % ( id_string, XForm.CLONED_SUFFIX), to_username) xls_data = default_storage.open(path) xls_file = default_storage.save(xls_file, xls_data) context.message = u'%s-%s' % (form_owner, xls_file) survey = DataDictionary.objects.create( user=request.user, xls=xls_file ).survey # log to cloner's account audit = {} audit_log( Actions.FORM_CLONED, request.user, request.user, _("Cloned form '%(id_string)s'.") % { 'id_string': survey.id_string, }, audit, request) clone_form_url = reverse( show, kwargs={ 'username': to_username, 'id_string': xform.id_string + XForm.CLONED_SUFFIX}) return { 'type': 'alert-success', 'text': _(u'Successfully cloned to %(form_url)s into your ' u'%(profile_url)s') % {'form_url': u'<a href="%(url)s">%(id_string)s</a> ' % { 'id_string': survey.id_string, 'url': clone_form_url }, 'profile_url': u'<a href="%s">profile</a>.' % reverse(profile, kwargs={'username': to_username})} } form_result = publish_form(set_form) if form_result['type'] == 'alert-success': # comment the following condition (and else) # when we want to enable sms check for all. # until then, it checks if form barely related to sms if is_sms_related(form_result.get('form_o')): form_result_sms = check_form_sms_compatibility(form_result) context.message_list = [form_result, form_result_sms] else: context.message = form_result else: context.message = form_result if request.is_ajax(): res = loader.render_to_string( 'message.html', context_instance=context).replace("'", r"\'").replace('\n', '') return HttpResponse( "$('#mfeedback').html('%s').show();" % res) else: return HttpResponse(context.message['text'])
def edit(request, username, id_string): xform = XForm.objects.get(user__username=username, id_string=id_string) owner = xform.user if username == request.user.username or\ request.user.has_perm('logger.change_xform', xform): if request.POST.get('description'): audit = { 'xform': xform.id_string } audit_log( Actions.FORM_UPDATED, request.user, owner, _("Description for '%(id_string)s' updated from " "'%(old_description)s' to '%(new_description)s'.") % { 'id_string': xform.id_string, 'old_description': xform.description, 'new_description': request.POST['description'] }, audit, request) xform.description = request.POST['description'] elif request.POST.get('title'): audit = { 'xform': xform.id_string } audit_log( Actions.FORM_UPDATED, request.user, owner, _("Title for '%(id_string)s' updated from " "'%(old_title)s' to '%(new_title)s'.") % { 'id_string': xform.id_string, 'old_title': xform.title, 'new_title': request.POST.get('title') }, audit, request) xform.title = request.POST['title'] elif request.POST.get('toggle_shared'): if request.POST['toggle_shared'] == 'data': audit = { 'xform': xform.id_string } audit_log( Actions.FORM_UPDATED, request.user, owner, _("Data sharing updated for '%(id_string)s' from " "'%(old_shared)s' to '%(new_shared)s'.") % { 'id_string': xform.id_string, 'old_shared': _("shared") if xform.shared_data else _("not shared"), 'new_shared': _("shared") if not xform.shared_data else _("not shared") }, audit, request) xform.shared_data = not xform.shared_data elif request.POST['toggle_shared'] == 'form': audit = { 'xform': xform.id_string } audit_log( Actions.FORM_UPDATED, request.user, owner, _("Form sharing for '%(id_string)s' updated " "from '%(old_shared)s' to '%(new_shared)s'.") % { 'id_string': xform.id_string, 'old_shared': _("shared") if xform.shared else _("not shared"), 'new_shared': _("shared") if not xform.shared else _("not shared") }, audit, request) xform.shared = not xform.shared elif request.POST['toggle_shared'] == 'active': audit = { 'xform': xform.id_string } audit_log( Actions.FORM_UPDATED, request.user, owner, _("Active status for '%(id_string)s' updated from " "'%(old_shared)s' to '%(new_shared)s'.") % { 'id_string': xform.id_string, 'old_shared': _("shared") if xform.downloadable else _("not shared"), 'new_shared': _("shared") if not xform.downloadable else _("not shared") }, audit, request) xform.downloadable = not xform.downloadable elif request.POST.get('form-license'): audit = { 'xform': xform.id_string } audit_log( Actions.FORM_UPDATED, request.user, owner, _("Form License for '%(id_string)s' updated to " "'%(form_license)s'.") % { 'id_string': xform.id_string, 'form_license': request.POST['form-license'], }, audit, request) MetaData.form_license(xform, request.POST['form-license']) elif request.POST.get('data-license'): audit = { 'xform': xform.id_string } audit_log( Actions.FORM_UPDATED, request.user, owner, _("Data license for '%(id_string)s' updated to " "'%(data_license)s'.") % { 'id_string': xform.id_string, 'data_license': request.POST['data-license'], }, audit, request) MetaData.data_license(xform, request.POST['data-license']) elif request.POST.get('source') or request.FILES.get('source'): audit = { 'xform': xform.id_string } audit_log( Actions.FORM_UPDATED, request.user, owner, _("Source for '%(id_string)s' updated to '%(source)s'.") % { 'id_string': xform.id_string, 'source': request.POST.get('source'), }, audit, request) MetaData.source(xform, request.POST.get('source'), request.FILES.get('source')) elif request.POST.get('enable_sms_support_trigger') is not None: sms_support_form = ActivateSMSSupportFom(request.POST) if sms_support_form.is_valid(): audit = { 'xform': xform.id_string } enabled = \ sms_support_form.cleaned_data.get('enable_sms_support') if enabled: audit_action = Actions.SMS_SUPPORT_ACTIVATED audit_message = _(u"SMS Support Activated on") else: audit_action = Actions.SMS_SUPPORT_DEACTIVATED audit_message = _(u"SMS Support Deactivated on") audit_log( audit_action, request.user, owner, audit_message % {'id_string': xform.id_string}, audit, request) # stored previous states to be able to rollback form status # in case we can't save. pe = xform.allows_sms pid = xform.sms_id_string xform.allows_sms = enabled xform.sms_id_string = \ sms_support_form.cleaned_data.get('sms_id_string') compat = check_form_sms_compatibility(None, json.loads(xform.json)) if compat['type'] == 'alert-error': xform.allows_sms = False xform.sms_id_string = pid try: xform.save() except IntegrityError: # unfortunately, there's no feedback mechanism here xform.allows_sms = pe xform.sms_id_string = pid elif request.POST.get('media_url'): uri = request.POST.get('media_url') MetaData.media_add_uri(xform, uri) elif request.FILES.get('media'): audit = { 'xform': xform.id_string } audit_log( Actions.FORM_UPDATED, request.user, owner, _("Media added to '%(id_string)s'.") % { 'id_string': xform.id_string }, audit, request) for aFile in request.FILES.getlist("media"): MetaData.media_upload(xform, aFile) elif request.POST.get('map_name'): mapbox_layer = MapboxLayerForm(request.POST) if mapbox_layer.is_valid(): audit = { 'xform': xform.id_string } audit_log( Actions.FORM_UPDATED, request.user, owner, _("Map layer added to '%(id_string)s'.") % { 'id_string': xform.id_string }, audit, request) MetaData.mapbox_layer_upload(xform, mapbox_layer.cleaned_data) elif request.FILES: audit = { 'xform': xform.id_string } audit_log( Actions.FORM_UPDATED, request.user, owner, _("Supporting document added to '%(id_string)s'.") % { 'id_string': xform.id_string }, audit, request) MetaData.supporting_docs(xform, request.FILES['doc']) xform.update() if request.is_ajax(): return HttpResponse(_(u'Updated succeeded.')) else: return HttpResponseRedirect(reverse(show, kwargs={ 'username': username, 'id_string': id_string })) return HttpResponseForbidden(_(u'Update failed.'))
def profile(request, username): context = RequestContext(request) content_user = get_object_or_404(User, username=username) context.form = QuickConverter() # xlsform submission... if request.method == 'POST' and request.user.is_authenticated(): def set_form(): form = QuickConverter(request.POST, request.FILES) survey = form.publish(request.user).survey audit = {} audit_log( Actions.FORM_PUBLISHED, request.user, content_user, _("Published form '%(id_string)s'.") % { 'id_string': survey.id_string, }, audit, request) enketo_webform_url = reverse( enter_data, kwargs={'username': username, 'id_string': survey.id_string} ) return { 'type': 'alert-success', 'preview_url': reverse(enketo_preview, kwargs={ 'username': username, 'id_string': survey.id_string }), 'text': _(u'Successfully published %(form_id)s.' u' <a href="%(form_url)s">Enter Web Form</a>' u' or <a href="#preview-modal" data-toggle="modal">' u'Preview Web Form</a>') % {'form_id': survey.id_string, 'form_url': enketo_webform_url}, 'form_o': survey } form_result = publish_form(set_form) if form_result['type'] == 'alert-success': # comment the following condition (and else) # when we want to enable sms check for all. # until then, it checks if form barely related to sms if is_sms_related(form_result.get('form_o')): form_result_sms = check_form_sms_compatibility(form_result) context.message_list = [form_result, form_result_sms] else: context.message = form_result else: context.message = form_result # profile view... # for the same user -> dashboard if content_user == request.user: context.show_dashboard = True context.all_forms = content_user.xforms.count() context.form = QuickConverterFile() context.form_url = QuickConverterURL() request_url = request.build_absolute_uri( "/%s" % request.user.username) context.url = request_url.replace('http://', 'https://') xforms = XForm.objects.filter(user=content_user)\ .select_related('user', 'instances') context.user_xforms = xforms # forms shared with user xfct = ContentType.objects.get(app_label='logger', model='xform') xfs = content_user.userobjectpermission_set.filter(content_type=xfct) shared_forms_pks = list(set([xf.object_pk for xf in xfs])) context.forms_shared_with = XForm.objects.filter( pk__in=shared_forms_pks).exclude(user=content_user)\ .select_related('user') context.xforms_list = [ { 'id': 'published', 'xforms': context.user_xforms, 'title': _(u"Published Forms"), 'small': _("Export, map, and view submissions.") }, { 'id': 'shared', 'xforms': context.forms_shared_with, 'title': _(u"Shared Forms"), 'small': _("List of forms shared with you.") } ] # for any other user -> profile set_profile_data(context, content_user) return render_to_response("profile.html", context_instance=context)
def edit(request, username, id_string): xform = XForm.objects.get(user__username=username, id_string=id_string) owner = xform.user if request.GET.get("crowdform"): crowdform_action = request.GET["crowdform"] request_username = request.user.username # ensure is crowdform if xform.is_crowd_form: if crowdform_action == "delete": MetaData.objects.get( xform__id_string=id_string, data_value=request_username, data_type=MetaData.CROWDFORM_USERS ).delete() elif crowdform_action == "add": MetaData.crowdform_users(xform, request_username) return HttpResponseRedirect(reverse(profile, kwargs={"username": request_username})) if username == request.user.username or request.user.has_perm("logger.change_xform", xform): if request.POST.get("description"): audit = {"xform": xform.id_string} audit_log( Actions.FORM_UPDATED, request.user, owner, _("Description for '%(id_string)s' updated from " "'%(old_description)s' to '%(new_description)s'.") % { "id_string": xform.id_string, "old_description": xform.description, "new_description": request.POST["description"], }, audit, request, ) xform.description = request.POST["description"] elif request.POST.get("title"): audit = {"xform": xform.id_string} audit_log( Actions.FORM_UPDATED, request.user, owner, _("Title for '%(id_string)s' updated from " "'%(old_title)s' to '%(new_title)s'.") % {"id_string": xform.id_string, "old_title": xform.title, "new_title": request.POST.get("title")}, audit, request, ) xform.title = request.POST["title"] elif request.POST.get("toggle_shared"): if request.POST["toggle_shared"] == "data": audit = {"xform": xform.id_string} audit_log( Actions.FORM_UPDATED, request.user, owner, _("Data sharing updated for '%(id_string)s' from " "'%(old_shared)s' to '%(new_shared)s'.") % { "id_string": xform.id_string, "old_shared": _("shared") if xform.shared_data else _("not shared"), "new_shared": _("shared") if not xform.shared_data else _("not shared"), }, audit, request, ) xform.shared_data = not xform.shared_data elif request.POST["toggle_shared"] == "form": audit = {"xform": xform.id_string} audit_log( Actions.FORM_UPDATED, request.user, owner, _("Form sharing for '%(id_string)s' updated " "from '%(old_shared)s' to '%(new_shared)s'.") % { "id_string": xform.id_string, "old_shared": _("shared") if xform.shared else _("not shared"), "new_shared": _("shared") if not xform.shared else _("not shared"), }, audit, request, ) xform.shared = not xform.shared elif request.POST["toggle_shared"] == "active": audit = {"xform": xform.id_string} audit_log( Actions.FORM_UPDATED, request.user, owner, _("Active status for '%(id_string)s' updated from " "'%(old_shared)s' to '%(new_shared)s'.") % { "id_string": xform.id_string, "old_shared": _("shared") if xform.downloadable else _("not shared"), "new_shared": _("shared") if not xform.downloadable else _("not shared"), }, audit, request, ) xform.downloadable = not xform.downloadable elif request.POST["toggle_shared"] == "crowd": audit = {"xform": xform.id_string} audit_log( Actions.FORM_UPDATED, request.user, owner, _("Crowdform status for '%(id_string)s' updated from " "'%(old_status)s' to '%(new_status)s'.") % { "id_string": xform.id_string, "old_status": _("crowdform") if not xform.is_crowd_form else _("not crowdform"), "new_status": _("crowdform") if xform.is_crowd_form else _("not crowdform"), }, audit, request, ) if xform.is_crowd_form: xform.is_crowd_form = False else: xform.is_crowd_form = True xform.shared = True xform.shared_data = True elif request.POST.get("form-license"): audit = {"xform": xform.id_string} audit_log( Actions.FORM_UPDATED, request.user, owner, _("Form License for '%(id_string)s' updated to " "'%(form_license)s'.") % {"id_string": xform.id_string, "form_license": request.POST["form-license"]}, audit, request, ) MetaData.form_license(xform, request.POST["form-license"]) elif request.POST.get("data-license"): audit = {"xform": xform.id_string} audit_log( Actions.FORM_UPDATED, request.user, owner, _("Data license for '%(id_string)s' updated to " "'%(data_license)s'.") % {"id_string": xform.id_string, "data_license": request.POST["data-license"]}, audit, request, ) MetaData.data_license(xform, request.POST["data-license"]) elif request.POST.get("source") or request.FILES.get("source"): audit = {"xform": xform.id_string} audit_log( Actions.FORM_UPDATED, request.user, owner, _("Source for '%(id_string)s' updated to '%(source)s'.") % {"id_string": xform.id_string, "source": request.POST.get("source")}, audit, request, ) MetaData.source(xform, request.POST.get("source"), request.FILES.get("source")) elif request.POST.get("enable_sms_support_trigger") is not None: sms_support_form = ActivateSMSSupportFom(request.POST) if sms_support_form.is_valid(): audit = {"xform": xform.id_string} enabled = sms_support_form.cleaned_data.get("enable_sms_support") if enabled: audit_action = Actions.SMS_SUPPORT_ACTIVATED audit_message = _(u"SMS Support Activated on") else: audit_action = Actions.SMS_SUPPORT_DEACTIVATED audit_message = _(u"SMS Support Deactivated on") audit_log( audit_action, request.user, owner, audit_message % {"id_string": xform.id_string}, audit, request ) # stored previous states to be able to rollback form status # in case we can't save. pe = xform.allows_sms pid = xform.sms_id_string xform.allows_sms = enabled xform.sms_id_string = sms_support_form.cleaned_data.get("sms_id_string") compat = check_form_sms_compatibility(None, json.loads(xform.json)) if compat["type"] == "alert-error": xform.allows_sms = False xform.sms_id_string = pid try: xform.save() except IntegrityError: # unfortunately, there's no feedback mechanism here xform.allows_sms = pe xform.sms_id_string = pid elif request.FILES.get("media"): audit = {"xform": xform.id_string} audit_log( Actions.FORM_UPDATED, request.user, owner, _("Media added to '%(id_string)s'.") % {"id_string": xform.id_string}, audit, request, ) for aFile in request.FILES.getlist("media"): MetaData.media_upload(xform, aFile) elif request.POST.get("map_name"): mapbox_layer = MapboxLayerForm(request.POST) if mapbox_layer.is_valid(): audit = {"xform": xform.id_string} audit_log( Actions.FORM_UPDATED, request.user, owner, _("Map layer added to '%(id_string)s'.") % {"id_string": xform.id_string}, audit, request, ) MetaData.mapbox_layer_upload(xform, mapbox_layer.cleaned_data) elif request.FILES: audit = {"xform": xform.id_string} audit_log( Actions.FORM_UPDATED, request.user, owner, _("Supporting document added to '%(id_string)s'.") % {"id_string": xform.id_string}, audit, request, ) MetaData.supporting_docs(xform, request.FILES["doc"]) xform.update() if request.is_ajax(): return HttpResponse(_(u"Updated succeeded.")) else: return HttpResponseRedirect(reverse(show, kwargs={"username": username, "id_string": id_string})) return HttpResponseForbidden(_(u"Update failed."))
def profile(request, username): context = RequestContext(request) content_user = get_object_or_404(User, username=username) context.form = QuickConverter() # xlsform submission... if request.method == "POST" and request.user.is_authenticated(): def set_form(): form = QuickConverter(request.POST, request.FILES) survey = form.publish(request.user).survey audit = {} audit_log( Actions.FORM_PUBLISHED, request.user, content_user, _("Published form '%(id_string)s'.") % {"id_string": survey.id_string}, audit, request, ) enketo_webform_url = reverse(enter_data, kwargs={"username": username, "id_string": survey.id_string}) return { "type": "alert-success", "preview_url": reverse(enketo_preview, kwargs={"username": username, "id_string": survey.id_string}), "text": _( u"Successfully published %(form_id)s." u' <a href="%(form_url)s">Enter Web Form</a>' u' or <a href="#preview-modal" data-toggle="modal">' u"Preview Web Form</a>" ) % {"form_id": survey.id_string, "form_url": enketo_webform_url}, "form_o": survey, } form_result = publish_form(set_form) if form_result["type"] == "alert-success": # comment the following condition (and else) # when we want to enable sms check for all. # until then, it checks if form barely related to sms if is_sms_related(form_result.get("form_o")): form_result_sms = check_form_sms_compatibility(form_result) context.message_list = [form_result, form_result_sms] else: context.message = form_result else: context.message = form_result # profile view... # for the same user -> dashboard if content_user == request.user: context.show_dashboard = True context.all_forms = content_user.xforms.count() context.form = QuickConverterFile() context.form_url = QuickConverterURL() context.url = request.build_absolute_uri("/%s" % request.user.username) xforms = XForm.objects.filter(user=content_user).select_related("user", "instances") context.user_xforms = xforms # forms shared with user xfct = ContentType.objects.get(app_label="logger", model="xform") xfs = content_user.userobjectpermission_set.filter(content_type=xfct) shared_forms_pks = list(set([xf.object_pk for xf in xfs])) context.forms_shared_with = ( XForm.objects.filter(pk__in=shared_forms_pks).exclude(user=content_user).select_related("user") ) context.xforms_list = [ { "id": "published", "xforms": context.user_xforms, "title": _(u"Published Forms"), "small": _("Export, map, and view submissions."), }, { "id": "shared", "xforms": context.forms_shared_with, "title": _(u"Shared Forms"), "small": _("List of forms shared with you."), }, ] # for any other user -> profile set_profile_data(context, content_user) return render_to_response("profile.html", context_instance=context)