فهرست منبع

Implemented most of the necessary functionality in the database module and added some exceptions.

Helmut Pozimski 10 سال پیش
والد
کامیت
cf8a66eadd
2فایلهای تغییر یافته به همراه125 افزوده شده و 7 حذف شده
  1. 99 7
      jwmudlib/database.py
  2. 26 0
      jwmudlib/jwmu_exceptions.py

+ 99 - 7
jwmudlib/database.py

@@ -85,16 +85,108 @@ class db:
         # fill the weekdays table with the existing 7 days of the week,
         # these are always hardcoded
         self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
-                                "name) VALUES (1, 'undefined', 'Monday')")
+                                "name) VALUES (1, 'undefined', 'Monday');")
         self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
-                                "name) VALUES (2, 'undefined', 'Tuesday')")
+                                "name) VALUES (2, 'undefined', 'Tuesday');")
         self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
-                                "name) VALUES (3, 'undefined', 'Wednesday')")
+                                "name) VALUES (3, 'undefined', 'Wednesday');")
         self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
-                                "name) VALUES (4, 'undefined', 'Thursday')")
+                                "name) VALUES (4, 'undefined', 'Thursday');")
         self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
-                                "name) VALUES (5, 'undefined', 'Friday')")
+                                "name) VALUES (5, 'undefined', 'Friday');")
         self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
-                                "name) VALUES (6, 'undefined', 'Saturday')")
+                                "name) VALUES (6, 'undefined', 'Saturday');")
         self.__ExecuteStatement("INSERT INTO weekdays (day_id, alarm_time, "
-                                "name) VALUES (7, 'undefined', 'Sunday')")
+                                "name) VALUES (7, 'undefined', 'Sunday');")
+
+    def FetchCategoryId(self, category_name):
+        """Fetches the id of a category with a given name from the database
+        and returns it.
+
+        """
+        query = "SELECT category_id FROM categories WHERE name=?;"
+        result = self.__ExecuteQuery(query, (category_name,))
+        category_id = result.fetchone()
+        try:
+            return int(category_id[0])
+        except TypeError:
+            raise jwmu_exceptions.CategoryNotFound()
+
+    def ChangeAlarmTimeWeekdays(self, alarm_time, start=None, end=None, day=None):
+        """Changes the alarm time either for a single day or a range of days,
+        like 1-5 for regular work days or 6-7 for week end. What mode is
+        chosen depends on the parameters defined when the function is called.
+
+        """
+        statement = "UPDATE weekdays SET alarm_time = ? WHERE day_id = ?;"
+        if day is not None:
+            self.__ExecuteStatement(statement, (alarm_time, day))
+        elif start is not None and end is not None:
+            for day in range(start, end+1):
+                self.__ExecuteStatement(statement, (alarm_time, day))
+        else:
+            raise jwmu_exceptions.WrongParameters()
+
+    def CreateCategory(self, category_name):
+        """Inserts a new category into the database and returns the newly
+        assigned category id.
+
+        """
+        statement = "INSERT INTO categories (name) VALUES (?);"
+        self.__ExecuteStatement(statement, (category_name,))
+        category_id = self.FetchCategoryId(category_name)
+        return category_id
+
+    def DeleteCategory(self, category_id):
+        """Deletes a category with the given ID from the database."""
+        statement = "UPDATE alarmdays SET category=NULL WHERE category = ?;"
+        self.__ExecuteStatement(statement, (category_id,))
+        statement = "DELETE FROM categories WHERE category_id = ?;"
+        self.__ExecuteStatement(statement, (category_id,))
+
+    def InsertDay(self, date, alarm_time, category_id=None):
+        """Inserts an additional day with the given parameters and optionally
+        a category into the database.
+
+        """
+        try:
+            if category_id is not None:
+                statement = "INSERT INTO alarmdays (date, alarm_time, category) " \
+                            "VALUES (? , ?, ?);"
+                self.__ExecuteStatement(statement, (date, alarm_time, category_id))
+            else:
+                statement = "INSERT INTO alarmdays (date, alarm_time) VALUES " \
+                            "(?, ?);"
+                self.__ExecuteStatement(statement, (date, alarm_time))
+        except sqlite3.IntegrityError:
+            raise jwmu_exceptions.DayAlreadyInDatabase()
+
+    def ModifyAlarmTime(self, date, alarm_time):
+        """Changes the alarm time for a given day in the database."""
+        statement = "UPDATE alarmdays SET alarm_time = ? WHERE date = ?;"
+        self.__ExecuteStatement(statement, (alarm_time, date))
+
+    def DeleteDay(self, date):
+        """Deletes a specified day from the database."""
+        statement = "DELETE FROM alarmdays WHERE date = ?;"
+        self.__ExecuteStatement(statement, (date,))
+
+    def FetchDays(self):
+        """Retrieves a list of all the days with their corresponding
+        categories from the database.
+
+        """
+        query = "SELECT date, alarmdays.alarm_time, name FROM alarmdays LEFT " \
+                "JOIN categories ON alarmdays.category=categories.category_id;"
+        result = self.__ExecuteQuery(query).fetchall()
+        return result
+
+    def FetchWeekDays(self):
+        """Retrieves a list of all the week days and their alarm times from
+        the database.
+
+        """
+        query = "SELECT * FROM weekdays;"
+        result = self.__ExecuteQuery(query).fetchall()
+        return result
+

+ 26 - 0
jwmudlib/jwmu_exceptions.py

@@ -30,3 +30,29 @@ class DataBaseWriteFailed(Exception):
 
     def __str__(self):
         return self.__message
+
+
+class WrongParameters(Exception):
+    def __init__(self):
+        self.__message = "The wrong number or values of parameters where " \
+                         "given to the function, the requested action failed."
+
+    def __str__(self):
+        return self.__message
+
+
+class CategoryNotFound(Exception):
+    def __init__(self):
+        self.__message = "The category could not be found in the database."
+
+    def __str__(self):
+        return self.__message
+
+
+class DayAlreadyInDatabase(Exception):
+    def __init__(self):
+        self.__message = "This day already exists in the database."
+
+    def __str__(self):
+        return self.__message
+