コード例 #1
0
ファイル: req_process.py プロジェクト: hooface/ADL_LRS
def activity_state_get(req_dict):
    # add ETag for concurrency
    state_id = req_dict['params'].get('stateId', None)
    activity_id = req_dict['params']['activityId']
    agent = req_dict['params']['agent']
    a = Agent.objects.retrieve(**agent)
    if not a:
        response = HttpResponseNotFound("No agent found for activity state")
    else:    
        registration = req_dict['params'].get('registration', None)
        actstate = ActivityStateManager(a)
        # state id means we want only 1 item
        if state_id:
            resource = actstate.get_state(activity_id, registration, state_id)
            if resource.state:
                response = HttpResponse(resource.state.read(), content_type=resource.content_type)
            else:
                response = HttpResponse(resource.json_state, content_type=resource.content_type)
            response['ETag'] = '"%s"' % resource.etag
        # no state id means we want an array of state ids
        else:
            since = req_dict['params'].get('since', None)
            resource = actstate.get_state_ids(activity_id, registration, since)
            response = JsonResponse([k for k in resource], safe=False)
    
    # If it's a HEAD request
    if req_dict['method'].lower() != 'get':
        response.body = ''
    return response
コード例 #2
0
ファイル: download.py プロジェクト: ANSTO/mecat-ansto
def download_datafiles(request):

    if (len(request.POST.getlist('datafile')) == 0 \
        and len(request.POST.getlist('dataset')) == 0):

        response = HttpResponseNotFound()
        response.write('<p>No files selected!</p>\n')
        return response

    from string import atoi
    par = models.ExperimentParameter.objects.filter(parameterset__experiment=
                                                    atoi(request.POST.get('expid')))

    epn = par.get(name__name='EPN').string_value

    datafiles = request.POST.getlist('datafile')
    datasets = request.POST.getlist('dataset')

    file_string = ""
    for dsid in datasets:
        if has_dataset_access(request, dsid):
            for datafile in models.Dataset_File.objects.filter(dataset=dsid):
                absolute_filename = datafile.url.partition('://')[2]
                file_string += absolute_filename + "\\r\\nTARDIS\\r\\n"

    for dfid in datafiles:
        if has_datafile_access(request, dfid):
            datafile = models.Dataset_File.objects.get(pk=dfid)
            if not datafile.dataset.id in datasets:
                absolute_filename = datafile.url.partition('://')[2]
                file_string += absolute_filename + "\\r\\nTARDIS\\r\\n"

    download = VBLDownload(request)
    return download.download(epn, file_string)
コード例 #3
0
ファイル: views.py プロジェクト: zdenekmaxa/examples
def get_question(request, question_id=0):
    try:
        q = Question.objects.get(pk=question_id)  # identical to id=1
    except Exception, ex:
        r = HttpResponseNotFound()
        r.write("Error, reason: '%s'" % ex)
        return r
コード例 #4
0
ファイル: req_process.py プロジェクト: hooface/ADL_LRS
def agent_profile_get(req_dict):
    # add ETag for concurrency
    agent = req_dict['params']['agent']
    a = Agent.objects.retrieve(**agent)
    if not a:
        response = HttpResponseNotFound("No agent found for agent profile get")
    else:
        ap = AgentProfileManager(a)

        profile_id = req_dict['params'].get('profileId', None) if 'params' in req_dict else None
        if profile_id:
            resource = ap.get_profile(profile_id)
            if resource.profile:
                response = HttpResponse(resource.profile.read(), content_type=resource.content_type)
            else:
                response = HttpResponse(resource.json_profile, content_type=resource.content_type)
            response['ETag'] = '"%s"' % resource.etag
            return response

        since = req_dict['params'].get('since', None) if 'params' in req_dict else None
        resource = ap.get_profile_ids(since)
        response = JsonResponse([k for k in resource], safe=False)
    
    # If it's a HEAD request
    if req_dict['method'].lower() != 'get':
        response.body = ''
    return response
コード例 #5
0
def NotFound(msg):
    error = {
            'success': False,
            'data': {
                'code': 404,
                'message': msg
            }
        }
    resp = HttpResponseNotFound()
    resp.content = json.dumps(error)
    return resp
コード例 #6
0
ファイル: views.py プロジェクト: KineticHub/django-sendgrid
def handle_single_event_request(request):
    """
	Handles single event POST requests.
	"""
    eventData = request.POST

    # Parameters that are always passed with each event
    email = eventData.get("email", None)
    event = eventData.get("event", None)

    message_id = eventData.get("message_id", None)
    if message_id:
        try:
            emailMessage = EmailMessage.objects.get(message_id=message_id)
        except EmailMessage.DoesNotExist:
            msg = "EmailMessage with message_id {m} does not exist"
            logger.exception(msg.format(m=message_id))

            response = HttpResponseNotFound()
            response.write(msg.format(m=message_id) + "\n")
        else:
            eventObj = Event.objects.create(
                email_message=emailMessage, email=email, type=EventType.objects.get(name=event.upper())
            )

            response = HttpResponse()
    else:
        msg = "Expected 'message_id' was not found in event data"
        logger.exception(msg)

        response = HttpResponseBadRequest()
        response.write(msg + "\n")

    return response
コード例 #7
0
def page_not_found(request, exception):
    exception_repr = exception.__class__.__name__
    # Try to get an "interesting" exception message, if any (and not the ugly
    # Resolver404 dictionary)
    try:
        message = exception.args[0]
    except (AttributeError, IndexError):
        pass
    else:
        if isinstance(message, str) or isinstance(message, Promise):
            exception_repr = str(message)
    context = {
        'request_path': request.path,
        'exception': exception_repr,
    }
    template = get_template('404.html')
    body = template.render(context, request)
    r = HttpResponseNotFound(body)
    r.xframe_options_exempt = True
    return r
コード例 #8
0
ファイル: middleware.py プロジェクト: ecell/ecell3-ide
 def process_request(self, request):
     # Find the admin file and serve it up, if it exists and is readable.
     if not request.path.startswith(self.media_url):
         return None
     relative_url = re.sub(r'/+', '/', request.path)[len(self.media_url):]
     file_path = os.path.join(self.media_dir, relative_url)
     if not os.path.exists(file_path):
         resp = HttpResponseNotFound()
         resp.write("Not Found")
     else:
         try:
             fp = open(file_path, 'rb')
             resp = HttpResponse(
                 content_type = mimetypes.guess_type(file_path)[0])
             resp.write(fp.read())
             fp.close()
         except IOError:
             resp = HttpResponseForbidden()
             resp.write("Forbidden")
     return resp
コード例 #9
0
def emitir_nfe(modeladmin, request, queryset):
    if not request.user.has_perm('vendas.setar_nfe'):
        return HttpResponseNotFound('<h1>Sem permissão</h1>')
    else:
        queryset.update(nfe_emitida=True)
コード例 #10
0
ファイル: views.py プロジェクト: robotechnics/alumni
def image_preview(request, app_label, model, id, size):
    """
        Grab all image link within a peice of content
        and generate thumbnails of largest image
    """
    # get page object; protect size
    try:
        content_type = ContentType.objects.get(app_label=app_label,
                                               model=model)
        instance = content_type.get_object_for_this_type(id=id)
    except:
        return HttpResponseNotFound(_("Image not found."),
                                    content_type="text/plain")

    keys = [
        settings.CACHE_PRE_KEY, IMAGE_PREVIEW_CACHE, model,
        str(instance.id), size
    ]
    key = '.'.join(keys)
    response = cache.get(key)
    original_size = size

    if not response:
        from tendenci.apps.base.utils import parse_image_sources, make_image_object_from_url, image_rescale

        # set sizes
        size_min = (30, 30)
        size_cap = (512, 512)

        size_tuple = size.split('x')
        if len(size_tuple) == 2: size = int(size_tuple[0]), int(size_tuple[1])
        else: size = int(size), int(size)

        if size > size_cap: size = size_cap

        image_urls = []

        image = Pil.new('RGBA', size_min)

        # find biggest image, dimension-wise
        for image_url in image_urls:
            image_candidate = make_image_object_from_url(image_url)
            if image_candidate:
                if image_candidate.size > image.size:
                    image = image_candidate

        if image.size[1] > size_min[1]:
            # rescale, convert-to true-colors; return response

            image = image_rescale(image, size)
            if image.mode != "RGB":
                image = image.convert("RGB")

            response = HttpResponse(content_type='image/jpeg')

            image.save(response, "JPEG", quality=100)

            keys = [
                settings.CACHE_PRE_KEY, IMAGE_PREVIEW_CACHE, model,
                str(instance.id), size
            ]
            key = '.'.join(keys)

            cache.set(key, response)
            return response

        else:  # raise http 404 error (returns page not found)
            return HttpResponseNotFound(_("Image not found."),
                                        content_type="text/plain")
    else:
        return response
