Exemplo n.º 1
0
def uploadHistory():
	# Get user id
	u = UserModel()
	user = u.getUserByUsername(request.headers['username'])

	# Save history data
	h = HistoryModel()
	result = h.createHistory(request.form, user['id_user'])

	# Save images and link in DB
	imagelist = ast.literal_eval(request.form['images'])
	resizeImages(imagelist)
	i = ImageModel()
	i.addImages(imagelist, result['history_id'])

	# Send email to admins and author
	admins = u.getAllAdmins()
	data = {}
	data['title'] = request.form['title']
	data['author'] = user['real_name']
	data['id_history'] = result['history_id']
	for admin in admins:
		sendNewHistoryNotification(admin, data)
	if not user['admin']:
		sendNewHistoryNotification(user, data)

	return jsonify({'admin':result['isAdmin'], 'history_id': result['history_id']})
Exemplo n.º 2
0
def getHistory(id):
	h = HistoryModel()
	result = h.getHistoryById(id)

	if result is None:
		abort(404)

	return jsonify({'result': result})
Exemplo n.º 3
0
def listHistoryPoints():
	h = HistoryModel()
	points = h.getHistoryPoints()
	result = []
	for point in points:
		item = {}
		item['type'] = 'Feature'
		item['geometry'] = json.loads(point['geom'])
		h_property = {'h_id': point['id'], 'h_type': point['type']}
		item['properties'] = h_property
		result.append(item)
	return jsonify({'result': result})
Exemplo n.º 4
0
def listHistories():
	htype = request.args.get('type')
	fromid = request.args.get('id')
	isAdmin = False
	if 'username' in request.headers:
		u = UserModel()
		isAdmin = u.getUserByUsername(request.headers['username'])['admin']
	h = HistoryModel()
	result = h.getHistoriesByType(htype,fromid,isAdmin)

	if isinstance(result, dict):
		return jsonify(result)
	else:
		return jsonify({'result': result})
Exemplo n.º 5
0
def deleteHistory(id):
	h = HistoryModel()
	history = h.getHistoryById(id)
	h.deleteHistory(id)

	# Send email to admins and author
	u = UserModel()
	user = u.getUserByUsername(history['username'])
	admins = u.getAllAdmins()
	for admin in admins:
		sendDeletedHistoryNotification(admin, history)
	if not user['admin']:
		sendDeletedHistoryNotification(user, history)

	return jsonify({'result': 'true'})
Exemplo n.º 6
0
def editHistory(id):
	data = json.loads(request.data)

	h = HistoryModel()
	old_history = h.getHistoryById(id)
	h.updateHistory(id, data)

	i = ImageModel()
	# Unlink deleted images
	if len(data['images']) > 0:
		old_imagelist = old_history['images']
		new_imagelist = data['images']
		if isinstance(new_imagelist[0], dict):
			new_imagelist = [el['href'] for el in new_imagelist]
		for image in old_imagelist:
			if image['href'] not in new_imagelist:
				i.deleteImageByFilename(image['href'])

	# Save new images and link in DB
	if 'newImages' in data:
		imagelist = data['newImages']
		resizeImages(imagelist)
		i.addImages(imagelist, id)

	#Publish twitter
	if old_history['twitter'] != data['twitter'] and data['twitter']:
		auth = tweepy.OAuthHandler(app.config['consumer_key'], app.config['consumer_secret'])
		auth.set_access_token(app.config['access_token'], app.config['access_token_secret'])
		apiTwitter = tweepy.API(auth)
		maxLength = 117
		if len(old_history["images"]) > 0:
			maxLength = 93

		maxLength -= (len(app.config['hashtag']) + 1)

		tweet = data["text_history"]
		if(len(tweet) > maxLength):
			maxLength -= 3
			tweet = tweet[0:maxLength]
			tweet += "..."

		tweet += app.config['baseURL'] + data["historyUrl"] + str(data["id_history"]) + " " + app.config['hashtag']

		if len(old_history["images"]) > 0:
			imageTwitter = app.config['IMAGES_FOLDER'] + old_history["images"][0]["href"]
			apiTwitter.update_with_media(imageTwitter,status=tweet)
		else:
			status = apiTwitter.update_status(status=tweet)

	# Send email to admins and author
	u = UserModel()
	user = u.getUserByUsername(data['username'])
	admins = u.getAllAdmins()
	if old_history['status'] == data['status']:
		for admin in admins:
			sendEditedHistoryNotification(admin, data)
		if not user['admin']:
			sendEditedHistoryNotification(user, data)
	else:
		for admin in admins:
			sendPublishedHistoryNotification(admin, data)
		if not user['admin']:
			sendPublishedHistoryNotification(user, data)

	return jsonify({'result': 'true'})