Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions flask/app/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,58 @@ class CellAlteration(Event):
def __str__(self):
return f"Cell Alteration Event (id: {self.id}), [{self.alteration_type}], cell : {self.cell_id}, notebook : {self.notebook_id}"

class CopyPasteType(enum.Enum):
CELL = "cell"
CLIPBOARD = "clipboard"

# Copy Paste Event

class CopyPasteEvent(Event):
__tablename__ = 'CopyPasteEvent'

id = db.Column(db.Integer, db.ForeignKey('Event.id', ondelete="CASCADE"), primary_key=True)
cell_id = db.Column(db.String(100), nullable=False)
time = db.Column(db.DateTime, nullable=False)
content = db.Column(db.String(300), nullable=False) # TODO DECIDE MAX LENGTH
copy_paste_type = db.Column(db.Enum(CopyPasteType), nullable=False)

__mapper_args__ = {
'polymorphic_identity': 'CopyPasteEvent'
}

def __str__(self):
return f"Copy/Paste Event (id: {self.id}), type : {self.copy_paste_type}, notebook : {self.notebook_id}, cell : {self.cell_id}, content : {self.content}, (copied notebook : {self.copied_notebook_id}, copied cell : {self.copied_cell_id},)"

class CopyEvent(CopyPasteEvent):
__tablename__ = 'CopyEvent'

id = db.Column(db.Integer, db.ForeignKey('CopyPasteEvent.id', ondelete="CASCADE"), primary_key=True)

__mapper_args__ = {
'polymorphic_identity': 'CopyEvent'
}

def __str__(self):
return f"Copy Event (id: {self.id}), type : {self.copy_paste_type}, notebook : {self.notebook_id}, cell : {self.cell_id}, content : {self.content}"

class PasteEvent(CopyPasteEvent):
__tablename__ = 'PasteEvent'

id = db.Column(db.Integer, db.ForeignKey('CopyPasteEvent.id', ondelete="CASCADE"), primary_key=True)
copied_notebook_id = db.Column(db.String(100), nullable=True)
copied_cell_id = db.Column(db.String(100), nullable=True)
copied_time = db.Column(db.DateTime, nullable=True)

__mapper_args__ = {
'polymorphic_identity': 'PasteEvent'
}

def __str__(self):
if self.copy_paste_type == CopyPasteType.CLIPBOARD:
return f"Paste Event (id: {self.id}), type : {self.copy_paste_type}, notebook : {self.notebook_id}, cell : {self.cell_id}, content : {self.content}"
return f"Paste Event (id: {self.id}), type : {self.copy_paste_type}, notebook : {self.notebook_id}, cell : {self.cell_id}, content : {self.content}, (copied notebook : {self.copied_notebook_id}, copied cell : {self.copied_cell_id})"


# Notebook registration

class Notebook(db.Model):
Expand Down
98 changes: 96 additions & 2 deletions flask/app/views/send.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from flask import Blueprint, request, jsonify
import datetime
from app import db, socketio
from app.models.models import CellExecution, CellClickEvent, ConnectionType, NotebookClickEvent, CellAlteration, Notebook
from app.models.models import CellExecution, CellClickEvent, ConnectionType, NotebookClickEvent, CellAlteration, Notebook, CopyPasteType, CopyEvent, PasteEvent
from app.utils.constants import MAX_PAYLOAD_SIZE
from app.utils.cache import check_refresh_cache
from app.utils.utils import hash_user_id_with_salt
Expand Down Expand Up @@ -174,4 +174,98 @@ def postAlterEvent():
except Exception as e:
db.session.rollback()
return f'An error occurred: {str(e)}', 500


@send_bp.route('/copyevent/cell', methods=['POST'])
def postCellCopyEvent():
data = request.get_json()
hashed_user_id = hash_user_id_with_salt(data['user_id'])

try:
new_copy_event = CopyEvent(
notebook_id=data["notebook_id"],
user_id=hashed_user_id,
cell_id=data["cell_id"],
time=datetime.datetime.strptime(data["time"],'%Y-%m-%dT%H:%M:%S.%f%z'),
content=data["content"],
copy_paste_type=CopyPasteType.CELL,
)

db.session.add(new_copy_event)
db.session.commit()
return jsonify('Cell Copy OK')

except Exception as e:
db.session.rollback()
return f'An error occurred: {str(e)}', 500

@send_bp.route('/pasteevent/cell', methods=['POST'])
def postCellPasteEvent():
data = request.get_json()
hashed_user_id = hash_user_id_with_salt(data['user_id'])

try:
new_paste_event = PasteEvent(
notebook_id=data["notebook_id"],
user_id=hashed_user_id,
copied_notebook_id=data["copied_notebook_id"],
copied_cell_id=data["copied_cell_id"],
copied_time=datetime.datetime.strptime(data["copied_time"],'%Y-%m-%dT%H:%M:%S.%f%z'),
cell_id=data["cell_id"],
time=datetime.datetime.strptime(data["time"],'%Y-%m-%dT%H:%M:%S.%f%z'),
content=data["content"],
copy_paste_type=CopyPasteType.CELL,
)

db.session.add(new_paste_event)
db.session.commit()
return jsonify('Cell Paste OK')

except Exception as e:
db.session.rollback()
return f'An error occurred: {str(e)}', 500

@send_bp.route('/copyevent/clipboard', methods=['POST'])
def postClipboardCopyEvent():
data = request.get_json()
hashed_user_id = hash_user_id_with_salt(data['user_id'])

try:
new_copy_event = CopyEvent(
notebook_id=data["notebook_id"],
user_id=hashed_user_id,
cell_id=data["cell_id"],
time=datetime.datetime.strptime(data["time"],'%Y-%m-%dT%H:%M:%S.%f%z'),
content=data["content"],
copy_paste_type=CopyPasteType.CLIPBOARD,
)

db.session.add(new_copy_event)
db.session.commit()
return jsonify('Clipboard Copy OK')

except Exception as e:
db.session.rollback()
return f'An error occurred: {str(e)}', 500

@send_bp.route('/pasteevent/clipboard', methods=['POST'])
def postClipboardPasteEvent():
data = request.get_json()
hashed_user_id = hash_user_id_with_salt(data['user_id'])

try:
new_paste_event = PasteEvent(
notebook_id=data["notebook_id"],
user_id=hashed_user_id,
cell_id=data["cell_id"],
time=datetime.datetime.strptime(data["time"],'%Y-%m-%dT%H:%M:%S.%f%z'),
content=data["content"],
copy_paste_type=CopyPasteType.CLIPBOARD,
)

db.session.add(new_paste_event)
db.session.commit()
return jsonify('Clipboard Paste OK')

except Exception as e:
db.session.rollback()
return f'An error occurred: {str(e)}', 500