Skip to content

Commit

Permalink
added calendar for travels and stops
Browse files Browse the repository at this point in the history
  • Loading branch information
elblogbruno committed Aug 15, 2021
1 parent 083420b commit f135796
Show file tree
Hide file tree
Showing 49 changed files with 9,759 additions and 368 deletions.
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from flask_dropzone import Dropzone

app = create_app('dev')
dropzone = Dropzone(app)
droppzone = Dropzone(app)



Expand Down
169 changes: 163 additions & 6 deletions controller/models/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from werkzeug.security import generate_password_hash, check_password_hash
import db
from sqlalchemy import Column, Boolean, String, Float, DateTime, Text, Date, Time, desc
from datetime import datetime
# from datetime import datetime
import os
import json
import shutil
import uuid
from datetime import *

def datetime_parser(o):
if isinstance(o, datetime):
Expand All @@ -21,6 +23,7 @@ class Wanderpi(db.Base):
file_path = Column(String(256), nullable=False)
created_date = Column(DateTime, default=datetime.utcnow)
travel_id = Column(String(256), nullable=False)
stop_id = Column(String(256), nullable=False)
is_image = Column(Boolean)
has_been_edited = Column(Boolean, default=False)

Expand Down Expand Up @@ -81,8 +84,8 @@ class Travel(db.Base):
created_date = Column(DateTime, default=datetime.utcnow)
start_date = Column(Date)
end_date = Column(Date)
notes = Column(String(256), nullable=False)


def __repr__(self):
return f'<User {self.id}>'

Expand All @@ -98,6 +101,7 @@ def save(self):
db.session.commit()
self.save_json()


def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
# this functions gets the object as a dictionary and saves it to a json file
Expand All @@ -108,6 +112,15 @@ def save_json(self):
with open(json_file_path, 'w') as f:
json.dump(self.as_dict(), f, default = datetime_parser)

def delete_all_stops(self):
stops = db.session.query(Stop).filter(Stop.travel_id == self.id).all()

for stop in stops:
stop.delete()

db.session.commit()
return True

def delete_all_wanderpis(self):
videos = db.session.query(Wanderpi).filter(Wanderpi.travel_id == self.id).all()

Expand All @@ -117,22 +130,41 @@ def delete_all_wanderpis(self):
db.session.commit()
return True

def get_all_notes(self):
return db.session.query(Note).filter(Note.travel_id == self.id).all()

def get_all_wanderpis(self, filter=None):
if filter:
is_image = (filter == 'image')
return db.session.query(Wanderpi).filter(Wanderpi.travel_id == self.id, Wanderpi.is_image == is_image).all()
else:
return db.session.query(Wanderpi).filter(Wanderpi.travel_id == self.id).all()

def delete(self, id):
def get_all_stops(self):
return db.session.query(Stop).filter(Stop.travel_id == self.id).all()

def delete(self):
if os.path.isdir(self.travel_folder_path):
print("Removing folder from disk")
shutil.rmtree(self.travel_folder_path, ignore_errors=True)

db.session.query(Travel).filter(Travel.id == id).delete()
db.session.query(Travel).filter(Travel.id == self.id).delete()
db.session.commit()
return True

def init_calendar(self):
sdate = self.start_date
edate = self.end_date
calendar_days = [sdate+timedelta(days=x) for x in range((edate-sdate).days)]

print(calendar_days)
if len(calendar_days) == 0:
calendar_days.append(sdate)

for day in calendar_days:
note_id = str(uuid.uuid4())
note = Note(id=note_id, travel_id=self.id, content="No content", total_price=0.0, day=day)
note.save()

@staticmethod
def get_by_id(id):
Expand All @@ -149,7 +181,6 @@ class Point(db.Base):
lat = Column(String(256), nullable=False)
long = Column(String(256), nullable=False)


def __repr__(self):
return f'<Point {self.id}>'

Expand Down Expand Up @@ -178,4 +209,130 @@ def get_by_id(id):

@staticmethod
def get_all():
return db.session.query(Point).all()
return db.session.query(Point).all()

class Stop(db.Base):
__tablename__ = 'stops'
id = Column(String(256), primary_key=True)
travel_id = Column(String(256), nullable=False)
lat = Column(String(256), nullable=False)
long = Column(String(256), nullable=False)
name = Column(String(256), nullable=False)


def __repr__(self):
return f'<Stop {self.id}>'

def delete(self):
db.session.query(Stop).filter(Stop.id == self.id).delete()
db.session.commit()
return True

def save(self):
db.session.add(self)
db.session.commit()
#self.save_json()

def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}

def get_all_wanderpis(self, filter=None):
if filter:
is_image = (filter == 'image')
return db.session.query(Wanderpi).filter(Wanderpi.stop_id == self.id, Wanderpi.is_image == is_image).all()
else:
return db.session.query(Wanderpi).filter(Wanderpi.stop_id == self.id).all()


@staticmethod
def get_by_id(id):
return db.session.query(Stop).get(id)

@staticmethod
def get_all():
return db.session.query(Stop).all()


class Note(db.Base):
__tablename__ = 'notes'
id = Column(String(256), primary_key=True)
travel_id = Column(String(256), nullable=False)
content = Column(String(256), nullable=False)
total_price = Column(Float, nullable=False)
day = Column(Date, nullable=False)
creation_date = Column(DateTime, nullable=False, default=datetime.utcnow)

def __repr__(self):
return f'<Note {self.id}>'


def set_id(self, file_owner_id):
self.file_owner_id = file_owner_id

def save(self):
db.session.add(self)
db.session.commit()
#self.save_json()

def get_all_input(self, filter=None):
return db.session.query(MoneyInput).filter(MoneyInput.note_id == self.id).all()

def get_all_input_as_json(self):
return json.dump(db.session.query(MoneyInput).filter(MoneyInput.note_id == self.id).all(), default= datetime_parser)

def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}

@staticmethod
def get_by_id(id):
return db.session.query(Note).get(id)

@staticmethod
def get_all():
return db.session.query(Note).all()


class MoneyInput(db.Base):
__tablename__ = 'input'
id = Column(String(256), primary_key=True)
note_id = Column(String(256), nullable=False)
name = Column(String(256), nullable=False)
hour = Column(Time, nullable=False)
value = Column(Float, nullable=False)

# def __repr__(self):
# return f'<MoneyInput {self.id}>'

def set_id(self, note_id):
self.note_id = note_id

def save(self):
db.session.add(self)
db.session.commit()
#self.save_json()

def as_json(self):
return {
"id": self.id,
"note_id":self.note_id,
"name": self.name,
"hour": self.hour.isoformat(),
"value": self.value
}

def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}

@staticmethod
def get_by_id(id):
return db.session.query(MoneyInput).get(id)

@staticmethod
def get_all():
return db.session.query(MoneyInput).all()

@staticmethod
def delete(id):
db.session.query(MoneyInput).filter(MoneyInput.id == id).delete()
db.session.commit()
return True
Loading

0 comments on commit f135796

Please sign in to comment.