# Path: HPCStats/Exceptions.py
# class HPCStatsDBIntegrityError(HPCStatsException):
# 
#     """Class for DB intregrity errors exceptions in HPCStats"""
# 
#     def __init__(self, msg):
# 
#         super(HPCStatsDBIntegrityError, self).__init__(msg)
# 
# class HPCStatsRuntimeError(HPCStatsException):
# 
#     """Class for runtime errors exceptions in HPCStats"""
# 
#     def __init__(self, msg):
# 
#         super(HPCStatsRuntimeError, self).__init__(msg)
# 
# Path: HPCStats/Model/User.py
# class User(object):
# 
#     """Model class for the Userhpc table."""
# 
#     def __init__(self, login, firstname, lastname, department, user_id=None):
# 
#         self.user_id = user_id
#         self.login = login
#         self.lastname = lastname
#         self.firstname = firstname
#         self.department = department
# 
#     def __str__(self):
# 
#         return str(self.firstname) + " " + str(self.lastname) \
#                + " [" + str(self.department) + "] " + str(self.login)
# 
#     def __eq__(self, other):
# 
#         return self.login == other.login
# 
#     def find(self, db):
#         """Search the User in the database based on its login. If exactly
#            one user matches in database, set user_id attribute properly
#            and returns its value. If more than one user matches, raises
#            HPCStatsDBIntegrityError. If no user is found, returns None.
#         """
# 
#         req = """
#                 SELECT userhpc_id
#                   FROM Userhpc
#                  WHERE userhpc_login = %s
#               """
#         params = ( self.login, )
#         db.execute(req, params)
#         nb_rows = db.cur.rowcount
#         if nb_rows == 0:
#             logger.debug("user %s not found in DB", str(self))
#             return None
#         elif nb_rows > 1:
#             raise HPCStatsDBIntegrityError(
#                     "several user_id found in DB for user %s" \
#                       % (str(self)))
#         else:
#             self.user_id = db.cur.fetchone()[0]
#             logger.debug("user %s found in DB with id %d",
#                           str(self),
#                           self.user_id )
#             return self.user_id
# 
#     def save(self, db):
#         """Insert User in database. You must make sure that the User does
#            not already exist in database yet (typically using User.find()
#            method else there is a risk of future integrity errors because of
#            duplicated users. If user_id attribute is set, it raises
#            HPCStatsRuntimeError.
#         """
# 
#         if self.user_id is not None:
#             raise HPCStatsRuntimeError(
#                     "could not insert user %s since already existing in "\
#                     "database" \
#                       % (str(self)))
# 
#         req = """
#                 INSERT INTO Userhpc ( userhpc_login,
#                                       userhpc_firstname,
#                                       userhpc_name,
#                                       userhpc_department)
#                 VALUES ( %s, %s, %s, %s)
#                 RETURNING userhpc_id
#               """
#         params = ( self.login,
#                    self.firstname,
#                    self.lastname,
#                    self.department )
# 
#         #print db.cur.mogrify(req, params)
#         db.execute(req, params)
#         self.user_id = db.cur.fetchone()[0]
# 
#     def update(self, db):
#         """Update User firstname, lastname and department fields in database.
#            Raises HPCStatsRuntimeError is user_id is None.
#         """
# 
#         if self.user_id is None:
#             raise HPCStatsRuntimeError(
#                     "could not update user %s since not found in database" \
#                       % (str(self)))
# 
#         req = """
#                 UPDATE Userhpc
#                    SET userhpc_firstname = %s,
#                        userhpc_name = %s,
#                        userhpc_department = %s
#                 WHERE userhpc_id = %s
#               """
#         params = ( self.firstname,
#                    self.lastname,
#                    self.department,
#                    self.user_id )
# 
#         #print db.cur.mogrify(req, params)
#         db.execute(req, params)


import logging
from HPCStats.Exceptions import HPCStatsDBIntegrityError, HPCStatsRuntimeError
from HPCStats.Model.User import User
            deletion_date = self.deletion_date.strftime('%Y-%m-%d')

        return self.user.login \
               + " (" + str(self.uid) + "|" + str(self.gid) + "): " \
               + creation_date + "/" + deletion_date

    def __eq__(self, other):

        return self.cluster.name == other.cluster.name and \
               self.user.login == other.user.login

    def existing(self, db):
        """Returns True if the account already exists in database (same cluster
           and user), or False if not.
        """

        req = """
                SELECT account_uid
                  FROM Account
                 WHERE userhpc_id = %s
                   AND cluster_id = %s
              """
        params = ( self.user.user_id,
                   self.cluster.cluster_id )
        db.execute(req, params)
        nb_rows = db.cur.rowcount
        if nb_rows == 0:
            logger.debug("account %s not found in DB", str(self))
            self.exists = False
        elif nb_rows > 1:

