예제 #1
0
 def post(self):
     decoded = simplejson.JSONDecoder().decode(self.request.body)
     mySensor = db.GqlQuery(
         "SELECT __key__ FROM Sensor WHERE name=:1 AND vendor=:2 AND version=:3",
         decoded.get('name'), decoded.get('vendor'),
         decoded.get('version')).get()
     if mySensor == None:
         newSensor = Sensor()
         type = decoded.get('type')
         testsensortype = db.GqlQuery(
             "SELECT __key__ FROM SensorType WHERE type=:1", type).get()
         if testsensortype == None:
             sensortype = SensorType()
             sensortype.type = type
             sensortype.put()
             newSensor.type = sensortype.key().id()
         else:
             newSensor.type = testsensortype.id()
         newSensor.name = decoded.get('name')
         newSensor.vendor = decoded.get('vendor')
         newSensor.version = decoded.get('version')
         newSensor.put()
         self.response.set_status(201)
         self.response.out.write(simplejson.JSONEncoder().encode(
             {"id": newSensor.key().id()}))
     else:
         self.response.set_status(200)
         self.response.out.write(simplejson.JSONEncoder().encode(
             {"id": mySensor.id()}))
예제 #2
0
    def delete(self, *args, **kwargs):
        """Deletes a thought based on the input in the PUT request.
    """
        thought_id = self.request.get(Names.id)
        body = {}
        session_id = get_session_id(self.request)

        # Get the user info.
        user = authenticate_user_session(session_id)
        if user == None:
            body['success'] = False
            body['errorMsg'] = 'Your session has expired.'
            self.response.out.write(simplejson.JSONEncoder().encode(body))
            return

        if user_can_modify_thought_using_id(user, thought_id):
            deleteThoughtUsingId(thought_id)
            body['success'] = True
            self.response.out.write(simplejson.JSONEncoder().encode(body))
            return
        else:
            body['success'] = False
            body[
                'errorMsg'] = 'You do not have permission to delete this thought.'
            self.response.out.write(simplejson.JSONEncoder().encode(body))
            return
예제 #3
0
    def create_or_update_thought(self):
        """Creates or updates a thought based on the HTTP request.
    """
        args = simplejson.loads(self.request.body)
        json_thought = args[
            Names.
            thought]  # This is not a JSON string but rather a dictionary.

        body = {'success': None, 'errorMsg': None}

        session_id = get_session_id(self.request)
        if is_none_or_empty(session_id):
            body['success'] = False
            body['errorMsg'] = 'You are not logged in.'
            self.response.out.write(simplejson.JSONEncoder().encode(body))
            return

        user = authenticate_user_session(session_id)
        if user == None:
            body['success'] = False
            body['errorMsg'] = 'Your session has expired.'
            self.response.out.write(simplejson.JSONEncoder().encode(body))
            return

        thought_has_been_saved = False
        thought = None
        if Names.id not in json_thought:
            thought_has_been_saved = False
        else:
            thought = get_thought_using_id(json_thought[Names.id])
            logging.info('thought: ' + str(thought))
            if thought != None:
                thought_has_been_saved = True

        if not thought_has_been_saved:
            id = create_and_save_thought(json_thought, user)

            self.response.headers[content_type_name] = json_content_type
            body['success'] = True
            body['id'] = id
            self.response.out.write(simplejson.JSONEncoder().encode(body))
        else:
            # Check that the user has permission to update the thought
            if not is_user_permitted_to_modify_thought(user, thought):
                body['success'] = False
                body[
                    'errorMsg'] = 'You are not permmited to modify the thought.'
                self.response.out.write(simplejson.JSONEncoder().encode(body))
                return

            update_thought(thought, json_thought, user)

            self.response.headers[content_type_name] = json_content_type
            body['success'] = True
            body['id'] = get_thoughtId(thought)
            self.response.out.write(simplejson.JSONEncoder().encode(body))
예제 #4
0
파일: utils.py 프로젝트: wkral/scrwall.com
def get_json(obj, keys):
    obj_map = {}
    for key in keys:
        obj_map[key] = getattr(obj, key, '')
    encoder = simplejson.JSONEncoder()
    encoder.default = iso_date
    return encoder.encode(obj_map)
