def ajax_add_photo(): photo = request.files.get('photo') if not photo: return json.dumps({'ok': False, 'error': "Something wrong"}) if photos_model.get_photos_nbr_by_id(session.get('id')) < 4: login = session.get('login') extension = photo.filename.rsplit('.', 1)[1] date = datetime.now().strftime("%Y-%m-%d %H:%M:%S") photo_name = str(login + date + '.' + extension) path = '/static/media/' + login + '/' + photo_name try: images.save(photo, login, photo_name) except UploadNotAllowed: return json.dumps({'ok': False, 'error': "Extension not allowed"}) # images.save(photo, login, photo_name) photos_model.add_photo(session.get('id'), path) rating = user_model.get_user_fame_rating(session.get('id')) + 10 user_model.update_user_rating(rating, session.get('id')) return json.dumps({'ok': True, 'error': "Photo successfully uploaded"}) else: return json.dumps({ 'ok': False, 'error': "You have the maximum number of photos" })
def ajax_delete_friend(): user_id = request.args.get('user_id') if sympathys.check_sympathy(session.get('id'), user_id): if sympathys.check_request(session.get('id'), user_id) == 'sender': sympathys.remove_request(session.get('id'), user_id) status = 'sender' else: status = 'taker' sympathys.set_request_status_to_zero(session.get('id'), user_id) if not sympathys.unlike_user(session.get('id'), user_id): msg = str(session.get('firstname')) + ' ' + str( session.get('lastname')) + ' does not like you anymore.' image = user_model.get_avatar(session.get('id')) notification_view.add_notification(session.get('id'), user_id, msg, 'unlike_user', image) rating = user_model.get_user_fame_rating(user_id) - 20 user_model.update_user_rating(rating, user_id) return json.dumps({ 'ok': True, 'error': "Unlike", 'status': status }) else: return json.dumps({'ok': False, 'error': "Something went wrong"}) else: return json.dumps({'ok': False, 'error': "Something went wrong"})
def ajax_add_comment(): id_photo = request.form['id_photo'] id_user = session.get('id') text = html.escape(request.form['text']) if not id_photo or not id_user or not text: return json.dumps({ 'ok': False, 'error': "Something went wrong" }) res = comments.add_comment(id_photo, id_user, text) to_whom_id = user_model.get_user_id_by_photo_id(id_photo) if to_whom_id != session.get('id'): msg = 'You have a new comment from ' + str(session.get('firstname')) + ' ' + str(session.get('lastname')) + '.' image = user_model.get_avatar(session.get('id')) notifications_view.add_notification(session.get('id'), to_whom_id, msg, 'comment', image) rating = user_model.get_user_fame_rating(to_whom_id) + 5 user_model.update_user_rating(rating, to_whom_id) if not res: return json.dumps({ 'ok': False, 'error': "The comment has not been added" }) return json.dumps({ 'ok': True, 'error': "Commented", 'id_comment': res, 'text': text })
def ajax_delete_photo(): id_photo = request.form['id_photo'] if not id_photo: return json.dumps({'ok': False, 'error': 'Something wrong'}) photos_model.delete_photo_by_id(id_photo) rating = user_model.get_user_fame_rating(session.get('id')) - 10 user_model.update_user_rating(rating, session.get('id')) return json.dumps({'ok': True, 'error': 'Photo deleted'})
def ajax_edit_avatar(): avatar = request.files.get('avatar') if not avatar: return json.dumps({ 'ok': False, 'error': "Not avatar", 'fields': ["avatar"] }) id = session.get('id') login = session.get('login') user = user_model.get_user_by_id(id)[0] extension = avatar.filename.rsplit('.', 1)[1] avatar_name = str('avatar' + '_' + login + '.' + extension) path = '/static/media/' + login + '/' + avatar_name # CHECK IF AVATAR LOADED EARLIER EXISTS avatar_extension = user['avatar'].rsplit('.', 1)[1] name = (APP_ROOT + '/static/media/' + login + '/avatar_' + login + '.' + avatar_extension).strip() # DELETE IF EXISTS if os.path.isfile(name): os.remove(name) user_model.delete_avatar_from_db(avatar_name, id) else: rating = user['fame_rating'] + 42 user_model.update_user_rating(rating, id) # SAVE AVATAR TO FOLDER try: photos.save(avatar, login, avatar_name) except UploadNotAllowed: return json.dumps({ 'ok': False, 'error': "Extension not allowed", 'fields': ["avatar"] }) # photos.save(avatar, login, avatar_name) # CHANGE AVATAR IN DB if not user_model.change_avatar(path, id): return json.dumps({ 'ok': True, 'error': 'Avatar successfully uploaded', }) else: return json.dumps({ 'ok': False, 'error': "Something wrong", 'fields': ["avatar"] })
def ajax_edit_work(): company = request.form['company'] designation = request.form['designation'] from_date = request.form['from_date'] to_date = request.form['to_date'] city = request.form['city'] description = request.form['description'] if not company: return json.dumps({ 'ok': False, 'error': "Please fill in selected field", 'fields': ['company'] }) if not designation: return json.dumps({ 'ok': False, 'error': "Please fill in selected field", 'fields': ['designation'] }) if not from_date: return json.dumps({ 'ok': False, 'error': "Please fill in selected field", 'fields': ['from_date'] }) if not to_date: return json.dumps({ 'ok': False, 'error': "Please fill in selected field", 'fields': ['to_date'] }) if not city: return json.dumps({ 'ok': False, 'error': "Please fill in selected field", 'fields': ['work_city'] }) if user_model.add_work(session.get('id'), company, designation, from_date, to_date, city, description): rating = user_model.get_user_fame_rating(session.get('id')) + 15 user_model.update_user_rating(rating, session.get('id')) return json.dumps({'ok': False, 'error': "Work successfully added"}) else: return json.dumps({'ok': False, 'error': "Something went wrong"})
def ajax_like_back_user(): user_id = request.args.get('user_id') if not sympathys.check_sympathy(session.get('id'), user_id): sympathys.like_back_user(session.get('id'), user_id) sympathys.add_users_to_sympathys(session.get('id'), user_id) msg = 'You like each other! Now you can chat with ' + str( session.get('firstname')) + ' ' + str( session.get('lastname')) + '.' image = user_model.get_avatar(session.get('id')) notification_view.add_notification(session.get('id'), user_id, msg, 'like_back_user', image) rating = user_model.get_user_fame_rating(user_id) + 20 user_model.update_user_rating(rating, user_id) return json.dumps({'ok': True, 'error': "Liked_back"})
def ajax_like(): id_photo = request.form['id_photo'] id_user = request.form['id_user'] if not id_photo or not id_user: return json.dumps({'ok': False, 'error': "Something went wrong"}) res = likes.like(id_photo, id_user) if res: to_whom_id = user_model.get_user_id_by_photo_id(id_photo) if to_whom_id != session.get('id'): msg = str(session.get('firstname')) + ' ' + str( session.get('lastname')) + ' liked your photo.' image = user_model.get_avatar(session.get('id')) notification_view.add_notification(session.get('id'), to_whom_id, msg, 'like', image) rating = user_model.get_user_fame_rating(to_whom_id) + 5 user_model.update_user_rating(rating, to_whom_id) if res == 'was': return json.dumps({'ok': True, 'error': "Was dislike"}) return json.dumps({'ok': True, 'error': "Like"}) else: return json.dumps({'ok': False, 'error': "Remove like"})
def ajax_pick_up_like(): user_id = request.args.get('user_id') if not sympathys.check_sympathy(session.get('id'), user_id): if sympathys.check_request(session.get('id'), user_id) == 'sender': if sympathys.check_request_status(session.get('id'), user_id)[0]['status'] == 0: if sympathys.remove_request(session.get('id'), user_id) == []: rating = user_model.get_user_fame_rating(user_id) - 20 user_model.update_user_rating(rating, user_id) return json.dumps({'ok': True, 'error': "Pick_up_like"}) else: return json.dumps({ 'ok': False, 'error': "Something went wrong" }) else: return json.dumps({ 'ok': False, 'error': "Something went wrong" }) return json.dumps({'ok': False, 'error': "Something went wrong"}) else: return json.dumps({'ok': False, 'error': "It's your friend"})
def ajax_edit_education(): university = request.form['university'] date_from = request.form['date_from'] date_to = request.form['date_to'] description = request.form['description'] if not university: return json.dumps({ 'ok': False, 'error': "Please fill in selected field", 'fields': ['school'] }) if not date_from: return json.dumps({ 'ok': False, 'error': "Please fill in selected field", 'fields': ['date_from'] }) if not date_to: return json.dumps({ 'ok': False, 'error': "Please fill in selected field", 'fields': ['date_to'] }) if user_model.add_education(session.get('id'), university, date_from, date_to, description): rating = user_model.get_user_fame_rating(session.get('id')) + 15 user_model.update_user_rating(rating, session.get('id')) return json.dumps({ 'ok': True, 'error': "Education successfully added" }) else: return json.dumps({'ok': False, 'error': "Something went wrong"})
def ajax_like_user(): user_id = request.args.get('user_id') if photos_model.get_photos_nbr_by_id(session.get('id')) > 0: if photos_model.get_photos_nbr_by_id(user_id) < 1: return json.dumps({ 'ok': False, 'error': "To like a user, he must have at least one photo uploaded" }) check = sympathys.check_request(session.get('id'), user_id) if not check: res = sympathys.like_user(session.get('id'), user_id) if res: msg = str(session.get('firstname')) + ' ' + str( session.get('lastname')) + ' likes you!' image = user_model.get_avatar(session.get('id')) notification_view.add_notification(session.get('id'), user_id, msg, 'like_user', image) rating = user_model.get_user_fame_rating(user_id) + 20 user_model.update_user_rating(rating, user_id) return json.dumps({'ok': True, 'error': "Liked"}) else: return json.dumps({ 'ok': False, 'error': "Something went wrong" }) else: return json.dumps({'ok': False, 'error': "It's your friend"}) else: return json.dumps({ 'ok': False, 'error': "To like a user, you must have at least one photo uploaded" })