def notes_delete_message(): resource_id = request.form['resource_id'] thread_id = str(request.form['thread_id']) # TODO: fix inconsistant field name msg_id = request.form['msgId'] user = current_user.name thread = Note.get_by_thread_id(thread_id) if thread is None: return Response('no thread', mimetype='text/plain') # TODO: check if actually is owner is_owner = True def should_keep(msg): _content, _user, _id = msg # lack permission to delete if not (_user == user or is_owner): return True # keep if it's not the message we're looking for return _id != msg_id msgs = json.loads(thread.data) new_msgs = filter(should_keep, msgs) if len(msgs) == len(new_msgs): return Response('error', mimetype='text/plain') if len(new_msgs) == 0: db.session.delete(thread) else: thread.data = json.dumps(new_msgs) thread.updated = datetime.utcnow() unread_notes = UnreadNote.query.filter_by(msg_id=msg_id).all() for note in unread_notes: db.session.delete(note) db.session.commit() return Response('deleted', mimetype='text/plain')
def scriptcontent(): resource_id = request.form['resource_id'] if resource_id == 'Demo': latest_version = ScriptData.get_latest_version('Demo') lines = [["Fade in:", 1], ["INT. ", 0]] if latest_version is not None: lines = json.loads(latest_version.data) return jsonify(title='Duck Soup', lines=lines, notes=[], sharedwith=[], contacts=[], autosave='true') user_email = get_current_user_email_with_default() permission = Screenplay.get_users_permission(resource_id, user_email) if permission not in ['owner', 'collab']: return 'not found' title = Screenplay.get_title(resource_id) latest_version = ScriptData.get_latest_version(resource_id) sharedwith = Screenplay.get_all_collaborators(resource_id) user = current_user.name unread_notes = UnreadNote.query. \ filter_by(resource_id=resource_id, user=user).all() unread_msg_ids = set([n.msg_id for n in unread_notes]) note_rows = Note.get_by_resource_id(resource_id) notes = [note.to_dict(unread_msg_ids) for note in note_rows] return jsonify(title=title, lines=json.loads(latest_version.data), lastSavedVersionNumber=latest_version.version, notes=notes, sharedwith=sharedwith, autosave='true')
def notes_submit_message(): resource_id = request.form['resource_id'] thread_id = str(request.form['thread_id']) content = request.form['content'] msg_id = request.form['msg_id'] # only if this edits a previous message user = get_current_user_email_with_default() if resource_id == 'Demo': output = json.dumps([content, msg_id, user, thread_id]) return Response(output, mimetype='text/plain') thread = Note.get_by_thread_id(thread_id) msgs = json.loads(thread.data) was_new_message = True for msg in msgs: _content, _user, _id = msg if _id == msg_id and _user == user: msg[0] = content was_new_message = False if was_new_message: msg_id = str(datetime.utcnow()) msgs.append([content, user, msg_id]) thread.data = json.dumps(msgs) thread.updated = datetime.utcnow() new_note_notification(resource_id, user, thread_id, msg_id) db.session.commit() if request.form['fromPage'] == 'mobileviewnotes': return Response('sent', mimetype='text/plain') output = json.dumps([content, msg_id, user, thread_id]) return Response(output, mimetype='text/plain')
def scriptcontent(): resource_id = request.form['resource_id'] if resource_id == 'Demo': latest_version = ScriptData.get_latest_version('Demo') return jsonify(title='Duck Soup', lines=json.loads(latest_version.data), spelling=[], notes=[], sharedwith=[], contacts=[], autosave='true') user_email = get_current_user_email_with_default() screenplay = UsersScripts.query.filter_by(resource_id=resource_id, user=user_email).first() if not screenplay: return 'not found' latest_version = ScriptData.get_latest_version(resource_id) sharedwith = UsersScripts.get_all_collaborators(resource_id) user = current_user.name unread_notes = UnreadNote.query. \ filter_by(resource_id=resource_id, user=user).all() unread_msg_ids = set([n.msg_id for n in unread_notes]) note_rows = Note.get_by_resource_id(resource_id) notes = [note.to_dict(unread_msg_ids) for note in note_rows] return jsonify(title=screenplay.title, lines=json.loads(latest_version.data), lastSavedVersionNumber=latest_version.version, notes=notes, sharedwith=sharedwith, autosave='true')
def notes_view(): resource_id = request.args.get('resource_id') title = Screenplay.get_title(resource_id) notes = Note.get_by_resource_id(resource_id) output = [[n.row, n.col, json.loads(n.data), n.thread_id] for n in notes] j = json.dumps(output) # TODO: figure out correct permission here f = False # is allowed to delete threads? defaulting to NO return render_template('mobile/MobileViewNotes.html', j=j, title=title, f=f, user=current_user.name, sign_out='/user/sign-out')
def notes_delete_thread(): resource_id = request.form['resource_id'] thread_id = str(request.form['thread_id']) thread = Note.get_by_thread_id(thread_id) db.session.delete(thread) unread_notes = UnreadNote.query.filter_by(thread_id=thread_id).all() for note in unread_notes: db.session.delete(note) db.session.commit() return Response('1', mimetype='text/plain')
def notes_position(): resource_id = request.form['resource_id'] positions = request.form['positions'] now = datetime.utcnow() for row, col, thread_id in json.loads(positions): thread_id = str(thread_id) thread = Note.get_by_thread_id(thread_id) thread.row = row thread.col = col thread.updated = now db.session.commit() return Response('1', mimetype='text/plain')
def notes_new_thread(): resource_id = request.form['resource_id'] row = int(request.form['row']) col = int(request.form['col']) thread_id = str(request.form['thread_id']) content = request.form['content'] msg_id = str(datetime.utcnow()) user = get_current_user_email_with_default() if resource_id != "Demo": message = [content, user, msg_id] data = json.dumps([message]) note = Note(resource_id=resource_id, thread_id=thread_id, data=data, row=row, col=col) db.session.add(note) new_note_notification(resource_id, user, thread_id, msg_id) db.session.commit() if request.form['fromPage'] == 'mobileviewnotes': return Response('sent', mimetype='text/plain') dump = json.dumps([row, col, thread_id, msg_id, user]) return Response(dump, mimetype='text/plain')