# Path: HPCStats/Exceptions.py
# class HPCStatsDBIntegrityError(HPCStatsException):
# 
#     """Class for DB intregrity errors exceptions in HPCStats"""
# 
#     def __init__(self, msg):
# 
#         super(HPCStatsDBIntegrityError, self).__init__(msg)
# 
# class HPCStatsRuntimeError(HPCStatsException):
# 
#     """Class for runtime errors exceptions in HPCStats"""
# 
#     def __init__(self, msg):
# 
#         super(HPCStatsRuntimeError, self).__init__(msg)
# 
# Path: HPCStats/Model/User.py
# class User(object):
# 
#     """Model class for the Userhpc table."""
# 
#     def __init__(self, login, firstname, lastname, department, user_id=None):
# 
#         self.user_id = user_id
#         self.login = login
#         self.lastname = lastname
#         self.firstname = firstname
#         self.department = department
# 
#     def __str__(self):
# 
#         return str(self.firstname) + " " + str(self.lastname) \
#                + " [" + str(self.department) + "] " + str(self.login)
# 
#     def __eq__(self, other):
# 
#         return self.login == other.login
# 
#     def find(self, db):
#         """Search the User in the database based on its login. If exactly
#            one user matches in database, set user_id attribute properly
#            and returns its value. If more than one user matches, raises
#            HPCStatsDBIntegrityError. If no user is found, returns None.
#         """
# 
#         req = """
#                 SELECT userhpc_id
#                   FROM Userhpc
#                  WHERE userhpc_login = %s
#               """
#         params = ( self.login, )
#         db.execute(req, params)
#         nb_rows = db.cur.rowcount
#         if nb_rows == 0:
#             logger.debug("user %s not found in DB", str(self))
#             return None
#         elif nb_rows > 1:
#             raise HPCStatsDBIntegrityError(
#                     "several user_id found in DB for user %s" \
#                       % (str(self)))
#         else:
#             self.user_id = db.cur.fetchone()[0]
#             logger.debug("user %s found in DB with id %d",
#                           str(self),
#                           self.user_id )
#             return self.user_id
# 
#     def save(self, db):
#         """Insert User in database. You must make sure that the User does
#            not already exist in database yet (typically using User.find()
#            method else there is a risk of future integrity errors because of
#            duplicated users. If user_id attribute is set, it raises
#            HPCStatsRuntimeError.
#         """
# 
#         if self.user_id is not None:
#             raise HPCStatsRuntimeError(
#                     "could not insert user %s since already existing in "\
#                     "database" \
#                       % (str(self)))
# 
#         req = """
#                 INSERT INTO Userhpc ( userhpc_login,
#                                       userhpc_firstname,
#                                       userhpc_name,
#                                       userhpc_department)
#                 VALUES ( %s, %s, %s, %s)
#                 RETURNING userhpc_id
#               """
#         params = ( self.login,
#                    self.firstname,
#                    self.lastname,
#                    self.department )
# 
#         #print db.cur.mogrify(req, params)
#         db.execute(req, params)
#         self.user_id = db.cur.fetchone()[0]
# 
#     def update(self, db):
#         """Update User firstname, lastname and department fields in database.
#            Raises HPCStatsRuntimeError is user_id is None.
#         """
# 
#         if self.user_id is None:
#             raise HPCStatsRuntimeError(
#                     "could not update user %s since not found in database" \
#                       % (str(self)))
# 
#         req = """
#                 UPDATE Userhpc
#                    SET userhpc_firstname = %s,
#                        userhpc_name = %s,
#                        userhpc_department = %s
#                 WHERE userhpc_id = %s
#               """
#         params = ( self.firstname,
#                    self.lastname,
#                    self.department,
#                    self.user_id )
# 
#         #print db.cur.mogrify(req, params)
#         db.execute(req, params)


# Path: HPCStats/Model/Account.py
import logging
from HPCStats.Exceptions import HPCStatsDBIntegrityError, HPCStatsRuntimeError
from HPCStats.Model.User import User

            deletion_date = self.deletion_date.strftime('%Y-%m-%d')

        return self.user.login \
               + " (" + str(self.uid) + "|" + str(self.gid) + "): " \
               + creation_date + "/" + deletion_date

    def __eq__(self, other):

        return self.cluster.name == other.cluster.name and \
               self.user.login == other.user.login

    def existing(self, db):
        """Returns True if the account already exists in database (same cluster
           and user), or False if not.
        """

        req = """
                SELECT account_uid
                  FROM Account
                 WHERE userhpc_id = %s
                   AND cluster_id = %s
              """
        params = ( self.user.user_id,
                   self.cluster.cluster_id )
        db.execute(req, params)
        nb_rows = db.cur.rowcount
        if nb_rows == 0:
            logger.debug("account %s not found in DB", str(self))
            self.exists = False
        elif nb_rows > 1:

            raise HPCStatsDBIntegrityError(