def form_photos(request, username, id_string): xform, owner = check_and_set_user_and_form(username, id_string, request) if not xform: return HttpResponseForbidden(_(u'Not shared.')) context = RequestContext(request) context.form_view = True context.content_user = owner context.xform = xform image_urls = [] for instance in xform.instances.all(): for attachment in instance.attachments.all(): # skip if not image e.g video or file if not attachment.mimetype.startswith('image'): continue data = {} for i in ['small', 'medium', 'large', 'original']: url = reverse(attachment_url, kwargs={'size': i}) url = '%s?media_file=%s' % (url, attachment.media_file.name) data[i] = url image_urls.append(data) context.images = image_urls context.profile, created = UserProfile.objects.get_or_create(user=owner) return render_to_response('form_photos.html', context_instance=context)
def delete_data(request, username=None, id_string=None): xform, owner = check_and_set_user_and_form(username, id_string, request) response_text = u'' if not xform: return HttpResponseForbidden(_(u'Not shared.')) data_id = request.POST.get('id') if not data_id: return HttpResponseBadRequest(_(u"id must be specified")) Instance.set_deleted_at(data_id) audit = { 'xform': xform.id_string } audit_log( Actions.SUBMISSION_DELETED, request.user, owner, _("Deleted submission with id '%(record_id)s' " "on '%(id_string)s'.") % { 'id_string': xform.id_string, 'record_id': data_id }, audit, request) response_text = json.dumps({"success": "Deleted data %s" % data_id}) if 'callback' in request.GET and request.GET.get('callback') != '': callback = request.GET.get('callback') response_text = ("%s(%s)" % (callback, response_text)) return HttpResponse(response_text, mimetype='application/json')
def delete_data(request, username=None, id_string=None): xform, owner = check_and_set_user_and_form(username, id_string, request) response_text = u"" if not xform: return HttpResponseForbidden(_(u"Not shared.")) data_id = request.POST.get("id") if not data_id: return HttpResponseBadRequest(_(u"id must be specified")) Instance.set_deleted_at(data_id) audit = {"xform": xform.id_string} audit_log( Actions.SUBMISSION_DELETED, request.user, owner, _("Deleted submission with id '%(record_id)s' " "on '%(id_string)s'.") % {"id_string": xform.id_string, "record_id": data_id}, audit, request, ) response_text = json.dumps({"success": "Deleted data %s" % data_id}) if "callback" in request.GET and request.GET.get("callback") != "": callback = request.GET.get("callback") response_text = "%s(%s)" % (callback, response_text) return HttpResponse(response_text, mimetype="application/json")
def api(request, username=None, id_string=None): """ Returns all results as JSON. If a parameter string is passed, it takes the 'query' parameter, converts this string to a dictionary, an that is then used as a MongoDB query string. NOTE: only a specific set of operators are allow, currently $or and $and. Please send a request if you'd like another operator to be enabled. NOTE: Your query must be valid JSON, double check it here, http://json.parser.online.fr/ E.g. api?query='{"last_name": "Smith"}' """ if request.method == "OPTIONS": response = HttpResponse() add_cors_headers(response) return response helper_auth_helper(request) helper_auth_helper(request) xform, owner = check_and_set_user_and_form(username, id_string, request) if not xform: return HttpResponseForbidden(_(u'Not shared.')) try: args = { 'username': username, 'id_string': id_string, 'query': request.GET.get('query'), 'fields': request.GET.get('fields'), 'sort': request.GET.get('sort') } if 'start' in request.GET: args["start"] = int(request.GET.get('start')) if 'limit' in request.GET: args["limit"] = int(request.GET.get('limit')) if 'count' in request.GET: args["count"] = True if int(request.GET.get('count')) > 0\ else False cursor = ParsedInstance.query_mongo(**args) except ValueError as e: return HttpResponseBadRequest(e.__str__()) records = list(record for record in cursor) response_text = json_util.dumps(records) if 'callback' in request.GET and request.GET.get('callback') != '': callback = request.GET.get('callback') response_text = ("%s(%s)" % (callback, response_text)) response = HttpResponse(response_text, mimetype='application/json') add_cors_headers(response) return response
def form_photos(request, username, id_string): GALLERY_IMAGE_COUNT_LIMIT = 2500 GALLERY_THUMBNAIL_CHUNK_SIZE = 25 GALLERY_THUMBNAIL_CHUNK_DELAY = 5000 # ms xform, owner = check_and_set_user_and_form(username, id_string, request) if not xform: return HttpResponseForbidden(t('Not shared.')) data = {} data['form_view'] = True data['content_user'] = owner data['xform'] = xform image_urls = [] too_many_images = False # Show the most recent images first for instance in xform.instances.all().order_by('-pk'): attachments = instance.attachments.all() # If we have to truncate, don't include a partial instance if len(image_urls) + attachments.count() > GALLERY_IMAGE_COUNT_LIMIT: too_many_images = True break for attachment in attachments: # skip if not image e.g video or file if not attachment.mimetype.startswith('image'): continue data = {} data['original'] = attachment.secure_url() for suffix in settings.THUMB_CONF.keys(): data[suffix] = attachment.secure_url(suffix) image_urls.append(data) data['images'] = image_urls data['too_many_images'] = too_many_images data['thumbnail_chunk_size'] = GALLERY_THUMBNAIL_CHUNK_SIZE data['thumbnail_chunk_delay'] = GALLERY_THUMBNAIL_CHUNK_DELAY data['profilei'], created = UserProfile.objects.get_or_create(user=owner) return render(request, 'form_photos.html', data)
def api(request, username=None, id_string=None): """ Returns all results as JSON. If a parameter string is passed, it takes the 'query' parameter, converts this string to a dictionary, an that is then used as a MongoDB query string. NOTE: only a specific set of operators are allow, currently $or and $and. Please send a request if you'd like another operator to be enabled. NOTE: Your query must be valid JSON, double check it here, http://json.parser.online.fr/ E.g. api?query='{"last_name": "Smith"}' """ if request.method == "OPTIONS": response = HttpResponse() add_cors_headers(response) return response helper_auth_helper(request) helper_auth_helper(request) xform, owner = check_and_set_user_and_form(username, id_string, request) if not xform: return HttpResponseForbidden(_(u"Not shared.")) try: args = { "username": username, "id_string": id_string, "query": request.GET.get("query"), "fields": request.GET.get("fields"), "sort": request.GET.get("sort"), } if "start" in request.GET: args["start"] = int(request.GET.get("start")) if "limit" in request.GET: args["limit"] = int(request.GET.get("limit")) if "count" in request.GET: args["count"] = True if int(request.GET.get("count")) > 0 else False cursor = ParsedInstance.query_mongo(**args) except ValueError, e: return HttpResponseBadRequest(e.__str__())