コード例 #11
0
def data_export(request, username, id_string, export_type):
    owner = get_object_or_404(User, username=username)
    xform = get_object_or_404(XForm, id_string=id_string, user=owner)
    helper_auth_helper(request)
    if not has_permission(xform, owner, request):
        return HttpResponseForbidden(_(u'Not shared.'))
    query = request.GET.get("query")
    extension = export_type

    # check if we should force xlsx
    force_xlsx = request.GET.get('xls') != 'true'
    if export_type == Export.XLS_EXPORT and force_xlsx:
        extension = 'xlsx'
    elif export_type in [Export.CSV_ZIP_EXPORT, Export.SAV_ZIP_EXPORT]:
        extension = 'zip'

    audit = {"xform": xform.id_string, "export_type": export_type}
    # check if we need to re-generate,
    # we always re-generate if a filter is specified
    if should_create_new_export(xform, export_type) or query or\
            'start' in request.GET or 'end' in request.GET:
        format_date_for_mongo = lambda x, datetime: datetime.strptime(
            x, '%y_%m_%d_%H_%M_%S').strftime('%Y-%m-%dT%H:%M:%S')
        # check for start and end params
        if 'start' in request.GET or 'end' in request.GET:
            if not query:
                query = '{}'
            query = json.loads(query)
            query[SUBMISSION_TIME] = {}
            try:
                if request.GET.get('start'):
                    query[SUBMISSION_TIME]['$gte'] = format_date_for_mongo(
                        request.GET['start'], datetime)
                if request.GET.get('end'):
                    query[SUBMISSION_TIME]['$lte'] = format_date_for_mongo(
                        request.GET['end'], datetime)
            except ValueError:
                return HttpResponseBadRequest(
                    _("Dates must be in the format YY_MM_DD_hh_mm_ss"))
            else:
                query = json.dumps(query)
        try:
            export = generate_export(export_type, extension, username,
                                     id_string, None, query)
            audit_log(
                Actions.EXPORT_CREATED, request.user, owner,
                _("Created %(export_type)s export on '%(id_string)s'.") % {
                    'id_string': xform.id_string,
                    'export_type': export_type.upper()
                }, audit, request)
        except NoRecordsFoundError:
            return HttpResponseNotFound(_("No records found to export"))
    else:
        export = newset_export_for(xform, export_type)

    # log download as well
    audit_log(
        Actions.EXPORT_DOWNLOADED, request.user, owner,
        _("Downloaded %(export_type)s export on '%(id_string)s'.") % {
            'id_string': xform.id_string,
            'export_type': export_type.upper()
        }, audit, request)

    if not export.filename:
        # tends to happen when using newset_export_for.
        return HttpResponseNotFound("File does not exist!")
    # get extension from file_path, exporter could modify to
    # xlsx if it exceeds limits
    path, ext = os.path.splitext(export.filename)
    ext = ext[1:]
    if request.GET.get('raw'):
        id_string = None
    response = response_with_mimetype_and_name(Export.EXPORT_MIMES[ext],
                                               id_string,
                                               extension=ext,
                                               file_path=export.filepath)
    return response
コード例 #12
0
ファイル: views.py プロジェクト: truthakash/tscharts
    def get(self, request, patient_id=None, format=None):

        badRequest = False
        notFound = False
        patient = None
        byClinicId = None
        aClinic = None
        
        if patient_id:
            try:
                patient = Patient.objects.get(id = patient_id)
            except:
                patient = None
        else:
            # look for optional arguments for searching
            byClinicId = request.GET.get("clinic", '')
            if byClinicId != '':
                aClinic = Clinic.objects.get(id=byClinicId)
                if not aClinic:
                    notFound = True

            if not notFound:
                kwargs = {}
                name = request.GET.get('name', '')
                if not name == '':
                    try:
                        patient = Patient.objects.filter(Q(paternal_last__icontains=name) | Q(maternal_last__icontains=name) | Q(first__icontains=name) | Q(middle__icontains=name))
                    except:
                        patient = None
                else:
                    paternal_last = request.GET.get('paternal_last', '')
                    if not paternal_last == '':
                        kwargs["paternal_last__icontains"] = paternal_last
                    maternal_last = request.GET.get('maternal_last', '')
                    if not maternal_last == '':
                        kwargs["maternal_last__icontains"] = maternal_last
                    first = request.GET.get('first', '')
                    if not first == '':
                        kwargs["first__icontains"] = first
                    dob = request.GET.get('dob', '')
                    if not dob == '':
                        x = dob.split("/")
                        if len(x) == 3:
                            try:
                                kwargs["dob"] = datetime.strptime(dob, "%m/%d/%Y")
                            except:
                                badRequest = True
                        else:
                            badRequest = True

                    curp = request.GET.get('curp', '')
                    if not curp == '':
                        kwargs["curp"] = curp
                    
                    oldid = request.GET.get('oldid', '')
                    if not oldid == '':
                        try:
                            kwargs["oldid"] = int(oldid)
                        except:
                            badRequest = True
                    
                    gender = request.GET.get('gender', '')
                    if not gender == '':
                        if gender == "Male":
                            kwargs["gender"] = "m"
                        elif gender == "Female":
                            kwargs["gender"] = "f"
                        else:
                            badRequest = True

                    if not badRequest:
                        try:
                            patient = Patient.objects.filter(**kwargs)
                        except:
                            patient = None

        if not patient and not badRequest:
            notFound = True
        elif patient:
            if patient_id:
                ret = self.serialize(patient)
            else: 
                ret = []
                for x in patient:
                    if aClinic != None:
                        routingSlip = RoutingSlip.objects.filter(patient=x.id, clinic=aClinic)
                        if routingSlip != None and len(routingSlip) > 0:
                            ret.append(x.id)
                    else:
                        ret.append(x.id)
                            
        if badRequest:
            return HttpResponseBadRequest()
        elif notFound:
            return HttpResponseNotFound()
        else:
            return Response(ret)
コード例 #13
0
def return_response_not_found(request):
    c = Context({'status': 'ERROR: Not Found', 'error': True})
    return HttpResponseNotFound(
        render_response_index(request, 'tardis_portal/blank_status.html', c))
コード例 #14
0
 def invalid_id():
     return HttpResponseNotFound("Invalid location ID")
コード例 #15
0
ファイル: webhooks.py プロジェクト: muhend1s/m-saleor
def handle_additional_actions(
    request: WSGIRequest, payment_details: Callable, channel_slug: str
):
    """Handle redirect with additional actions.

    When a customer uses a payment method with redirect, before customer is redirected
    back to storefront, the request goes through the Saleor. We use the data received
    from Adyen, as a query params or as a post data, to finalize an additional action.
    After that, if payment doesn't require any additional action we create an order.
    In case if action data exists, we don't create an order and we include them in url.
    """
    payment_id = request.GET.get("payment")
    checkout_pk = request.GET.get("checkout")

    if not payment_id or not checkout_pk:
        logger.warning(
            "Missing payment_id or checkout id in Adyen's request.",
            extra={"payment_id": payment_id, "checkout_id": checkout_pk},
        )
        return HttpResponseNotFound()

    payment = get_payment(payment_id, transaction_id=None)
    if not payment:
        logger.warning(
            "Payment doesn't exist or is not active.", extra={"payment_id": payment_id}
        )
        return HttpResponseNotFound(
            "Cannot perform payment. There is no active Adyen payment."
        )

    # Adyen for some payment methods can call success notification before we will
    # call an additional_action.
    if not payment.order_id:
        if not payment.checkout or str(payment.checkout.token) != checkout_pk:
            logger.warning(
                "There is no checkout with this payment.",
                extra={"checkout_pk": checkout_pk, "payment_id": payment_id},
            )
            return HttpResponseNotFound(
                "Cannot perform payment. There is no checkout with this payment."
            )

    extra_data = json.loads(payment.extra_data)
    data = extra_data[-1] if isinstance(extra_data, list) else extra_data

    return_url = payment.return_url

    if not return_url:
        logger.warning(
            "Missing return_url for payment.",
            extra={"payment_id": payment_id, "checkout_pk": checkout_pk},
        )
        return HttpResponseNotFound(
            "Cannot perform payment. Lack of data about returnUrl."
        )

    try:
        request_data = prepare_api_request_data(request, data)
    except KeyError as e:
        return HttpResponseBadRequest(e.args[0])
    try:
        result = api_call(request_data, payment_details)
    except PaymentError as e:
        return HttpResponseBadRequest(str(e))
    handle_api_response(payment, result, channel_slug)
    redirect_url = prepare_redirect_url(payment_id, checkout_pk, result, return_url)
    parsed = urlparse(return_url)
    redirect_class = HttpResponseRedirect
    redirect_class.allowed_schemes = [parsed.scheme]
    return redirect_class(redirect_url)
コード例 #16
0
def custom_handler404(request, exception):
    return HttpResponseNotFound('Ошибка 404')