예제 #5
0
def administration_system_directory_data_request_handler(request, model):
    message = ''
    if request.method == 'POST':
        path = request.POST.get('path', '')
        if path != '':
            try:
                model.objects.get(path=path)
            except model.DoesNotExist:
                # save dir
                source_dir = model()
                source_dir.path = path
                source_dir.save()
                message = 'Directory added.'
            else:
                message = 'Directory already added.'
        else:
            message = 'Path is empty.'
        if model == models.StorageDirectory:
            administration_render_storage_directories_to_dicts()

    response = {}
    response['message'] = message
    response['directories'] = []

    for directory in model.objects.all():
        response['directories'].append({
            'id': directory.id,
            'path': directory.path
        })

    return HttpResponse(simplejson.JSONEncoder().encode(response),
                        mimetype='application/json')
예제 #6
0
def directory_children(request, basePath=False):
    path = ''
    if (basePath):
        path = path + basePath
    path = path + request.GET.get('base_path', '')
    path = path + request.GET.get('path', '')

    response = {}
    entries = []
    directories = []

    for entry in sorted_directory_list(path):
        entry = archivematicaFunctions.strToUnicode(entry)
        if unicode(entry)[0] != '.':
            entries.append(entry)
            entry_path = os.path.join(path, entry)
            if os.path.isdir(archivematicaFunctions.unicodeToStr(
                    entry_path)) and os.access(
                        archivematicaFunctions.unicodeToStr(entry_path),
                        os.R_OK):
                directories.append(entry)

    response = {'entries': entries, 'directories': directories}

    return HttpResponse(
        simplejson.JSONEncoder(encoding='utf-8').encode(response),
        mimetype='application/json')
예제 #7
0
파일: payments.py 프로젝트: daasara/riba
def createICICIPaymentReq(params):
    components = ['*', '*', '*', '*', '*', '*', \
            '*', '*', '*', '*', '*', '*', '*', '*', '*']
    data = {'action':'create_icici_request'}
    data['ip'] = params['ip']
    data['detectFraud'] = False
    data['storeBillingShipping'] = False
    data['useragent'] = params['useragent']
    data['acceptHeader'] = params['acceptheader']
    data['returnUrl'] = params['returnUrl']

    data['paymentAction'] = params['paymentAction']
    data['paymentParam'] = params['paymentParam']
    data['payableAmount'] = params['payableAmount']
    data['reqType'] = 'req.Preauthorization'
    data['userId'] = params['userId']

    encoder = simplejson.JSONEncoder()
    jsonStr =encoder.encode(data)
    url = makeAPIUrl(components, None)
    req = APIRequest(url, jsonStr)
    res = getApiResponse(req)
    payment_req = Payment()
    if res.getStatus() == 200:
        payment_req.fromJSONObj(res.getJSONData()['payment'])
        return dict(payment=payment_req, redirect_url=res.getJSONData()['redirectUrl'])
예제 #8
0
def delete(request):
    filepath = request.POST.get('filepath', '')
    filepath = os.path.join('/', filepath)
    error = check_filepath_exists(filepath)

    if error == None:
        filepath = os.path.join(filepath)
        if os.path.isdir(filepath):
            try:
                shutil.rmtree(filepath)
            except:
                error = 'Error attempting to delete directory.'
        else:
            os.remove(filepath)

    response = {}

    if error != None:
        response['message'] = error
        response['error'] = True
    else:
        response['message'] = 'Delete successful.'

    return HttpResponse(simplejson.JSONEncoder().encode(response),
                        mimetype='application/json')
예제 #9
0
def administration_system_directory_delete_request_handler(request, model, id):
    model.objects.get(pk=id).delete()
    if model == models.StorageDirectory:
        administration_render_storage_directories_to_dicts()
    response = {}
    response['message'] = 'Deleted.'
    return HttpResponse(simplejson.JSONEncoder().encode(response), mimetype='application/json')
예제 #10
0
class CookieStorage(storage.BaseStorage):
    encoder = json.JSONEncoder(separators=(',', ':'))

    def __init__(self, *args, **kwargs):
        super(CookieStorage, self).__init__(*args, **kwargs)
        self.data = self.load_data()
        if self.data is None:
            self.init_data()

    def load_data(self):
        try:
            data = self.request.get_signed_cookie(self.prefix)
        except KeyError:
            data = None
        except BadSignature:
            raise SuspiciousOperation('FormWizard cookie manipulated')
        if data is None:
            return None
        return json.loads(data, cls=json.JSONDecoder)

    def update_response(self, response):
        if self.data:
            response.set_signed_cookie(self.prefix,
                                       self.encoder.encode(self.data))
        else:
            response.delete_cookie(self.prefix)
