예제 #1
0
    def process_response(self, request, response):
        """
        Calculate the ETag, if needed.
        """
        if settings.SEND_BROKEN_LINK_EMAILS:
            warnings.warn(
                "SEND_BROKEN_LINK_EMAILS is deprecated. "
                "Use BrokenLinkEmailsMiddleware instead.",
                DeprecationWarning,
                stacklevel=2)
            BrokenLinkEmailsMiddleware().process_response(request, response)

        if settings.USE_ETAGS:
            if response.has_header('ETag'):
                etag = response['ETag']
            elif response.streaming:
                etag = None
            else:
                etag = '"%s"' % hashlib.md5(response.content).hexdigest()
            if etag is not None:
                if (200 <= response.status_code < 300
                        and request.META.get('HTTP_IF_NONE_MATCH') == etag):
                    cookies = response.cookies
                    response = http.HttpResponseNotModified()
                    response.cookies = cookies
                else:
                    response['ETag'] = etag

        return response
예제 #2
0
def export_resources(request):
    """
    Export resource files.
    """
    def file_iterator(file, chunk_size=512):
        while True:
            c = file.read(chunk_size)
            if c:
                yield c
            else:
                # remove temp file
                file.close()
                break

    response = http.HttpResponseNotModified()

    # get data's zip
    zipfile = None
    try:
        zipfile = tempfile.TemporaryFile()
        exporter.export_resources(zipfile)
        zipfile.seek(0)

        filename = time.strftime("resources_%Y%m%d_%H%M%S.zip",
                                 time.localtime())
        response = http.StreamingHttpResponse(file_iterator(zipfile))
        response['Content-Type'] = 'application/octet-stream'
        response['Content-Disposition'] = 'attachment;filename="%s"' % filename
    except Exception, e:
        if zipfile:
            zipfile.close()
        message = "Can't export resources: %s" % e
        logger.log_tracemsg(message)
        return render(request, 'fail.html', {"message": message})
예제 #3
0
def export_game_data(request):
    """
    Export game world files.
    """
    response = http.HttpResponseNotModified()
    file_type = request.GET.get("file_type", None)

    # get data's zip
    zipfile = None
    try:
        zipfile = tempfile.TemporaryFile()
        exporter.export_zip_all(zipfile, file_type)
        zipfile.seek(0)

        filename = time.strftime("worlddata_%Y%m%d_%H%M%S.zip",
                                 time.localtime())
        response = http.StreamingHttpResponse(utils.file_iterator(zipfile))
        response['Content-Type'] = 'application/octet-stream'
        response['Content-Disposition'] = 'attachment;filename="%s"' % filename
    except Exception, e:
        message = "Can't export game data: %s" % e
        logger.log_tracemsg(message)

        zipfile.close()
        return render(request, 'fail.html', {"message": message})
예제 #4
0
    def process_response(self, request, response):
        "Send broken link emails and calculate the Etag, if needed."
        if response.status_code == 404:
            if settings.SEND_BROKEN_LINK_EMAILS:
                # If the referrer was from an internal link or a non-search-engine site,
                # send a note to the managers.
                domain = request.get_host()
                referer = request.META.get('HTTP_REFERER', None)
                is_internal = _is_internal_request(domain, referer)
                path = request.get_full_path()
                if referer and not _is_ignorable_404(path) and (
                        is_internal or '?' not in referer):
                    ua = request.META.get('HTTP_USER_AGENT', '<none>')
                    ip = request.META.get('REMOTE_ADDR', '<none>')
                    mail_managers("Broken %slink on %s" % ((is_internal and 'INTERNAL ' or ''), domain),
                        "Referrer: %s\nRequested URL: %s\nUser agent: %s\nIP address: %s\n" \
                                  % (referer, request.get_full_path(), ua, ip))
                return response

        # Use ETags, if requested.
        if settings.USE_ETAGS:
            if response.has_header('ETag'):
                etag = response['ETag']
            else:
                etag = '"%s"' % md5_constructor(response.content).hexdigest()
            if response.status_code >= 200 and response.status_code < 300 and request.META.get(
                    'HTTP_IF_NONE_MATCH') == etag:
                cookies = response.cookies
                response = http.HttpResponseNotModified()
                response.cookies = cookies
            else:
                response['ETag'] = etag

        return response