コード例 #17
0
    def get(self, request, *args, **kwargs):
        user = None

        try:
            user = self.get_user(request)
        except self.AuthorizationError as exc:
            return HttpResponseBadRequest(str(exc))
        else:
            if user is None:
                return HttpResponse('badauth', status=401)

        # Check parameters
        if 'host' not in request.GET:
            return HttpResponseBadRequest('Missing host parameter')

        host = None
        content = None
        record = None

        if request.GET.get('content'):
            content = request.GET['content']
        elif request.GET.get('ip'):
            content = request.GET['ip']
        else:
            content = request.META['REMOTE_ADDR']

        if len(content) > 65535:
            return HttpResponseBadRequest('ip exceeds max length')

        # Try to find the host
        try:
            host = Host.objects.get(user=user, name=request.GET['host'])
        except Host.DoesNotExist:
            return HttpResponseNotFound('host not found')

        if request.GET.get('record_id'):
            try:
                record_id = int(request.GET['record_id'])
            except (ValueError, TypeError, IndexError):
                return HttpResponseBadRequest(
                    'Parameter record_id has invalid value')
            else:
                try:
                    record = host.records.get(id=record_id)
                except Record.DoesNotExist:
                    return HttpResponseNotFound('record not found')
                else:
                    return self.update_record(host,
                                              record,
                                              content,
                                              check=True,
                                              user=user)

        # Find appropriate record
        try:
            ip = IPAddress(content)
        except (AddrFormatError, AddrConversionError):
            return HttpResponseBadRequest('invalid ip address (%s)' %
                                          (content, ))
        else:
            desired_rr_type = 'A' if ip.version == 4 else 'AAAA'
            records = host.records.filter(type=desired_rr_type)

            if records.count() > 0:
                record = records.first()
                return self.update_record(host, record, content, user=user)
            else:
                return self.create_record(host,
                                          desired_rr_type,
                                          content,
                                          user=user)
コード例 #18
0
ファイル: error_views.py プロジェクト: Team-De-bug/med-bay
def error_404_handler(request, exception):
    return HttpResponseNotFound(render(request, 'errors/error_404.html'))
コード例 #19
0
ファイル: user_info.py プロジェクト: MAKENTNU/web
def get_user_info_from_username(request, username):
    details = get_user_details_from_username(username)
    return JsonResponse(details) if details else HttpResponseNotFound()
コード例 #20
0
ファイル: views.py プロジェクト: ventycn/oq-engine
def calc_result(request, result_id):
    """
    Download a specific result, by ``result_id``.

    The common abstracted functionality for getting hazard or risk results.

    :param request:
        `django.http.HttpRequest` object. Can contain a `export_type` GET
        param (the default is 'xml' if no param is specified).
    :param result_id:
        The id of the requested artifact.
    :returns:
        If the requested ``result_id`` is not available in the format
        designated by the `export_type`.

        Otherwise, return a `django.http.HttpResponse` containing the content
        of the requested artifact.

    Parameters for the GET request can include an `export_type`, such as 'xml',
    'geojson', 'csv', etc.
    """
    # If the result for the requested ID doesn't exist, OR
    # the job which it is related too is not complete,
    # throw back a 404.
    try:
        job_id, job_status, job_user, datadir, ds_key = logs.dbcmd(
            'get_result', result_id)
        if not utils.user_has_permission(request, job_user):
            return HttpResponseForbidden()
    except dbapi.NotFound:
        return HttpResponseNotFound()

    etype = request.GET.get('export_type')
    export_type = etype or DEFAULT_EXPORT_TYPE

    tmpdir = tempfile.mkdtemp()
    try:
        exported = core.export_from_db(
            (ds_key, export_type), job_id, datadir, tmpdir)
    except DataStoreExportError as exc:
        # TODO: there should be a better error page
        return HttpResponse(content='%s: %s' % (exc.__class__.__name__, exc),
                            content_type='text/plain', status=500)
    if not exported:
        # Throw back a 404 if the exact export parameters are not supported
        return HttpResponseNotFound(
            'Nothing to export for export_type=%s, %s' % (export_type, ds_key))
    elif len(exported) > 1:
        # Building an archive so that there can be a single file download
        archname = ds_key + '-' + export_type + '.zip'
        zipfiles(exported, os.path.join(tmpdir, archname))
        exported = os.path.join(tmpdir, archname)
    else:  # single file
        exported = exported[0]

    content_type = EXPORT_CONTENT_TYPE_MAP.get(
        export_type, DEFAULT_CONTENT_TYPE)

    fname = 'output-%s-%s' % (result_id, os.path.basename(exported))
    stream = FileWrapper(open(exported, 'rb'))  # 'b' is needed on Windows
    stream.close = lambda: (
        FileWrapper.close(stream), shutil.rmtree(tmpdir))
    response = FileResponse(stream, content_type=content_type)
    response['Content-Disposition'] = (
        'attachment; filename=%s' % os.path.basename(fname))
    response['Content-Length'] = str(os.path.getsize(exported))
    return response
コード例 #21
0
def custom_404(request):
    context_dict = {
        'request': request,
        'signed_in': None != request.user,
    }
    return HttpResponseNotFound(render_to_response('404.html', context_dict))
コード例 #22
0
    def search_results(self, request):
        has_filters = True if len(list(
            request.GET.items())) > 1 is not None else False
        request.GET = request.GET.copy()

        se = SearchEngineFactory().create()
        search_results_object = {"query": Query(se)}

        include_provisional = get_provisional_type(request)
        permitted_nodegroups = self.get_permitted_nodegroups(request.user)

        # get a list of resourceIds of type Person et al that meet
        # the search query then use those ids to search for Person instances related to Physical Thing instances
        request.GET[
            "resource-type-filter"] = '[{"graphid":"f71f7b9c-b25b-11e9-901e-a4d18cec433a","name":"Person","inverted":false}]'

        search_filter_factory = SearchFilterFactory(request)
        try:
            for filter_type, querystring in list(
                    request.GET.items()) + [("search-results", "")]:
                search_filter = search_filter_factory.get_filter(filter_type)
                if search_filter:
                    search_filter.append_dsl(search_results_object,
                                             permitted_nodegroups,
                                             include_provisional)
        except Exception as err:
            return JSONResponse(err.message, status=500)

        dsl = search_results_object.pop("query", None)
        dsl.exclude("*")
        results = dsl.search(index="resources", limit=1000)
        resourceIds = [hit["_id"] for hit in results["hits"]["hits"]]

        search_results_object = {"query": Query(se)}

        request.GET[
            "resource-type-filter"] = '[{"graphid":"9519cb4f-b25b-11e9-8c7b-a4d18cec433a","name":"Physical Thing","inverted":false}]'
        search_filter_factory = SearchFilterFactory(request)
        try:
            for filter_type, querystring in list(
                    request.GET.items()) + [("search-results", "")]:
                search_filter = search_filter_factory.get_filter(filter_type)
                if search_filter:
                    search_filter.append_dsl(search_results_object,
                                             permitted_nodegroups,
                                             include_provisional)
        except Exception as err:
            return JSONResponse(err.message, status=500)

        # only search for realted instance references when a filter is applied (aside from the paging filter)
        if has_filters:
            # the resource id query can only return resources of type Physical Thing; this is to handle for the scenario
            # where a Person (or other relatable model added to filter) is related to something other than a Person
            resource_id_query = Bool()
            resource_id_query.must(
                Nested(path="ids",
                       query=Terms(field="ids.id", terms=resourceIds)))
            resource_id_query.must(
                Terms(field="graph_id",
                      terms="9519cb4f-b25b-11e9-8c7b-a4d18cec433a"))

            # we need to wrap the existing query (search_results_object['query']) in a Bool query along with
            # the resource id query above
            outer_query = Bool()
            outer_query.should(resource_id_query)

            dsl = search_results_object.pop("query", None)
            outer_query.should(dsl.dsl.pop("query", None))
            dsl.dsl["query"] = {}
            search_results_object["query"] = dsl

            search_results_object["query"].add_query(outer_query)

        dsl = search_results_object.pop("query", None)
        dsl.include("graph_id")
        dsl.include("root_ontology_class")
        dsl.include("resourceinstanceid")
        dsl.include("points")
        dsl.include("geometries")
        dsl.include("displayname")
        dsl.include("displaydescription")
        dsl.include("map_popup")
        dsl.include("provisional_resource")
        dsl.include("domains")
        # if request.GET.get('tiles', None) is not None:
        #     dsl.include('tiles')

        dsl.include("tiles")
        results = dsl.search(index="resources")
        # print JSONSerializer().serialize(dsl.dsl)

        if results is not None:
            # allow filters to modify the results
            for filter_type, querystring in list(
                    request.GET.items()) + [("search-results", "")]:
                search_filter = search_filter_factory.get_filter(filter_type)
                if search_filter:
                    search_filter.post_search_hook(search_results_object,
                                                   results,
                                                   permitted_nodegroups)

            ret = {}
            ret["results"] = results

            for key, value in list(search_results_object.items()):
                ret[key] = value

            ret["reviewer"] = request.user.groups.filter(
                name="Resource Reviewer").exists()
            ret["timestamp"] = datetime.now()
            ret["total_results"] = dsl.count(index="resources")

            return JSONResponse(ret)
        else:
            return HttpResponseNotFound(
                _("There was an error retrieving the search results"))
