def _create_direcotry(self): if self._dbPath: directory = os.path.abspath(self._dbPath) directory = os.path.split(directory)[0] Common.create_dir(directory) else: Log.error('empty db path')
def all(cls): ''' Returns a list of all items in this table. ''' try: return cls.query.all() except IntegrityError as ex: Log.error(cls.__name__, str(ex)) return []
def find(cls, *criteria): ''' Returns a list of all items that match the given criteria. ''' try: return cls.query.filter(*criteria).all() except IntegrityError as ex: Log.error(cls.__name__, str(ex)) return []
def render(template, **kwargs): ''' Renders a template. Catches the template not found exception and shows the missing template error page if caught. ''' try: return render_template(template, **kwargs) except TemplateNotFound: Log.error(__name__, "Template not found: %s." % (template)) return render_template("error/template.html", name=template)
def get(cls, identifier): ''' Fetches the item with the given id from the database and returns it. Returns None if no such item exists. ''' if not identifier: return None try: return cls.query.get(identifier) except IntegrityError as ex: Log.error(cls.__name__, str(ex)) return None
def update(self): ''' Saves all changes to this item to the database. ''' try: self.changedOn = datetime.now() if not self.__batch__: db.session.commit() # @UndefinedVariable return True except IntegrityError as ex: Log.error(self.__class__.__name__, str(ex)) db.session.rollback() # @UndefinedVariable return False
def delete(self): ''' Deletes this item from the database. ''' try: db.session.delete(self) # @UndefinedVariable if not self.__batch__: db.session.commit() # @UndefinedVariable return True except IntegrityError as ex: Log.error(self.__class__.__name__, str(ex)) db.session.rollback() # @UndefinedVariable return False
def unique(cls, *criteria): ''' If there's exactly one item matching the given criteria it is returned. Otherwise None is returned. ''' try: items = cls.query.filter(*criteria).all() if (not items) or (len(items) != 1): return None return items[0] except IntegrityError as ex: Log.error(cls.__name__, str(ex)) return None
def batch(cls, function): ''' Pass a function performing database updates. Transaction is only submitted after all updates have been performed. This is faster than performing them one after another. ''' cls.__batch__ = True function() cls.__batch__ = False try: db.session.commit() # @UndefinedVariable except IntegrityError as ex: Log.error(cls.__name__, str(ex))
def create(self): ''' Adds this item to the database. ''' self.createdOn = datetime.now() self.changedOn = datetime.now() try: db.session.add(self) # @UndefinedVariable if not self.__batch__: db.session.commit() # @UndefinedVariable return True except IntegrityError as ex: Log.error(self.__class__.__name__, str(ex)) db.session.rollback() # @UndefinedVariable return False
def send(self, recipients, subject, message): ''' Sends an html email with the given message and subject to a list of recipients. ''' sender = self.username header = "From: <%s>\n" % (sender) header += "To: <%s>\n" % (", ".join(x for x in recipients)) header += "MIME-Version: 1.0\nContent-type: text/html\n" header += "Subject: %s\n\n" % (subject) try: smtp = smtplib.SMTP(self.host, self.port) if self.tls: smtp.ehlo() smtp.starttls() smtp.ehlo() smtp.login(self.username, self.password) smtp.sendmail(sender, recipients, header + message) return True except Exception as e: Log.error(self.__class__, "Could not send mail." + str(e)) return False
def _execute_sql(self, cmd, isQueury=True, commit=True): values = [] rowCount = 0 self._dbLock.acquire() try: con = self._connection cursor = con.cursor() cursor.execute(cmd) values = cursor.fetchall() rowCount = cursor.rowcount if not isQueury and commit: con.commit() except Exception as e: Log.error('db except: %s' % e) # raise e finally: cursor.close() self._dbLock.release() if isQueury: return values return rowCount