예제 #5
0
파일: tools.py 프로젝트: xujiameng/muddery
def export_js_localized_strings(request):
    """
    Export all unlicalized strings in files.
    """
    response = http.HttpResponseNotModified()

    try:
        # write to a file
        file = tempfile.TemporaryFile()

        # get strings
        strings = utils.all_unlocalized_js_strings(True)

        for s in strings:
            file.write('"%s": "",\n' % s)

        file.seek(0)
        filename = time.strftime("strings_%Y%m%d_%H%M%S.js", time.localtime())
        response = http.StreamingHttpResponse(utils.file_iterator(file))
        response['Content-Type'] = 'application/octet-stream'
        response['Content-Disposition'] = 'attachment;filename="%s"' % filename
    except Exception as e:
        message = "Can't export game data: %s" % e
        logger.log_tracemsg(message)

        file.close()
        return render(request, 'fail.html', {"message": message})

    return response
예제 #6
0
 def check_etag(self, data):
     new_etag = self.etag_function and self.etag_function(data)
     if not new_etag:
         return
     if self.request.META.get('HTTP_IF_NONE_MATCH', None) == new_etag:
         raise http.HttpResponseNotModified()
     if self.request.META.get('HTTP_IF_MATCH', new_etag) != new_etag:
         raise http.HttpResponse(status=412)  # Precondition Failed
예제 #7
0
파일: views.py 프로젝트: lynnyuue/muddery
def import_data_single(request):
    """
    Import the game world from an uploaded zip file.
    """
    response = http.HttpResponseNotModified()
    model_name = request.POST.get("model_name", None)
    upload_file = request.FILES.get("import_data_single", None)

    success = False
    if upload_file:
        # Get file type.
        (filename, ext_name) = os.path.splitext(upload_file.name)
        file_type = None
        if ext_name:
            file_type = ext_name[1:]

        reader_class = readers.get_reader(file_type)
        if not reader_class:
            return render(request, 'fail.html',
                          {"message": "Can not import this type of file."})

        # Get tempfile's name.
        temp_name = tempfile.mktemp()

        if reader_class.binary:
            open_mode = "wb"
        else:
            open_mode = "w"
        temp_file = open(temp_name, open_mode)

        try:
            # Write to a template file.
            for chunk in upload_file.chunks():
                temp_file.write(chunk)
            temp_file.flush()

            # If model name is empty, get model name from filename.
            if not model_name:
                model_name = filename

            if importer.import_file(temp_name,
                                    model_name,
                                    file_type=file_type,
                                    widecard=False):
                success = True
        except Exception, e:
            logger.log_tracemsg("Cannot import game data: %s" % e)

        if temp_file:
            temp_file.close()

        try:
            os.remove(temp_name)
        except:
            pass
예제 #8
0
    def process_response(self, request, response):
        "Send broken link emails and calculate the Etag, if needed."
        if response.status_code == 404:
            if settings.SEND_BROKEN_LINK_EMAILS and not settings.DEBUG:
                # If the referrer was from an internal link or a
                # non-search-engine site, send a note to the managers.
                domain = request.get_host()
                referer = request.META.get('HTTP_REFERER')
                is_internal = _is_internal_request(domain, referer)
                path = request.get_full_path()
                if (referer
                        and not _is_ignorable_404(path)
                        and (is_internal or '?' not in referer)):
                    ua = request.META.get('HTTP_USER_AGENT', '<none>')
                    ip = request.META.get('REMOTE_ADDR', '<none>')

                    user = None
                    if request.user and hasattr(request.user, 'email'):
                        user = request.user.email

                    content = (
                        "Referrer: %s\n"
                        "Requested URL: %s\n"
                        "User agent: %s\n"
                        "IP address: %s\n"
                        "User: %s\n"
                    ) % (referer, request.get_full_path(), ua, ip, user)
                    internal = is_internal and 'INTERNAL ' or ''
                    mail_managers(
                        "Broken %slink on %s" % (internal, domain),
                        content,
                        fail_silently=True,
                    )
                return response

        # Use ETags, if requested.
        if settings.USE_ETAGS:
            if response.has_header('ETag'):
                etag = response['ETag']
            elif response.streaming:
                etag = None
            else:
                etag = '"%s"' % hashlib.md5(response.content).hexdigest()
            if etag is not None:
                if (200 <= response.status_code < 300
                        and request.META.get('HTTP_IF_NONE_MATCH') == etag):
                    cookies = response.cookies
                    response = http.HttpResponseNotModified()
                    response.cookies = cookies
                else:
                    response['ETag'] = etag

        return response
