Exemplo n.º 1
0
def update_object(model, value_model, resp, save_path, exclude_fields=None, null_fields=None, mandatory_fields=None):
	model_entry = get_by(model, value_model, test="update_object")
	if model_entry: # If the model_entry exist
		new_model = {}
		if mandatory_fields:
			verify_mandatory_field_form(mandatory_fields, resp)
		for param in resp:
			can_set = 1
			if exclude_fields and param in exclude_fields:
				can_set = 0
			if null_fields and param in null_fields and resp[param] is not None:
				if param == 'password' :
					new_model[param] = base64.b64encode(resp[param])
				else:
					new_model[param] = resp[param]
				can_set = 0
			if can_set == 1 and param in model_entry:
				new_model[param] = resp[param]
		for param in new_model:
			if param in model_entry:
				model_entry[param] = new_model[param]
		myjson.save_json(model, save_path)
	else: # If the model_entry does not exist
		abort(make_response('Object not found', 400))
	return model_entry
Exemplo n.º 2
0
def delete_object(model, id, save_path):
	model_entry = get_by(model, id)
	if model_entry is None:
		abort(make_response('Object not found', 400))
	for i in xrange(len(model)):
		if model[i]['id'] == id:
			model.pop(i)
			break
	myjson.save_json(model, save_path)
Exemplo n.º 3
0
def save(obj):
	models = Models().get(obj.__class__.__name__)
	model = filter(lambda u: u.id == int(obj.id), models)
	
	if len(model) is 0:
		models.append(obj)
	else:
		model[0] = copy.deepcopy(obj)
	final = []
	for m in models:
		final.append(m._to_json())
	myjson.save_json(final, obj.__class__.__name__)
Exemplo n.º 4
0
def new_object(model, resp, save_path, allowed_fields, mandatory_fields=None, special_fields=None, associations=None):
	new_model = {}
	if mandatory_fields:
		verify_mandatory_field_form(mandatory_fields, resp)

	for param in allowed_fields:
		if special_fields and param in special_fields and param in resp:
			if param == 'password' :
				new_model[param] = base64.b64encode(resp[param])
		else:
			if param in resp:
				new_model[param] = resp[param]
			else: 
				new_model[param] = ''
	if associations:
		for association in associations:
			new_model[association['association_name']] = association['association_value']
	new_model['id'] = len(model) + 1
	model.append(new_model)
	myjson.save_json(model, save_path)
	return new_model
Exemplo n.º 5
0
def destroy_token(user):
    user['token'] = ''
    myjson.save_json(glob.users, users_path)
Exemplo n.º 6
0
def generate_token(user):
    user = helpers.get_by(glob.users, user['id'])
    user['token'] = base64.b64encode(str(user['id']) + str(os.urandom(5)) + str(glob.secret_key))
    myjson.save_json(glob.users, users_path)