Exemplo n.º 1
0

@login_and_domain_required
def form_context(request, domain, app_id, module_id, form_id):
    app = Application.get(app_id)
    form_url = "%s%s" % (get_url_base(), reverse('download_xform', args=[domain, app_id, module_id, form_id]))
    case_id = request.GET.get('case_id')
    delegation = request.GET.get('task-list') == 'true'
    offline = request.GET.get('offline') == 'true'
    return json_response(
        touchforms_api.get_full_context(domain, request.couch_user, 
                                        app, form_url, case_id,
                                        delegation=delegation, offline=offline))
        

cloudcare_api = login_or_digest_ex(allow_cc_users=True)

@login_and_domain_required
def view_case(request, domain, case_id=None):
    context = {}
    case_json = CommCareCase.get(case_id).get_json() if case_id else None
    case_type = case_json['properties']['case_type'] if case_json else None
    case_spec_id = request.GET.get('spec')
    if case_spec_id:
        case_spec = CaseSpec.get(case_spec_id)
    else:
        case_spec = None
        context.update(dict(
            suggested_case_specs=CaseSpec.get_suggested(domain, case_type)
        ))
    context.update({
Exemplo n.º 2
0
    session_extras = {'session_name': session_name, 'app_id': app._id}
    session_extras.update(
        get_cloudcare_session_data(domain, form, request.couch_user))

    delegation = request.GET.get('task-list') == 'true'
    session_helper = CaseSessionDataHelper(domain,
                                           request.couch_user,
                                           case_id,
                                           app,
                                           form,
                                           delegation=delegation)
    return json_response(
        session_helper.get_full_context(root_context, session_extras))


cloudcare_api = login_or_digest_ex(allow_cc_users=True)


class ReadableQuestions(View):

    urlname = 'readable_questions'

    @csrf_exempt
    @method_decorator(cloudcare_api)
    def dispatch(self, request, *args, **kwargs):
        return super(ReadableQuestions, self).dispatch(request, *args,
                                                       **kwargs)

    def post(self, request, domain):
        instance_xml = request.POST.get('instanceXml').encode('utf-8')
        app_id = request.POST.get('appId')
Exemplo n.º 3
0
class CaseAttachmentAPI(View):
    @method_decorator(login_or_digest_ex(allow_cc_users=True))
    def get(self, *args, **kwargs):
        """
        https://bitbucket.org/commcare/commcare/wiki/CaseAttachmentAPI
        max_size	The largest size (in bytes) for the attachment
        max_image_width	The largest width in pixels for an an image attachment
        max_image_height	The largest width in pixels for an an image attachment
        """

        max_filesize = int(self.request.GET.get('max_size', 0))  #todo

        img = self.request.GET.get('img', None)
        size = self.request.GET.get('size',
                                    OBJECT_ORIGINAL)  #alternative to abs size
        max_width = int(self.request.GET.get('max_image_width', 0))
        max_height = int(self.request.GET.get('max_image_height', 0))

        case_id = kwargs.get('case_id', None)
        if not case_id or not CommCareCase.get_db().doc_exist(case_id):
            raise Http404

        attachment_key = kwargs.get('attachment_id', None)

        if img is not None:
            if size == "debug_all":
                case_doc = CommCareCase.get(case_id)
                r = HttpResponse(content_type="text/html")
                r.write('<html><body>')
                r.write('<ul>')
                for fsize in IMAGE_SIZE_ORDERING:
                    meta, stream = CommCareCase.fetch_case_image(
                        case_id,
                        attachment_key,
                        filesize_limit=max_filesize,
                        width_limit=max_width,
                        height_limit=max_height,
                        fixed_size=fsize)

                    r.write('<li>')
                    r.write('Size: %s<br>' % fsize)
                    r.write("Limit: max_size: %s" % max_filesize)
                    if max_width > 0:
                        r.write(", max_width: %s" % max_width)
                    if max_height > 0:
                        r.write(", max_height: %s" % max_height)
                    r.write("<br>")
                    if meta is not None:
                        r.write('Resolution: %d x %d<br>' %
                                (meta['width'], meta['height']))
                        r.write('Filesize: %d<br>' % (meta['content_length']))
                        r.write(
                            '<img src="%(attach_url)s?img&size=%(size_key)s&max_size=%(max_filesize)s&max_image_width=%(max_width)s&max_image_height=%(max_height)s">'
                            % {
                                "attach_url":
                                reverse("api_case_attachment",
                                        kwargs={
                                            "domain": self.request.domain,
                                            "case_id": case_id,
                                            "attachment_id": attachment_key,
                                        }),
                                "domain":
                                case_doc.domain,
                                "case_id":
                                case_id,
                                "attachment_key":
                                attachment_key,
                                "size_key":
                                fsize,
                                "max_filesize":
                                max_filesize,
                                "max_width":
                                max_width,
                                "max_height":
                                max_height
                            })
                    else:
                        r.write('Not available')
                    r.write('</li>')
                r.write('</ul></body></html>')
                return r
            else:
                # image workflow
                attachment_meta, attachment_stream = CommCareCase.fetch_case_image(
                    case_id,
                    attachment_key,
                    filesize_limit=max_filesize,
                    width_limit=max_width,
                    height_limit=max_height,
                    fixed_size=size)
        else:
            # default stream
            attachment_meta, attachment_stream = CommCareCase.fetch_case_attachment(
                case_id, attachment_key)

        if attachment_meta is not None:
            mime_type = attachment_meta['content_type']
        else:
            mime_type = "plain/text"
        response = StreamingHttpResponse(streaming_content=attachment_stream,
                                         mimetype=mime_type)
        return response