예제 #9
0
def transaction_poll(request, uuid):
    """ Open a long-standing HTTP connection to get transaction info.

    The HTTP response terminates when we get a transaction
    confirmation from the network.
    """

    # print "Entering transaction_poll()"

    transaction = models.DownloadTransaction.objects.get(uuid=uuid)

    redis = cache.client.get_client(write=True)
    pubsub = redis.pubsub()
    pubsub.subscribe("transaction_%s" % transaction.uuid)

    timeout = time.time() + 10

    # At the beginning of the poll do force one manual refresh of the address.
    # This way we mitigate problems with unreliable BlochChain notifications
    # and unreliable Redis pubsub
    if transaction.payment_source == models.DownloadTransaction.PAYMENT_SOURCE_BLOCKCHAIN:
        if blockchain.force_check_old_address(transaction):
            return http.HttpResponse(
                json.dumps(transaction.get_notification_message()))
    else:
        if payment.force_check_old_address(transaction):
            return http.HttpResponse(
                json.dumps(transaction.get_notification_message()))

    while time.time() < timeout:
        # print "Transaction polling started %s", now()

        try:
            message = pubsub.get_message()
            if message:
                # print "Got message", message
                if message["type"] != "subscribe":
                    data = json.loads(message["data"].decode("utf-8"))
                    if data["transaction_uuid"] == uuid:
                        return http.HttpResponse(
                            json.dumps(data).encode("utf-8"))
        except Exception as e:
            pubsub.close()
            logger.error("Polling exception")
            logger.exception(e)

        # print "Transaction polling stopped %s", now()
        time.sleep(0.5)

    # print "Returning"
    pubsub.close()
    return http.HttpResponseNotModified()
예제 #10
0
def feed(request):
    """A view to support creating, modifying, and deleting feeds.

    RESTful: uses PUT to create, POST to edit, and DELETE to delete. Django's
    built-in syndication requires its own view, so GET is not implemented.

    """
    params = http.QueryDict(request.body, request.encoding)
    uid = users.get_current_user().user_id()
    if request.method == 'PUT':
        if db.Query(models.Feed).filter('link =', params['link']).count() == 0:
            f = models.Feed(link=params['link'], users=[uid], usercount=1)
            f.put()
            return feed_response(f)
        else:
            f = db.Query(models.Feed).filter('link =', params['link']).get()
            if uid in f.users:
                return http.HttpResponseNotModified()
            else:
                f.users.append(uid)
                f.usercount += 1
                f.put()
                return feed_response(f)
    elif request.method == 'POST':
        # Modify an existing Feed object.
        return http.HttpResponse(content='Not Implemented', status=501)
    elif request.method == 'DELETE':
        try:
            f = models.Feed.get_by_id(int(params['id']))
            f.users.remove(uid)
            f.usercount -= 1
            f.put()
            return feed_response(f)
        except ValueError:
            return http.HttpResponseBadRequest()
        except db.NotSavedError:
            return http.HttpResponseNotModified()
    else:
        return http.HttpResponseRedirect(urlresolvers.reverse('feeds'))