예제 #11
0
def ingest_upload_atk_match_dip_objects_to_resource_component_levels(
        request, uuid, resource_component_id):
    # load object relative paths
    object_path_json = simplejson.JSONEncoder().encode(
        ingest_upload_atk_get_dip_object_paths(uuid))

    try:
        # load resource and child data
        db = ingest_upload_atk_db_connection()
        resource_data_json = simplejson.JSONEncoder().encode(
            atk.get_resource_component_children(db, resource_component_id))
    except:
        return HttpResponseServerError(
            'Database error. Please contact an administrator.')

    return render(request, 'ingest/atk/match.html', locals())
예제 #12
0
def get_temp_directory(request):
    temp_dir = tempfile.mkdtemp()

    response = {}
    response['tempDir'] = temp_dir

    return HttpResponse(simplejson.JSONEncoder().encode(response),
                        mimetype='application/json')
예제 #13
0
def ingest_status(request, uuid=None):
    # Equivalent to: "SELECT SIPUUID, MAX(createdTime) AS latest FROM Jobs WHERE unitType='unitSIP' GROUP BY SIPUUID
    objects = models.Job.objects.filter(hidden=False, subjobof='').values('sipuuid').annotate(timestamp=Max('createdtime')).exclude(sipuuid__icontains = 'None').filter(unittype__exact = 'unitSIP')
    mcp_available = False
    try:
        client = MCPClient()
        mcp_status = etree.XML(client.list())
        mcp_available = True
    except Exception: pass
    def encoder(obj):
        items = []
        for item in obj:
            # Check if hidden (TODO: this method is slow)
            if models.SIP.objects.is_hidden(item['sipuuid']):
                continue
            jobs = helpers.get_jobs_by_sipuuid(item['sipuuid'])
            item['directory'] = utils.get_directory_name(jobs[0])
            item['timestamp'] = calendar.timegm(item['timestamp'].timetuple())
            item['uuid'] = item['sipuuid']
            item['id'] = item['sipuuid']
            del item['sipuuid']
            item['jobs'] = []
            for job in jobs:
                newJob = {}
                item['jobs'].append(newJob)

                # allow user to know name of file that has failed normalization
                if job.jobtype == 'Access normalization failed - copying' or job.jobtype == 'Preservation normalization failed - copying' or job.jobtype == 'thumbnail normalization failed - copying':
                    task = models.Task.objects.get(job=job)
                    newJob['filename'] = task.filename

                newJob['uuid'] = job.jobuuid
                newJob['type'] = job.jobtype
                newJob['microservicegroup'] = job.microservicegroup
                newJob['subjobof'] = job.subjobof
                newJob['currentstep'] = job.currentstep
                newJob['timestamp'] = '%d.%s' % (calendar.timegm(job.createdtime.timetuple()), str(job.createdtimedec).split('.')[-1])
                try: mcp_status
                except NameError: pass
                else:
                    xml_unit = mcp_status.xpath('choicesAvailableForUnit[UUID="%s"]' % job.jobuuid)
                    if xml_unit:
                        xml_unit_choices = xml_unit[0].findall('choices/choice')
                        choices = {}
                        for choice in xml_unit_choices:
                            choices[choice.find("chainAvailable").text] = choice.find("description").text
                        newJob['choices'] = choices
            items.append(item)
        return items

    response = {}
    response['objects'] = objects
    response['mcp'] = mcp_available

    return HttpResponse(
        simplejson.JSONEncoder(default=encoder).encode(response),
        mimetype='application/json'
    )
예제 #14
0
def transfer_delete(request, uuid):
    try:
        transfer = models.Transfer.objects.get(uuid__exact=uuid)
        transfer.hidden = True
        transfer.save()
        response = simplejson.JSONEncoder().encode({'removed': True})
        return HttpResponse(response, mimetype='application/json')
    except:
        raise Http404
