def number(self): with sqlite3.connect(self.db_name) as connection: self.__ensure_table(connection) version = connection.execute( *compile(Q(T(self.table)).fields('number'))).fetchone()[0] return version
def sync(self, folder: str) -> None: with sqlite3.connect(self.db_name) as connection: row = connection.execute(*compile( Q(T.icalendar).fields( '*').where(T.icalendar.user == self.user and T.icalendar.url == self.url))).fetchone() url = row[1] sync_dir = path.join(folder, self.user, 'ics') if not path.exists(sync_dir): makedirs(sync_dir) IcsCalendar( url, SimpleCalEventsIntervals( tz.tzlocal(), FileTimeIntervals( path.join(sync_dir, urllib.parse.quote(self.url, safe=''))))).sync() connection.execute( *Q(T.icalendar, result=Result( compile=compile)).where(T.icalendar.user == self.user and T.icalendar.url == self.url). update({T.icalendar.sync_time: int(round(time.time() * 1000))}))
def as_html(self, sync_url: typing.Callable[[str], str]) -> str: result = '' with sqlite3.connect(self.db_name) as connection: row = connection.execute(*compile( Q(T.icalendar).fields( '*').where(T.icalendar.user == self.user and T.icalendar.url == self.url))).fetchone() try: sync = datetime.datetime.fromtimestamp(row[3] / 1000) except OSError: sync = datetime.datetime.min name = row[2] url = row[1] delta: datetime.timedelta = datetime.datetime.now() - sync if delta.seconds > 60: sync_html = f'<mark>Sync was at {str(sync)}</mark>' else: sync_html = f'<code>Sync was at {str(sync)}</code>' result = f''' <h2>{name}</h2> <span><strong>Url: </strong>{url}</span></br> {sync_html}</br> <span><a href="{sync_url(url)}">Sync it now!</a></span></br> ''' return result
def user(self, token: str) -> User: user = '' with sqlite3.connect(self.db_name) as connection: row = connection.execute(*compile( Q(T.session).fields('user').where( T.session.token == token))).fetchone() user = row[0] return DbUser(self.db_name, user)
def contains(self, email): # type: (str) -> bool contains = False with sqlite3.connect(self.db_name) as connection: info = connection.execute( *compile(Q(T.user).fields('*').where(T.user.email == email)) ).fetchone() if info: contains = True return contains
def __ensure_table(self, connection: Connection) -> None: info = connection.execute(*compile( Q(T.sqlite_master).fields( '*').where((T.sqlite_master.name == 'version') & (T.sqlite_master.type == 'table')))).fetchone() if not info: connection.execute( 'CREATE TABLE version (number INTEGER NOT NULL)') connection.execute( *Q(T(self.table), result=Result( compile=compile)).insert({T(self.table).number: 0}))
def as_html(self) -> str: result = '' with sqlite3.connect(self.db_name) as connection: rows = connection.execute(*compile( Q(T.slack).fields('*').where( T.slack.user == self.user))).fetchall() for row in rows: # user = row[0] team = row[1] # token = row[2] # profile = row[3] # busy_text = row[4] # busy_emoji = row[5] # available_text = row[6] # available_emoji = row[7] result += f'<li><strong>Slack:</strong> <code>{team}</code></li>\n' return f'<ul>\n{result}</ul>'
def as_html(self, url: typing.Callable[[str], str]) -> str: result = '' with sqlite3.connect(self.db_name) as connection: rows = connection.execute( *compile( Q(T.icalendar).fields('*').where(T.icalendar.user == self.user) ) ).fetchall() result = '<ul>{0}</ul>'.format( '\n'.join( map( lambda row: '<li><a href="{1}">{0}</a></li>'.format( row[2], url(row[1]) ), rows ) ) ) return result