コード例 #23
0
ファイル: views.py プロジェクト: nirvguy/anafora
def authenticate(ps,
                 request,
                 projectName="",
                 corpusName="",
                 taskName="",
                 schemaName="",
                 schemaMode=None,
                 isAdjudication=False,
                 isView=False,
                 isCrossDoc=False):
    """
	@type ps:	ProjectSetting
	@type request:	HttpRequest
	@type projectName:	str
	@type corpusName:	str
	@type taskName:		str
	@type schemaName:	str
	@type schemaMode:	str
	@type isAdjudication:	bool
	@type isView:		bool
	@type isCrossDoc:	bool
	@rtype:			HttpResponse
	"""

    if isAdjudication:
        #if request.META["REMOTE_ADMIN"] != True:
        if not isAdjudicator(request):
            return HttpResponseForbidden(
                "%s does not have the authenticate right to adjudicate" %
                request.META["REMOTE_USER"])

    if isView:
        if request.META["REMOTE_ADMIN"] != True:
            return HttpResponseForbidden("%s does not have the 'view' right" %
                                         request.META["REMOTE_USER"])

    if projectName != "":
        if os.path.isdir(
                os.path.join(AnaforaProjectManager.rootPath,
                             projectName)) != True:
            return HttpResponseForbidden("Project Name: '%s' does not exists" %
                                         str(projectName))

    if corpusName != "":
        if os.path.isdir(
                os.path.join(AnaforaProjectManager.rootPath, projectName,
                             corpusName)) != True:
            return HttpResponseForbidden("Corpus Name: '%s' does not exists" %
                                         str(corpusName))
    if taskName != "":
        if os.path.isdir(
                os.path.join(AnaforaProjectManager.rootPath, projectName,
                             corpusName, taskName)) != True:
            return HttpResponseForbidden("Task Name: '%s' does not exists" %
                                         str(taskName))

    if schemaName != "":
        if isSchemaExist(schemaName, schemaMode) != True:
            return HttpResponseNotFound(
                "schema file of schema '%s' is not found" % schemaName)

    return None
コード例 #24
0
ファイル: views.py プロジェクト: shwe87/sync_and_share
def deleteShareFromContent(request):
	#print request.user
	#print request.body
	#print request.POST
	if (request.user.is_authenticated()):
		if request.method == 'POST':
			#typeOf = request.POST['typeOf']
			shareId = request.POST['shareId']
			toDeleteId = request.POST['toDeleteId']
			#uniqueId = request.POST['unique']
			#print uniqueId
		
			#Delete an item that the user was sharing with other users.
			try:
				shared_content = Share.objects.get(unique=shareId)
				shared_from = shared_content.shared_from
				if (shared_from.email == request.user.email):	#If the user is the owner
					try:
						sync = Sync.objects.get(unique=toDeleteId)
						#print "sync deleted"
						shared_content.shared.remove(sync)
					except Sync.DoesNotExist:
						response = HttpResponseNotFound()
						response.write("Not Found")
					checkIfEmpty(shared_content)
					#t = shared_content.shared.all()
					#print "Length of shared.all()"
					#print len(t)
					#if (len(t) is 0):
					#	print "deleted"
						#If it is 0, it means, there is nothing to share:
					#	shared_content.delete()
					#for h in t:
					#	print h.title
					#print "sync deleted 2"
					response = HttpResponse()
					response.write("OK")
				else:
					response = HttpResponseNotAllowed('You are not the owner')						
			except Share.DoesNotExist:
				response = HttpResponseNotFound()
				response.write("Not Found")			
		else:
			response = HttpResponseNotAllowed("Only POST allowed")
	else:
		response.status_code= 401
		response['error'] = 'Unauthorized'
		response.content = 'Unauthorized'
	#response= HttpResponse()
	return response
コード例 #25
0
ファイル: views.py プロジェクト: nirvguy/anafora
def getAnaforaXMLFile(request,
                      projectName,
                      corpusName,
                      taskName,
                      schemaName,
                      schemaMode=None,
                      isAdj=None,
                      annotatorName="",
                      subTaskName=""):
    """
	Given projectName, corpusName, taskName and schema, return the XML data file content

	the default of annotatorName is request.META["REMOTE_USER"]. If annotatorName is assigned, then return this specific annotator's file (annotator permission required) 
	"""

    if request.method != "GET":
        return HttpResponseForbidden()

    if isSchemaExist(schemaName, schemaMode) != True:
        return HttpResponseNotFound("schema file not found")

    #schema = schema.replace(".", "-")

    if annotatorName != "" and annotatorName != request.META[
            "REMOTE_USER"] and isAdjudicator(
                request) is not True and annotatorName != "preannotation":
        return HttpResponseForbidden("access not allowed")

    account = request.META[
        "REMOTE_USER"] if annotatorName == "" else annotatorName
    anaforaXMLFile = os.path.join(settings.ANAFORA_PROJECT_FILE_ROOT,
                                  projectName, corpusName, taskName)
    if subTaskName == None:
        anaforaXMLFile = os.path.join(
            anaforaXMLFile, "%s.%s.%s" %
            (taskName,
             reduce(lambda a, b: "%s-%s" % (a, b), (schemaName, ) +
                    ((schemaMode, ) if schemaMode != None else
                     ()) + (("Adjudication", ) if isAdj != None else
                            ())), account))
    else:
        anaforaXMLFile = os.path.join(
            anaforaXMLFile, subTaskName, "%s.%s.%s" %
            (subTaskName,
             reduce(lambda a, b: "%s-%s" % (a, b), (schemaName, ) +
                    ((schemaMode, ) if schemaMode != None else
                     ()) + (("Adjudication", ) if isAdj != None else
                            ())), account))

    if os.path.exists("%s.completed.xml" % anaforaXMLFile):
        anaforaXMLFile = "%s.completed.xml" % anaforaXMLFile
    elif os.path.exists("%s.inprogress.xml" % anaforaXMLFile):
        anaforaXMLFile = "%s.inprogress.xml" % anaforaXMLFile
    else:
        return HttpResponseNotFound("file not found")

    anaforaXML = ""
    with open(anaforaXMLFile) as fhd:
        try:
            anaforaXML = fhd.read()
        except:
            raise Exception("Open XML File %s error" % anaforaXMLFile)

    return HttpResponse(anaforaXML)
コード例 #26
0
def custom_handler500(request):
    return HttpResponseNotFound('Ошибка 500')
コード例 #27
0
ファイル: views.py プロジェクト: nirvguy/anafora
def setCompleted(request,
                 projectName,
                 corpusName,
                 taskName,
                 schemaName,
                 schemaMode=None,
                 isAdj=None):
    if request.method != "POST":
        return HttpResponseForbidden()

    if isSchemaExist(schemaName, schemaMode) != True:
        return HttpResponseNotFound("schema file not found")

    filePath = os.path.join(settings.ANAFORA_PROJECT_FILE_ROOT, projectName,
                            corpusName, taskName)

    if os.path.exists(filePath) != True:
        return HttpResponseNotFound("project, corpus or task not found")

    fileName = os.path.join(
        filePath, "%s.%s%s%s.%s" %
        (taskName, schemaName, "" if schemaMode == None else "-%s" %
         schemaMode, "" if isAdj == None else "-Adjudication",
         request.META["REMOTE_USER"]))

    ps = getProjectSetting()

    if os.path.exists("%s.inprogress.xml" % fileName):
        subprocess.call([
            "mv",
            "%s.inprogress.xml" % fileName,
            "%s.completed.xml" % fileName
        ])
        subprocess.call(
            "sed -u -i 's/<progress>in-progress<\/progress>/<progress>completed<\/progress>/' %s.completed.xml"
            % (fileName),
            shell=True)
        #subprocess.call(
        #	"sed -u -i 's/@%s/@gold/' %s.completed.xml" % (request.META["REMOTE_USER"], fileName),
        #	shell=True)
        #mode = ps.getMode(*(schemaName.replace("-Adjudication", "").split("-")))
        mode = ps.getMode(schemaName, schemaMode)
        if isAdj is not None or mode.directSetGold:
            # set as gold
            if mode.directSetGold:
                subprocess.call("sed -u -i 's/@%s/@gold/' %s.completed.xml" %
                                (request.META["REMOTE_USER"], fileName),
                                shell=True)
            fileNameGold = filePath + "/" + taskName + "." + schemaName + (
                "" if schemaMode == None else
                ("-" + schemaMode)) + ".gold.completed.xml"
            subprocess.call(["cp", fileName + ".completed.xml", fileNameGold])
            schema = ps.getSchema(schemaName)
            for tModeName in schema.modes:
                tMode = ps.getMode(schemaName, tModeName)
                if tMode.needPreannotation and tMode.preannotationFromMode is not None and tMode.preannotationFromMode.name == mode.name:
                    fileNamePreannotation = filePath + "/" + taskName + "." + schema.name + "-" + tMode.name + ".preannotation.completed.xml"
                    subprocess.call(
                        ["cp", fileNameGold, fileNamePreannotation])

        return HttpResponse()
    else:
        return HttpResponseNotFound(
            "in-progress '%s.inprogress.xml' file not found" % fileName)