예제 #15
0
def copy_to_arrange(request):
    sourcepath = request.POST.get('filepath', '')
    destination = request.POST.get('destination', '')

    error = check_filepath_exists('/' + sourcepath)

    if error == None:
        # use lookup path to cleanly find UUID
        lookup_path = '%sharedPath%' + sourcepath[
            SHARED_DIRECTORY_ROOT.__len__():sourcepath.__len__()] + '/'
        cursor = connection.cursor()
        query = 'SELECT unitUUID FROM transfersAndSIPs WHERE currentLocation=%s LIMIT 1'
        cursor.execute(query, (lookup_path, ))
        possible_uuid_data = cursor.fetchone()

        if possible_uuid_data:
            uuid = possible_uuid_data[0]

            # remove UUID from destination directory name
            modified_basename = os.path.basename(sourcepath).replace(
                '-' + uuid, '')
        else:
            modified_basename = os.path.basename(sourcepath)

        # confine destination to subdir of originals
        sourcepath = os.path.join('/', sourcepath)
        destination = os.path.join('/', destination) + '/' + modified_basename
        # do a check making sure destination is a subdir of ARRANGE_DIR
        destination = pad_destination_filepath_if_it_already_exists(
            destination)

        if os.path.isdir(sourcepath):
            try:
                shutil.copytree(sourcepath, destination)
            except:
                error = 'Error copying from ' + sourcepath + ' to ' + destination + '.'

            if error == None:
                # remove any metadata and logs folders
                for path in directory_contents(destination):
                    basename = os.path.basename(path)
                    if basename == 'metadata' or basename == 'logs':
                        if os.path.isdir(path):
                            shutil.rmtree(path)
        else:
            shutil.copy(sourcepath, destination)

    response = {}

    if error != None:
        response['message'] = error
        response['error'] = True
    else:
        response['message'] = 'Copy successful.'

    return HttpResponse(simplejson.JSONEncoder().encode(response),
                        mimetype='application/json')
예제 #16
0
def ingest_delete(request, uuid):
    try:
        sip = models.SIP.objects.get(uuid__exact=uuid)
        sip.hidden = True
        sip.save()
        response = simplejson.JSONEncoder().encode({'removed': True})
        return HttpResponse(response, mimetype='application/json')
    except:
        raise Http404
예제 #17
0
 def toJSONStr(self, all_attributes=False):
     '''returns a json string of the instance
        the optional parameter forces all attributes to be included in the json string.
        it defaults to false in which case it honors a list of attributes which are not
        encoded to json
     '''
     # FIXME we should implement JSON serializable
     o = self.toJSONObj()
     encoder = simplejson.JSONEncoder()
     return encoder.encode(o)
예제 #18
0
def ingest_upload_atk_save(request, uuid):
    pairs_saved = ingest_upload_atk_save_to_db(request, uuid)

    if pairs_saved > 0:
        response = {"message": "Submitted successfully."}
    else:
        response = {"message": "No pairs saved."}

    return HttpResponse(simplejson.JSONEncoder().encode(response),
                        mimetype='application/json')
예제 #19
0
def jobs_list_objects(request, uuid):
    response = []
    job = models.Job.objects.get(jobuuid=uuid)

    for root, dirs, files in os.walk(job.directory + '/objects', False):
        for name in files:
            directory = root.replace(job.directory + '/objects', '')
            response.append(os.path.join(directory, name))

    return HttpResponse(simplejson.JSONEncoder().encode(response),
                        mimetype='application/json')
예제 #20
0
def default(obj):
    '''Convert object to JSON encodable type.'''
    if isinstance(obj, Decimal):
        return float(obj)
    if isinstance(obj, datetime.datetime):
        return obj.strftime('%Y-%m-%d %H:%M:%S')
    if isinstance(obj, datetime.date):
        return obj.strftime('%Y-%m-%d')
    else:
        if type(obj) == ipaddr.IPv4Network or type(obj) == ipaddr.IPAddress:
            return str(obj)
        return simplejson.JSONEncoder().default(obj)
예제 #21
0
def copy_to_originals(request):
    filepath = request.POST.get('filepath', '')
    error = check_filepath_exists('/' + filepath)

    if error == None:
        processingDirectory = '/var/archivematica/sharedDirectory/currentlyProcessing/'
        sipName = os.path.basename(filepath)
        #autoProcessSIPDirectory = ORIGINALS_DIR
        autoProcessSIPDirectory = '/var/archivematica/sharedDirectory/watchedDirectories/SIPCreation/SIPsUnderConstruction/'
        tmpSIPDir = os.path.join(processingDirectory, sipName) + "/"
        destSIPDir = os.path.join(autoProcessSIPDirectory, sipName) + "/"

        sipUUID = uuid.uuid4().__str__()

        createStructuredDirectory(tmpSIPDir)
        databaseFunctions.createSIP(
            destSIPDir.replace('/var/archivematica/sharedDirectory/',
                               '%sharedPath%'), sipUUID)

        objectsDirectory = os.path.join('/', filepath, 'objects')

        #move the objects to the SIPDir
        for item in os.listdir(objectsDirectory):
            shutil.move(os.path.join(objectsDirectory, item),
                        os.path.join(tmpSIPDir, "objects", item))

        #moveSIPTo autoProcessSIPDirectory
        shutil.move(tmpSIPDir, destSIPDir)
        """
        # confine destination to subdir of originals
        filepath = os.path.join('/', filepath)
        destination = os.path.join(ORIGINALS_DIR, os.path.basename(filepath))
        destination = pad_destination_filepath_if_it_already_exists(destination)
        #error = 'Copying from ' + filepath + ' to ' + destination + '.'
        try:
            shutil.copytree(
                filepath,
                destination
            )
        except:
            error = 'Error copying from ' + filepath + ' to ' + destination + '.'
        """

    response = {}

    if error != None:
        response['message'] = error
        response['error'] = True
    else:
        response['message'] = 'Copy successful.'

    return HttpResponse(simplejson.JSONEncoder().encode(response),
                        mimetype='application/json')