예제 #11
0
파일: storage.py 프로젝트: kubili/PiplMesh
def serve(request, path):
    """
    Serve files from default storage.

    To use, put a URL pattern such as::

        (r'^(?P<path>.*)$', 'piplmesh.utils.storage.serve')

    in your URLconf.
    """

    if not settings.DEBUG:
        raise exceptions.ImproperlyConfigured(
            "The view can only be used in debug mode.")
    normalized_path = posixpath.normpath(urllib.unquote(path)).lstrip('/')

    if not storage.default_storage.exists(normalized_path):
        if path.endswith('/') or path == '':
            raise http.Http404("Directory indexes are not allowed here.")
        raise http.Http404("'%s' could not be found" % path)

    try:
        mimetype = storage.default_storage.mimetype(
            normalized_path) or 'application/octet-stream'
    except (NotImplementedError, AttributeError):
        mimetype = 'application/octet-stream'

    try:
        modified_time = time.mktime(
            storage.default_storage.modified_time(normalized_path).timetuple())
    except (NotImplementedError, AttributeError):
        modified_time = None

    size = storage.default_storage.size(normalized_path)

    if modified_time is not None and not static.was_modified_since(
            request.META.get('HTTP_IF_MODIFIED_SINCE'), modified_time, size):
        return http.HttpResponseNotModified(mimetype=mimetype)

    f = storage.default_storage.open(normalized_path, 'rb')
    try:
        response = http.HttpResponse(f.read(), mimetype=mimetype)
    finally:
        f.close()

    response['Content-Length'] = size

    if modified_time is not None:
        response['Last-Modified'] = http_utils.http_date(modified_time)

    return response
예제 #12
0
def import_data_single(request):
    """
    Import the game world from an uploaded zip file.
    """
    response = http.HttpResponseNotModified()
    model_name = request.POST.get("model_name", None)
    file_obj = request.FILES.get("import_data_single", None)

    err_message = None
    if file_obj:
        # Get file type.
        (filename, ext_name) = os.path.splitext(file_obj.name)

        # If model name is empty, get model name from filename.
        if not model_name:
            model_name = filename

        # get data handler
        data_handler = DATA_SETS.get_handler(model_name)
        if not data_handler:
            logger.log_tracemsg("Cannot get data handler: %s" % model_name)
            return render(request, 'fail.html',
                          {"message": "Can not import this file."})

        file_type = None
        if ext_name:
            file_type = ext_name[1:]

        reader_class = readers.get_reader(file_type)
        if not reader_class:
            return render(request, 'fail.html',
                          {"message": "Can not import this type of file."})

        # Get tempfile's name.
        temp_name = tempfile.mktemp()
        with open(temp_name, "wb") as temp_file:
            try:
                # Write to a template file.
                for chunk in file_obj.chunks():
                    temp_file.write(chunk)
                temp_file.flush()
                data_handler.import_file(temp_name, file_type=file_type)
            except Exception, e:
                err_message = "Cannot import game data. %s" % e
                logger.log_errmsg(err_message)

        try:
            os.remove(temp_name)
        except:
            pass
예제 #13
0
def import_resources_all(request):
    """
    Import resources from an uploaded zip file.
    """
    response = http.HttpResponseNotModified()
    file_obj = request.FILES.get("import_resources_all", None)

    if file_obj:
        with tempfile.TemporaryFile() as zipfile:
            try:
                for chunk in file_obj.chunks():
                    zipfile.write(chunk)
                importer.unzip_resources_all(zipfile)
            except Exception, e:
                logger.log_tracemsg("Cannot import resources: %s" % e)
                return render(request, 'fail.html', {"message": str(e)})