コード例 #28
0
ファイル: videos.py プロジェクト: vincentpanqi/edx-platform
def video_encodings_download(request, course_key_string):
    """
    Returns a CSV report containing the encoded video URLs for video uploads
    in the following format:

    Video ID,Name,Status,Profile1 URL,Profile2 URL
    aaaaaaaa-aaaa-4aaa-aaaa-aaaaaaaaaaaa,video.mp4,Complete,http://example.com/prof1.mp4,http://example.com/prof2.mp4
    """
    course = _get_and_validate_course(course_key_string, request.user)

    if not course:
        return HttpResponseNotFound()

    def get_profile_header(profile):
        """Returns the column header string for the given profile's URLs"""
        # Translators: This is the header for a CSV file column
        # containing URLs for video encodings for the named profile
        # (e.g. desktop, mobile high quality, mobile low quality)
        return _("{profile_name} URL").format(profile_name=profile)

    profile_whitelist = VideoUploadConfig.get_profile_whitelist()

    videos = list(_get_videos(course))
    name_col = _("Name")
    duration_col = _("Duration")
    added_col = _("Date Added")
    video_id_col = _("Video ID")
    status_col = _("Status")
    profile_cols = [get_profile_header(profile) for profile in profile_whitelist]

    def make_csv_dict(video):
        """
        Makes a dictionary suitable for writing CSV output. This involves
        extracting the required items from the original video dict and
        converting all keys and values to UTF-8 encoded string objects,
        because the CSV module doesn't play well with unicode objects.
        """
        # Translators: This is listed as the duration for a video that has not
        # yet reached the point in its processing by the servers where its
        # duration is determined.
        duration_val = str(video["duration"]) if video["duration"] > 0 else _("Pending")
        ret = dict(
            [
                (name_col, video["client_video_id"]),
                (duration_col, duration_val),
                (added_col, video["created"].isoformat()),
                (video_id_col, video["edx_video_id"]),
                (status_col, video["status"]),
            ] +
            [
                (get_profile_header(encoded_video["profile"]), encoded_video["url"])
                for encoded_video in video["encoded_videos"]
                if encoded_video["profile"] in profile_whitelist
            ]
        )
        return {
            key.encode("utf-8"): value.encode("utf-8")
            for key, value in ret.items()
        }

    response = HttpResponse(content_type="text/csv")
    # Translators: This is the suggested filename when downloading the URL
    # listing for videos uploaded through Studio
    filename = _("{course}_video_urls").format(course=course.id.course)
    # See https://tools.ietf.org/html/rfc6266#appendix-D
    response["Content-Disposition"] = rfc6266.build_header(
        filename + ".csv",
        filename_compat="video_urls.csv"
    )
    writer = csv.DictWriter(
        response,
        [
            col_name.encode("utf-8")
            for col_name
            in [name_col, duration_col, added_col, video_id_col, status_col] + profile_cols
        ],
        dialect=csv.excel
    )
    writer.writeheader()
    for video in videos:
        writer.writerow(make_csv_dict(video))
    return response
コード例 #29
0
from django.conf.urls.defaults import patterns, url, include
from django.contrib import admin
from django.http import HttpResponseNotFound, HttpResponseServerError

from test_app import views
from waffle.views import wafflejs

handler404 = lambda r: HttpResponseNotFound()
handler500 = lambda r: HttpResponseServerError()

admin.autodiscover()

urlpatterns = patterns(
    '', url(r'^flag_in_view', views.flag_in_view, name='flag_in_view'),
    url(r'^wafflejs$', wafflejs, name='wafflejs'),
    url(r'^switch-on', views.switched_view),
    url(r'^switch-off', views.switched_off_view),
    url(r'^flag-on', views.flagged_view),
    url(r'^flag-off', views.flagged_off_view),
    (r'^admin/', include(admin.site.urls)))
コード例 #30
0
ファイル: views.py プロジェクト: niteshdm/django-useraudit
def test_request_available(request):
    thread_request = middleware.get_request()
    if thread_request == request:
        return HttpResponse('OK')
    return HttpResponseNotFound()
コード例 #31
0
ファイル: views.py プロジェクト: georgesk/wims-lti
def wims_exam(request: HttpRequest, wims_pk: int,
              exam_pk: int) -> HttpResponse:
    """Redirect the client to the WIMS server corresponding to <wims_pk> and exam <exam_pk>.

        Will retrieve/create the right WIMS' class/user according to informations in request.POST.

        Raises:
            - Http404 if no LMS corresponding to request.POST["tool_consumer_instance_guid"]
                has been found in the database.
            - PermissionDenied if the class corresponding to request.POST['context_id'] does not
                exists and roles in request.POST['context_id'] are not one of
                settings.ROLES_ALLOWED_CREATE_WIMS_CLASS.

        Returns:
            - HttpResponseRedirect redirecting the user to WIMS, logged in his WIMS' class.
            - HttpResponse(status=502) if an error occured while communicating with the WIMS.
            - HttpResponse(status=504) if the WIMS server could not be joined."""
    if request.method == "GET":
        uri = request.build_absolute_uri()
        return HttpResponseNotAllowed(["POST"],
                                      GET_ERROR_MSG % (uri[:-1], uri))
    if request.method != "POST":
        return HttpResponseNotAllowed(
            ["POST"], "405 Method Not Allowed: '%s'" % request.method)

    try:
        parameters = parse_parameters(request.POST)
        logger.info("Request received from '%s'" %
                    request.META.get('HTTP_REFERER', "Unknown"))
        check_parameters(parameters)
        check_custom_parameters(parameters)
        is_valid_request(request)
    except BadRequestException as e:
        logger.info(str(e))
        return HttpResponseBadRequest(str(e))

    # Retrieve the WIMS server
    try:
        wims_srv = WIMS.objects.get(pk=wims_pk)
    except WIMS.DoesNotExist:
        raise Http404("Unknown WIMS server of id '%d'" % wims_pk)

    # Retrieve the LMS
    try:
        lms = LMS.objects.get(guid=parameters["tool_consumer_instance_guid"])
    except LMS.DoesNotExist:
        raise Http404("No LMS found with guid '%s'" %
                      parameters["tool_consumer_instance_guid"])

    wapi = wimsapi.WimsAPI(wims_srv.url, wims_srv.ident, wims_srv.passwd)

    try:
        # Check that the WIMS server is available
        bol, response = wapi.checkident(verbose=True)
        if not bol:
            raise wimsapi.WimsAPIError(response['message'])

        # Get the class
        wclass_db = WimsClass.objects.get(wims=wims_srv,
                                          lms=lms,
                                          lms_guid=parameters['context_id'])

        try:
            wclass = wimsapi.Class.get(wims_srv.url,
                                       wims_srv.ident,
                                       wims_srv.passwd,
                                       wclass_db.qclass,
                                       wims_srv.rclass,
                                       timeout=settings.WIMSAPI_TIMEOUT)
        except wimsapi.WimsAPIError as e:
            if "not existing" in str(
                    e):  # Class was deleted on the WIMS server
                qclass = wclass_db.qclass
                logger.info((
                    "Deleting class (id : %d - wims id : %s - lms id : %s) as it was"
                    "deleted from the WIMS server") %
                            (wclass_db.id, str(
                                wclass_db.qclass), str(wclass_db.lms_guid)))
                wclass_db.delete()
                return HttpResponseNotFound((
                    "Class of ID %s could not be found on the WIMS server. Maybe it has been "
                    "deleted from the WIMS server. Use this LTI link on your LMS to create a new "
                    "WIMS class: %s") % (qclass,
                                         request.build_absolute_uri(
                                             reverse("lti:wims_class",
                                                     args=[wims_pk]))))
            raise  # Unknown error (pragma: no cover)

        # Check whether the user already exists, creating it otherwise
        user_db, user = get_or_create_user(wclass_db, wclass, parameters)

        # Check whether the exam already exists, creating it otherwise
        exam_db, exam = get_exam(wclass_db, wclass, exam_pk, parameters)
        if int(exam.exammode) not in [1, 2]:  # not active or expired
            return HttpResponseForbidden(
                "This exam (%s) is currently unavailable (%s)" %
                (str(exam.qexam), MODE[int(exam.exammode)]))

        # Storing the URL and ID to send the grade back to the LMS
        try:
            gl = GradeLinkExam.objects.get(user=user_db, activity=exam_db)
            gl.sourcedid = parameters["lis_result_sourcedid"]
            gl.url = parameters["lis_outcome_service_url"]
            gl.save()
        except GradeLinkExam.DoesNotExist:
            GradeLinkExam.objects.create(
                user=user_db,
                activity=exam_db,
                lms=lms,
                sourcedid=parameters["lis_result_sourcedid"],
                url=parameters["lis_outcome_service_url"])

        # If user is a teacher, send all grade back to the LMS
        role = Role.parse_role_lti(parameters["roles"])
        if is_teacher(role):
            GradeLinkExam.send_back_all(exam_db)

        # Trying to authenticate the user on the WIMS server
        bol, response = wapi.authuser(wclass.qclass, wclass.rclass, user.quser)
        if not bol:  # pragma: no cover
            raise wimsapi.WimsAPIError(response['message'])

        params = (
            "&lang=%s&module=adm%%2Fclass%%2Fexam&+job=student&+exam=%s" %
            (wclass.lang, str(exam.qexam)))
        url = response["home_url"] + params

    except WimsClass.DoesNotExist as e:
        logger.info(str(e))
        return HttpResponseNotFound("Could not find class of id '%s'" %
                                    parameters['context_id'])

    except wimsapi.WimsAPIError as e:  # WIMS server responded with ERROR
        logger.info(str(e))
        return HttpResponse(str(e), status=502)

    except requests.RequestException:
        logger.exception("Could not join the WIMS server '%s'" % wims_srv.url)
        return HttpResponse("Could not join the WIMS server '%s'" %
                            wims_srv.url,
                            status=504)

    return redirect(url)