예제 #22
0
    def post(self):
        decoded = simplejson.JSONDecoder().decode(self.request.body)

        data = Data()
        data.test_id = decoded.get('test_id')
        data.device_id = decoded.get('device_id')
        data.content = decoded.get('content')
        data.put()

        self.response.set_status(201)
        self.response.out.write(simplejson.JSONEncoder().encode(
            {"id": data.key().id()}))
예제 #23
0
	def as_simple_results_JSON(self, qstr, schema, results, elapsed):
		if type(schema).__name__=='DatabaseError':
		     return self.error_simple_results_JSON("%s" %schema)
		elif type(schema).__name__=='HTTPError':
		     return self.error_simple_results_JSON("%s" %schema)
		
		rmetacontent = dict([('result_format', APIHandler.RESULT_FORMAT_SIMPLE), ('original_query', qstr), ('execution_time_in_s', str(elapsed))])
		variables = []
		for field in schema:
			variables.append(field[0])	
		rcontent = dict([('fields', variables), ('rows', results)])
		retval = dict([('metadata', rmetacontent), ('data', rcontent)])
		return json.JSONEncoder().encode(retval)
예제 #24
0
    def delete(self, *args, **kwargs):
        """Makes a thought private."""
        thought_id = self.request.get(Names.id)
        if is_none_or_empty(thought_id):
            self.error(400)  # Bad Request
            return

        body = {}
        session_id = get_session_id(self.request)
        if is_none_or_empty(session_id):
            body['success'] = False
            body['errorMsg'] = 'You are not logged in.'
            self.response.out.write(simplejson.JSONEncoder().encode(body))
            return

        # Authenticate User.
        user = authenticate_user_session(session_id)
        if user == None:
            body['success'] = False
            body['errorMsg'] = 'Your session has expired.'
            self.response.out.write(simplejson.JSONEncoder().encode(body))
            return

        if not user_can_modify_thought_using_id(user, thought_id):
            body['success'] = False
            body['errorMsg'] = 'You do not have succificient priviledges.'
            self.response.out.write(simplejson.JSONEncoder().encode(body))
            return

        if not thought_viewable_by_all_using_id(thought_id):
            body['success'] = False
            body['errorMsg'] = 'The thought is already private.'
            self.response.out.write(simplejson.JSONEncoder().encode(body))
            return

        make_thought_private_using_id(thought_id)
        body['success'] = True
        self.response.out.write(simplejson.JSONEncoder().encode(body))
예제 #25
0
def ingest_upload(request, uuid):
    """
        The upload DIP is actually not executed here, but some data is storaged
        in the database (permalink, ...), used later by upload-qubit.py
        - GET = It could be used to obtain DIP size
        - POST = Create Accesses tuple with permalink
    """
    try:
        sip = models.SIP.objects.get(uuid__exact=uuid)
    except:
        raise Http404

    if request.method == 'POST':
        if 'target' in request.POST:
            try:
                access = models.Access.objects.get(sipuuid=uuid)
            except:
                access = models.Access(sipuuid=uuid)
            access.target = cPickle.dumps({
              "target": request.POST['target'] })
            access.save()
            response = simplejson.JSONEncoder().encode({ 'ready': True })
            return HttpResponse(response, mimetype='application/json')
    elif request.method == 'GET':
        try:
            access = models.Access.objects.get(sipuuid=uuid)
            data = cPickle.loads(str(access.target))
        except:
            # pass
            raise Http404
        # Disabled, it could be very slow
        # job = models.Job.objects.get(jobtype='Upload DIP', sipuuid=uuid)
        # data['size'] = utils.get_directory_size(job.directory)
        response = simplejson.JSONEncoder().encode(data)
        return HttpResponse(response, mimetype='application/json')

    return HttpResponseBadRequest()