예제 #14
0
파일: views.py 프로젝트: lynnyuue/muddery
def export_data_single(request):
    """
    Export a data table.
    """
    response = http.HttpResponseNotModified()
    model_name = request.GET.get("model_name", None)
    file_type = request.GET.get("file_type", None)

    if not file_type:
        # Default file type.
        file_type = "csv"

    writer_class = writers.get_writer(file_type)
    if not writer_class:
        return render(request, 'fail.html',
                      {"message": "Can not export this type of file."})

    # Get tempfile's name.
    temp_name = tempfile.mktemp()
    temp_file = None
    try:
        exporter.export_file(temp_name, model_name, file_type)

        if writer_class.binary:
            open_mode = "rb"
        else:
            open_mode = "r"
        temp_file = open(temp_name, open_mode)

        filename = model_name + "." + writer_class.file_ext
        response = http.StreamingHttpResponse(
            file_iterator(temp_file, erase=True))
        response['Content-Type'] = 'application/octet-stream'
        response['Content-Disposition'] = 'attachment;filename="%s"' % filename
    except Exception, e:
        message = "Can't export game data: %s" % e
        logger.log_tracemsg(message)

        if temp_file:
            temp_file.close()

        try:
            os.remove(temp_name)
        except Exception, e:
            pass
예제 #15
0
파일: views.py 프로젝트: lynnyuue/muddery
def import_data_all(request):
    """
    Import the game world from an uploaded zip file.
    """
    response = http.HttpResponseNotModified()
    file_obj = request.FILES.get("import_data_all", None)

    if file_obj:
        zipfile = tempfile.TemporaryFile()
        try:
            for chunk in file_obj.chunks():
                zipfile.write(chunk)
            importer.unzip_data_all(zipfile)
            zipfile.close()
        except Exception, e:
            logger.log_tracemsg("Cannot import game data: %s" % e)

            zipfile.close()
            return render(request, 'fail.html', {"message": str(e)})
예제 #16
0
    def process_response(self, request, response):
        """
        Calculate the ETag, if needed.
        """
        if settings.USE_ETAGS:
            if response.has_header('ETag'):
                etag = response['ETag']
            elif response.streaming:
                etag = None
            else:
                etag = '"%s"' % hashlib.md5(response.content).hexdigest()
            if etag is not None:
                if (200 <= response.status_code < 300
                        and request.META.get('HTTP_IF_NONE_MATCH') == etag):
                    cookies = response.cookies
                    response = http.HttpResponseNotModified()
                    response.cookies = cookies
                else:
                    response['ETag'] = etag

        return response
예제 #17
0
def serve_upload(request, invoice_path):
    path = 'invoices/' + invoice_path
    upload = shortcuts.get_object_or_404(Invoice, document=path)
    fullpath = upload.document.path

    if not request.user.is_authenticated():
        return http.HttpResponseForbidden()

    statobj = os.stat(fullpath)
    mimetype, encoding = mimetypes.guess_type(fullpath)
    mimetype = mimetype or 'application/octet-stream'
    if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
                              statobj.st_mtime, statobj.st_size):
        return http.HttpResponseNotModified(mimetype=mimetype)
    response = http.HttpResponse(open(fullpath, 'rb').read(),
                                 mimetype=mimetype)
    response["Last-Modified"] = http_date(statobj.st_mtime)
    response["Content-Length"] = statobj.st_size
    if encoding:
        response["Content-Encoding"] = encoding
    return response
예제 #18
0
    def post(self, request, *args, **kwargs):

        public = request.POST.get('public')
        signer = Signer(public=public)

        value = signer.unsign(kwargs.get('signature'))

        if value == kwargs.get('username'):
            User = get_user_model()
            modified = User.objects.filter(username=kwargs['username']).filter(
                Q(key__isnull=True) | Q(key='')).update(key=public)

            if not modified:
                return http.HttpResponseNotModified()

            ActivityLog.objects.log_activity(
                request, User.objects.get(username=kwargs['username']),
                ActivityLog.TYPE.MODIFY, 'User Key Initialized')
        else:
            return http.HttpResponseForbidden()

        return JsonResponse({})
예제 #19
0
def _ajax_mark_as_read(request, instance):
    if instance.is_deleted:
        return http.HttpResponseNotModified()
    instance.soft_delete()
    return http.HttpResponse()