コード例 #32
0
ファイル: views.py プロジェクト: tuchang/fedireads
def book_page(request, book_identifier, tab='friends'):
    ''' info about a book '''
    book = books_manager.get_or_create_book(book_identifier)

    if is_api_request(request):
        return JsonResponse(activitypub.get_book(book))

    if isinstance(book, models.Work):
        book = book.default_edition
    if not book:
        return HttpResponseNotFound()

    work = book.parent_work
    if not work:
        return HttpResponseNotFound()

    book_reviews = models.Review.objects.filter(
        book__in=work.edition_set.all())

    if request.user.is_authenticated:
        user_reviews = book_reviews.filter(user=request.user, ).all()

        reviews = get_activity_feed(request.user, tab, model=book_reviews)

        try:
            # TODO: books can be on multiple shelves
            shelf = models.Shelf.objects.filter(user=request.user,
                                                edition=book).first()
        except models.Shelf.DoesNotExist:
            shelf = None

        user_tags = models.Tag.objects.filter(
            book=book, user=request.user).values_list('identifier', flat=True)
    else:
        tab = 'public'
        reviews = book_reviews.filter(privacy='public')
        shelf = None
        user_reviews = []
        user_tags = []

    rating = reviews.aggregate(Avg('rating'))
    tags = models.Tag.objects.filter(book=book).values(
        'book', 'name', 'identifier').distinct().all()

    data = {
        'book':
        book,
        'shelf':
        shelf,
        'user_reviews':
        user_reviews,
        'reviews':
        reviews.distinct(),
        'rating':
        rating['rating__avg'],
        'tags':
        tags,
        'user_tags':
        user_tags,
        'review_form':
        forms.ReviewForm(),
        'tag_form':
        forms.TagForm(),
        'feed_tabs': [{
            'id': 'friends',
            'display': 'Friends'
        }, {
            'id': 'local',
            'display': 'Local'
        }, {
            'id': 'federated',
            'display': 'Federated'
        }],
        'active_tab':
        tab,
        'path':
        '/book/%s' % book_identifier,
        'cover_form':
        forms.CoverForm(instance=book),
        'info_fields': [
            {
                'name': 'ISBN',
                'value': book.isbn
            },
            {
                'name': 'OCLC number',
                'value': book.oclc_number
            },
            {
                'name': 'OpenLibrary ID',
                'value': book.openlibrary_key
            },
            {
                'name': 'Goodreads ID',
                'value': book.goodreads_key
            },
            {
                'name': 'Format',
                'value': book.physical_format
            },
            {
                'name': 'Pages',
                'value': book.pages
            },
        ],
    }
    return TemplateResponse(request, 'book.html', data)
コード例 #33
0
ファイル: views.py プロジェクト: armstrong/armstrong.esi
def recursive_404(request):
    response = HttpResponseNotFound('<esi:include src="/recursive-404/" />')
    response._esi = {'used': True}
    return response
コード例 #34
0
ファイル: views.py プロジェクト: xxfyx/tscharts
    def get(self, request, format=None):
        badRequest = False
        notFound = False
        aClinic = None
        aStation = None
        queues = None
        queueStatus = None
        internalError = False
        ret = {}

        clinicid = request.GET.get('clinic', '')
        stationid = request.GET.get('station', '')
        clinicstation = request.GET.get('clinicstation', '')
        if not clinicid == '':
            try:
                clinicid = int(clinicid)
                try:
                    aClinic = Clinic.objects.get(id=clinicid)
                except:
                    aClinic = None
                    notFound = True
            except:
                badRequest = True
        else:
            badRequest = True # required arg

        if not stationid == '':
            try:
                stationid = int(stationid)
                try:
                    aStation = Station.objects.get(id=stationid)
                except:
                    aStation = None
                    notFound = True
            except:
                pass

        if not notFound and not badRequest:
            kwargs = {}
            kwargs["clinic"] = aClinic
            if aStation:
                kwargs["station"] = aStation 
            try:
                queues = Queue.objects.filter(**kwargs)
                if not queues or len(queues) == 0:
                    notFound = True
            except:
                notFound = True

        if not notFound and not badRequest:
            try:
                queueStatus = QueueStatus.objects.filter(clinic=aClinic)
                if not queueStatus or len(queueStatus) == 0:
                    notFound = True
                elif queueStatus and len(queueStatus) > 1:
                    internalError = True
                else:
                    queueStatus = queueStatus[0]
            except:
                internalError = True

        if not notFound and not badRequest and not internalError:
            ret["status"] = {"numwaiting": queueStatus.numwaiting,
                             "minq": queueStatus.minq,
                             "maxq": queueStatus.maxq,
                             "avgq": queueStatus.avgq,
                             "minwait": queueStatus.minwait,
                             "maxwait": queueStatus.maxwait,
                             "avgwait": queueStatus.avgwait}
            ret["queues"] = []
            for x in queues:
                if not clinicstation == '' and int(clinicstation) != x.clinicstation_id:
                    # clinicstation does not match
                    continue
                queueData = {}
                aClinicStation = None
                try:
                    aClinicStation = ClinicStation.objects.get(id=x.clinicstation_id)
                except:
                    aClinicStation = None

                if not aClinicStation:
                    internalError = True
                    break

                queueData["name"] = aClinicStation.name
                queueData["name_es"] = aClinicStation.name_es
                queueData["clinicstation"] = aClinicStation.id
                queueData["avgservicetime"] = x.avgservicetime
                queueData["entries"] = []

                try:
                    queueEntries = QueueEntry.objects.filter(queue=x.id).order_by("timein")
                    if not queueEntries:
                        queueEntries = []
                except:
                    internalError = True

                if internalError:
                    break

                for y in queueEntries: 
                    entryData = {}
                    entryData["id"] = y.id
                    entryData["patient"] = y.patient_id
                    entryData["timein"] = str(y.timein)
                    entryData["waittime"] = str(y.waittime)
                    entryData["estwaittime"] = str(y.estwaittime)
                    entryData["routingslipentry"] = y.routingslipentry_id
                    
                    queueData["entries"].append(entryData)
               
                ret["queues"].append(queueData) 
 
        if badRequest:
            return HttpResponseBadRequest()
        if notFound:
            return HttpResponseNotFound()
        if internalError:
            return HttpResponseServerError()
        return Response(ret)
コード例 #35
0
ファイル: views.py プロジェクト: adammck/shadow-poll
def view_404(request):
    response = HttpResponseNotFound()
    response.write("The path is not found")
    return response
コード例 #36
0
ファイル: views.py プロジェクト: pcon-world/pcon_db
def not_found(request, *args, **kwargs):
    return HttpResponseNotFound("Not found")