예제 #26
0
파일: payments.py 프로젝트: daasara/riba
def updatePayment(payment_req, *args, **kwargs):
    '''creates a payment request'''
    components = ['*', '*', '*', '*', '*', '*', \
            '*', '*', '*', '*', '*', '*', '*', '*', '*']
    payment_info = payment_req.toJSONObj()
    payment_info['orderId'] = payment_req['ORDER_ID']
    data = {'update':payment_info}
    encoder = simplejson.JSONEncoder()
    jsonStr =encoder.encode(data)
    url = makeAPIUrl(components, None)
    req = APIRequest(url, jsonStr)
    res = getApiResponse(req)
    if res.getStatus() == 200:
        payment_req.fromJSONObj(res.getJSONData())
        return payment_req
예제 #27
0
    def render(self, response_data={}, response_headers={}):
        """ Serializes the data from this request to JSON and outputs.
    
    Since the version of simplejson used by App Engine is a bit out of date,
    this has to monkey patch the _encode method into the simplejson library.
    (Future versions had support for setting this explicitly.)
    """
        merged_headers = self.get_headers()
        merged_headers.update(response_headers)
        for header_name, header_value in merged_headers.iteritems():
            self.response.headers.add_header(header_name, str(header_value))

        encoder = simplejson.JSONEncoder()
        encoder.default = self._encode  # Monkey patching is fun :)
        merged_data = self.get_data()
        merged_data.update(response_data)
        json = encoder.encode(merged_data)
        self.response.out.write(json)
예제 #28
0
  def __call__ (self, req, **kw):
    if not self._prepared: self._prepare()

    if not self.base:
      #self.base = req.get_host() nope, unless we stop using _data and use iterMembers
      self._data ['base'] = self._host (req)

    if req.is_ajax():  # and hasattr (self, '_ajax'):
      try:
        rslt = self._ajax (req, req.session, **kw)
        if isMappingType (rslt):
          # text/plain is seen by jQuery as 'string', app/json too, app/javascript nfg too
          # , mimetype="application/javascript") looks like we'll have to do it in the js
          return HttpResponse (json.JSONEncoder().encode(rslt))
        else:
          return HttpResponse (rslt)
      except Exception, e:
        return HttpResponse (e) # should return alert script, here!
예제 #29
0
class CookieStorage(storage.BaseStorage):
    encoder = json.JSONEncoder(separators=(',', ':'))

    def __init__(self, *args, **kwargs):
        super(CookieStorage, self).__init__(*args, **kwargs)
        self.data = self.load_data()
        if self.data is None:
            self.init_data()

    def unsign_cookie_data(self, data):
        if data is None:
            return None

        bits = data.split('$', 1)
        if len(bits) == 2:
            if bits[0] == self.get_cookie_hash(bits[1]):
                return bits[1]

        raise SuspiciousOperation('FormWizard cookie manipulated')

    def load_data(self):
        data = self.request.COOKIES.get(self.prefix, None)
        cookie_data = self.unsign_cookie_data(data)
        if cookie_data is None:
            return None
        return json.loads(cookie_data, cls=json.JSONDecoder)

    def update_response(self, response):
        if self.data:
            response.set_cookie(self.prefix,
                                self.create_cookie_data(self.data))
        else:
            response.delete_cookie(self.prefix)
        return response

    def create_cookie_data(self, data):
        encoded_data = self.encoder.encode(self.data)
        cookie_data = '%s$%s' % (self.get_cookie_hash(encoded_data),
                                 encoded_data)
        return cookie_data

    def get_cookie_hash(self, data):
        return hmac.new('%s$%s' % (settings.SECRET_KEY, self.prefix), data,
                        sha_hmac).hexdigest()
예제 #30
0
    def render(self, name, value, attrs=None):
        """Render TinyMCE widget as HTML.
    """
        if value is None:
            value = ''
        value = util.smart_unicode(value)
        final_attrs = self.build_attrs(attrs, name=name)

        self.mce_settings['elements'] = "id_%s" % name

        # convert mce_settings from dict to JSON
        mce_json = simplejson.JSONEncoder().encode(self.mce_settings)

        return safestring.mark_safe(
            self.TINY_MCE_HTML_FMT % {
                'attrs': widgets.flatatt(final_attrs),
                'value': html.escape(value),
                'settings_json': mce_json
            })