コード例 #37
0
ファイル: download.py プロジェクト: ANSTO/mytardis
def download_datafiles(request):

    # Create the HttpResponse object with the appropriate headers.
    # TODO: handle no datafile, invalid filename, all http links
    # (tarfile count?)
    expid = request.POST['expid']
    fileString = ''

    comptype = "zip"
    if 'comptype' in request.POST:
        comptype = request.POST['comptype']

    if 'datafile' in request.POST:
        if len(request.POST.getlist('datafile')) > 500:
            comptype = "tar"

    if 'dataset' in request.POST:
        comptype = "tar" #todo quickfix, calc how many files

    # the following protocols can be handled by this module
    protocols = ['', 'file', 'tardis']
    known_protocols = len(protocols)
    if 'datafile' in request.POST or 'dataset' in request.POST:
        if (len(request.POST.getlist('datafile')) > 0 \
                or len(request.POST.getlist('dataset'))) > 0:

            datasets = request.POST.getlist('dataset')
            datafiles = request.POST.getlist('datafile')

            for dsid in datasets:
                for datafile in Dataset_File.objects.filter(dataset=dsid):
                    if has_datafile_access(request=request,
                                            dataset_file_id=datafile.id):
                        p = datafile.protocol
                        if not p in protocols:
                            protocols += [p]
                        absolute_filename = datafile.url.partition('//')[2]
                        if(datafile.url.partition('//')[0] == 'tardis:'):
                            #temp fix for old data
                            filepath = '%s/%s/%s' %\
                            (expid, str(datafile.dataset.id),
                                absolute_filename)

                            print filepath + "######"

                            try:
                                wrapper = FileWrapper(file(
                                    datafile.get_absolute_filepath()))\
                                #exists test. os.exists broken
                            except IOError:
                                print "OLD FILE DETECTED"
                                filepath = '%s/%s' % (expid, absolute_filename)

                            fileString += ('\"' + filepath + '\" ')
                            print fileString
                        else:
                            fileString += '\"%s/%s\" ' %\
                            (expid, absolute_filename)


            for dfid in datafiles:
                datafile = Dataset_File.objects.get(pk=dfid)
                if datafile.dataset.id in datasets:
                    continue
                if has_datafile_access(request=request,
                        dataset_file_id=datafile.id):
                    p = datafile.protocol
                    if not p in protocols:
                        protocols += [p]
                    absolute_filename = datafile.url.partition('//')[2]
                    if(datafile.url.partition('//')[0] == 'tardis:'):
                        #temp fix for old data
                        filepath = '\"%s/%s/%s\" ' %\
                        (expid, str(datafile.dataset.id),
                            absolute_filename)

                        print filepath + "######"

                        try:
                            wrapper = FileWrapper(file(
                                datafile.get_absolute_filepath()))\
                            #exists test. os.exists broken
                        except IOError:
                            print "OLD FILE DETECTED"
                            filepath = '\"%s/%s\" ' %\
                                       (expid, absolute_filename)

                        fileString += filepath
                        print fileString
                    else:
                        fileString += '\"%s/%s\" ' % (expid, absolute_filename)
        else:
            return return_response_not_found(request)

    elif 'url' in request.POST:
        if not len(request.POST.getlist('url')) == 0:
            comptype = "tar" #todo quickfix for zip error
            fileString = ""
            for url in request.POST.getlist('url'):
                url = urllib.unquote(url)
                raw_path = url.partition('//')[2]
                experiment_id = request.POST['expid']
                datafile = Dataset_File.objects.filter(url__endswith=raw_path,
                    dataset__experiment__id=experiment_id)[0]
                if has_datafile_access(request=request,
                                       dataset_file_id=datafile.id):
                    p = datafile.protocol
                    if not p in protocols:
                        protocols += [p]
                    absolute_filename = datafile.url.partition('//')[2]
                    if(datafile.url.partition('//')[0] == 'tardis:'):
                        # expects tardis: formatted stuff
                        # to not include dataset id

                        #temp fix for old data
                        filepath = '\"%s/%s/%s\" ' %\
                            (expid, str(datafile.dataset.id),
                            absolute_filename)

                        print filepath + "######"

                        try:
                            wrapper = FileWrapper(file(
                                datafile.get_absolute_filepath()))\
                            #exists test. os.exists broken
                        except IOError:
                            print "OLD FILE DETECTED"
                            filepath = '\"%s/%s\" ' %\
                                       (expid, absolute_filename)

                        fileString += ('\"' + filepath + '\" ')
                        print fileString
                    else:
                        fileString += '\"%s/%s\" ' % (expid, absolute_filename)
        else:
            return return_response_not_found(request)
    else:
        return return_response_not_found(request)

    # more than one external download location?
    if len(protocols) > known_protocols + 2:
        response = HttpResponseNotFound()
        response.write('<p>Different locations selected!</p>\n')
        response.write('Please limit your selection and try again.\n')
        return response

    # redirect request if another (external) download protocol was found
    elif len(protocols) == known_protocols + 1:
        from django.core.urlresolvers import reverse, resolve
        try:
            for module in settings.DOWNLOAD_PROVIDERS:
                if module[0] == protocols[3]:
                    url = reverse('%s.download_datafiles' % module[1])
                    view, args, kwargs = resolve(url)
                    kwargs['request'] = request
                    return view(*args, **kwargs)
        except:
            return return_response_not_found(request)

    else:
        # tarfile class doesn't work on large files being added and
        # streamed on the fly, so going command-line-o
        if not fileString:
            return return_response_error(request)

        if comptype == "tar":
            cmd = 'tar -C %s -c %s' % (settings.FILE_STORE_PATH,
                                       fileString)

            # logger.info(cmd)
            response = \
                HttpResponse(FileWrapper(subprocess.Popen(
                                                    cmd,
                                                    stdout=subprocess.PIPE,
                                                    stderr=open(devnull, 'w'),
                                                    shell=True).stdout),
                             mimetype='application/x-tar')
            response['Content-Disposition'] = \
                    'attachment; filename="experiment%s-selection.tar"' % expid
            return response
        else:
            cmd = 'cd %s; zip -r - %s' % (settings.FILE_STORE_PATH,
                                       fileString)
            # logger.info(cmd)
            response = \
                HttpResponse(FileWrapper(subprocess.Popen(
                                                    cmd,
                                                    stdout=subprocess.PIPE,
                                                    stderr=open(devnull, 'w'),
                                                    shell=True).stdout),
                             mimetype='application/zip')
            response['Content-Disposition'] = \
                    'attachment; filename="experiment%s-selection.zip"' % expid
            return response
コード例 #38
0
 def wrapped_view(view_obj, *args, **kwargs):
     """Wrapper for the view function."""
     if ApiAccessConfig.current().enabled:
         return view_func(view_obj, *args, **kwargs)
     return HttpResponseNotFound()
コード例 #39
0
ファイル: download.py プロジェクト: grischa/mytardis-mrtardis
def download_datafiles(request):

    # Create the HttpResponse object with the appropriate headers.
    # TODO: handle no datafile, invalid filename, all http links
    # (tarfile count?)
    expid = request.POST['expid']
    protocols = []
    fileString = ''
    fileSize = 0

    # the following protocols can be handled by this module
    protocols = ['', 'file', 'tardis']

    if 'datafile' or 'dataset' in request.POST:

        if (len(request.POST.getlist('datafile')) > 0 \
                or len(request.POST.getlist('dataset'))) > 0:

            datasets = request.POST.getlist('dataset')
            datafiles = request.POST.getlist('datafile')

            for dsid in datasets:
                for datafile in Dataset_File.objects.filter(dataset=dsid):
                    if has_datafile_access(request=request,
                                           dataset_file_id=datafile.id):
                        p = datafile.protocol
                        if not p in protocols:
                            protocols += [p]

                        absolute_filename = datafile.url.partition('//')[2]
                        fileString += '%s/%s ' % (expid, absolute_filename)
                        fileSize += long(datafile.size)

            for dfid in datafiles:
                datafile = Dataset_File.objects.get(pk=dfid)
                if datafile.dataset.id in datasets:
                    continue
                if has_datafile_access(request=request,
                                        dataset_file_id=datafile.id):
                    p = datafile.protocol
                    if not p in protocols:
                        protocols += [p]
                    absolute_filename = datafile.url.partition('//')[2]
                    fileString += '%s/%s ' % (expid, absolute_filename)
                    fileSize += long(datafile.size)

        else:
            return return_response_not_found(request)

    # TODO: check if we really still need this method
    elif 'url' in request.POST:

        if not len(request.POST.getlist('url')) == 0:
            for url in request.POST.getlist('url'):
                datafile = \
                    Dataset_File.objects.get(url=urllib.unquote(url),
                        dataset__experiment__id=request.POST['expid'])
                if has_datafile_access(request=request,
                                       dataset_file_id=datafile.id):
                    p = datafile.protocol
                    if not p in protocols:
                        protocols += [p]
                    absolute_filename = datafile.url.partition('//')[2]
                    fileString += '%s/%s ' % (expid, absolute_filename)
                    fileSize += long(datafile.size)

        else:
            return return_response_not_found(request)
    else:
        return return_response_not_found(request)

    # more than one external download location?
    if len(protocols) > 4:
        response = HttpResponseNotFound()
        response.write('<p>Different locations selected!</p>\n')
        response.write('Please limit your selection and try again.\n')
        return response

    # redirect request if another (external) download protocol was found
    elif len(protocols) == 4:
        from django.core.urlresolvers import resolve
        view, args, kwargs = resolve('/%s%s' % (protocols[4],
                                                request.path))
        kwargs['request'] = request
        return view(*args, **kwargs)

    else:
        # tarfile class doesn't work on large files being added and
        # streamed on the fly, so going command-line-o
        if not fileString:
            return return_response_error(request)

        cmd = 'tar -C %s -c %s' % (settings.FILE_STORE_PATH,
                                   fileString)

        # logger.info(cmd)
        response = \
            HttpResponse(FileWrapper(subprocess.Popen(cmd,
                                                      stdout=subprocess.PIPE,
                                                      shell=True).stdout),
                         mimetype='application/x-tar')
        response['Content-Disposition'] = \
                'attachment; filename="experiment%s.tar"' % expid
        response['Content-Length'] = fileSize + 5120
        return response
コード例 #40
0
def download_datafiles(request):

    # Create the HttpResponse object with the appropriate headers.
    # TODO: handle no datafile, invalid filename, all http links
    # (tarfile count?)
    expid = request.POST["expid"]
    fileString = ""
    fileSize = 0

    comptype = "zip"
    if "comtype" in request.POST:
        comptype = request.POST["comptype"]

    # the following protocols can be handled by this module
    protocols = ["", "file", "tardis"]
    known_protocols = len(protocols)

    if "datafile" or "dataset" in request.POST:
        if (len(request.POST.getlist("datafile")) > 0 or len(request.POST.getlist("dataset"))) > 0:

            datasets = request.POST.getlist("dataset")
            datafiles = request.POST.getlist("datafile")

            for dsid in datasets:
                for datafile in Dataset_File.objects.filter(dataset=dsid):
                    if has_datafile_access(request=request, dataset_file_id=datafile.id):
                        p = datafile.protocol
                        if not p in protocols:
                            protocols += [p]
                        absolute_filename = datafile.url.partition("//")[2]
                        if datafile.url.partition("//")[0] == "tardis:":
                            fileString += "%s/%s/%s " % (expid, str(datafile.dataset.id), absolute_filename)
                        else:
                            fileString += "%s/%s " % (expid, absolute_filename)
                        fileSize += long(datafile.size)

            for dfid in datafiles:
                datafile = Dataset_File.objects.get(pk=dfid)
                if datafile.dataset.id in datasets:
                    continue
                if has_datafile_access(request=request, dataset_file_id=datafile.id):
                    p = datafile.protocol
                    if not p in protocols:
                        protocols += [p]
                    absolute_filename = datafile.url.partition("//")[2]
                    if datafile.url.partition("//")[0] == "tardis:":
                        fileString += "%s/%s/%s " % (expid, str(datafile.dataset.id), absolute_filename)
                    else:
                        fileString += "%s/%s " % (expid, absolute_filename)
                    fileSize += long(datafile.size)
        else:
            return return_response_not_found(request)

    elif "url" in request.POST:
        if not len(request.POST.getlist("url")) == 0:
            fileString = ""
            fileSize = 0
            for url in request.POST.getlist("url"):
                url = urllib.unquote(url)
                raw_path = url.partition("//")[2]
                experiment_id = request.POST["expid"]
                datafile = Dataset_File.objects.filter(url__endswith=raw_path, dataset__experiment__id=experiment_id)[0]
                if has_datafile_access(request=request, dataset_file_id=datafile.id):
                    p = datafile.protocol
                    if not p in protocols:
                        protocols += [p]
                    absolute_filename = datafile.url.partition("//")[2]
                    if datafile.url.partition("//")[0] == "tardis:":
                        # expects tardis: formatted stuff to not include dataset id
                        fileString += "%s/%s/%s " % (expid, str(datafile.dataset.id), absolute_filename)
                    else:
                        fileString += "%s/%s " % (expid, absolute_filename)
                    fileSize += long(datafile.size)
        else:
            return return_response_not_found(request)
    else:
        return return_response_not_found(request)

    # more than one external download location?
    if len(protocols) > known_protocols + 2:
        response = HttpResponseNotFound()
        response.write("<p>Different locations selected!</p>\n")
        response.write("Please limit your selection and try again.\n")
        return response

    # redirect request if another (external) download protocol was found
    elif len(protocols) == known_protocols + 1:
        from django.core.urlresolvers import reverse, resolve

        try:
            for module in settings.DOWNLOAD_PROVIDERS:
                if module[0] == protocols[3]:
                    url = reverse("%s.download_datafiles" % module[1])
                    view, args, kwargs = resolve(url)
                    kwargs["request"] = request
                    return view(*args, **kwargs)
        except:
            return return_response_not_found(request)

    else:
        # tarfile class doesn't work on large files being added and
        # streamed on the fly, so going command-line-o
        if not fileString:
            return return_response_error(request)

        if comptype == "tar":
            cmd = "tar -C %s -c %s" % (settings.FILE_STORE_PATH, fileString)

            # logger.info(cmd)
            response = HttpResponse(
                FileWrapper(subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).stdout),
                mimetype="application/x-tar",
            )
            response["Content-Disposition"] = 'attachment; filename="experiment%s-selection.tar"' % expid
            response["Content-Length"] = fileSize + 5120
            return response
        else:
            cmd = "cd %s; zip -r - %s" % (settings.FILE_STORE_PATH, fileString)

            # logger.info(cmd)
            response = HttpResponse(
                FileWrapper(subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).stdout),
                mimetype="application/zip",
            )
            response["Content-Disposition"] = 'attachment; filename="experiment%s-selection.zip"' % expid
            response["Content-Length"] = fileSize + 5120
            return response
コード例 #41
0
def custom_handler500(request):
    return HttpResponseNotFound('Ой, что то сломалось... 500!')
コード例 #42
0
ファイル: views.py プロジェクト: georgesk/wims-lti
def activities(request: HttpRequest, lms_pk: int, wims_pk: int,
               wclass_pk: int) -> HttpResponse:
    """Display the list of WIMS worksheet and exam in <wclass_pk> WIMS class."""
    try:
        class_srv = WimsClass.objects.get(pk=wclass_pk)
    except WimsClass.DoesNotExist:
        return HttpResponseNotFound(
            "WimsClass of ID %d Was not found on the server." % wclass_pk)

    try:
        try:
            wclass = wimsapi.Class.get(class_srv.wims.url,
                                       class_srv.wims.ident,
                                       class_srv.wims.passwd,
                                       class_srv.qclass,
                                       class_srv.wims.rclass,
                                       timeout=settings.WIMSAPI_TIMEOUT)
        except wimsapi.WimsAPIError as e:
            # Delete the class if it does not exists on the server anymore
            if "class %s not existing" % str(class_srv.qclass) in str(e):
                logger.info((
                    "Deleting class of pk'%s' has the corresponding class of id '%s' does not "
                    "exists on the WIMS server '%s'  anymore") %
                            (str(class_srv.pk), str(
                                class_srv.qclass), class_srv.wims.url))
                class_srv.delete()
            raise  # WIMS server responded with ERROR (pragma: no cover)
        sheets = wclass.listitem(wimsapi.Sheet)

        for s in sheets:
            s.lti_url = request.build_absolute_uri(
                reverse("lti:wims_sheet", args=[wims_pk, s.qsheet]))
            s.sheetmode = MODE[int(s.sheetmode)]

        exams = wclass.listitem(wimsapi.Exam)
        for e in exams:
            e.lti_url = request.build_absolute_uri(
                reverse("lti:wims_exam", args=[wims_pk, e.qexam]))
            e.exammode = MODE[int(e.exammode)]

    except wimsapi.InvalidResponseError as e:  # WIMS server responded with ERROR (pragma: no cover)
        logger.info(str(e), str(e.response))
        messages.error(
            request,
            'The WIMS server returned a badly formatted response: ' + str(e))

    except wimsapi.WimsAPIError as e:  # WIMS server responded with ERROR (pragma: no cover)
        logger.info(str(e))
        messages.error(request, 'The WIMS server returned an error: ' + str(e))

    except requests.RequestException:  # WIMS server responded with ERROR (pragma: no cover)
        logger.exception("Could not join the WIMS server '%s'" %
                         class_srv.wims.url)
        messages.error(request, 'Could not join the WIMS server')

    else:  # No exception occured
        return render(
            request, "lti_app/activities.html", {
                "LMS": LMS.objects.get(pk=lms_pk),
                "WIMS": WIMS.objects.get(pk=wims_pk),
                "class": WimsClass.objects.get(pk=wclass_pk),
                "sheets": sheets,
                "exams": exams,
            })

    # An exception occured
    return redirect('lti:classes', lms_pk=lms_pk,
                    wims_pk=wims_pk)  # pragma: no cover
コード例 #43
0
def custom_handler404(request, exception):
    return HttpResponseNotFound('Ой, что то сломалось... 404!')
コード例 #44
0
ファイル: urls.py プロジェクト: shortyawards/django-scribbler
def test_404(request):
    return HttpResponseNotFound()
コード例 #45
0
def view_404(request):
    response = HttpResponseNotFound()
    template = loader.get_template('404.html')
    response.write(template.render(RequestContext(request)))
    return response