From aa63641321b42431e5284e2ef4c392f66d5439ed Mon Sep 17 00:00:00 2001 From: Jogi Hofmüller Date: Tue, 2 Aug 2011 12:07:18 +0000 Subject: - started working on user locations git-svn-id: https://svn.spreadspace.org/mur.sat@92 7de4ea59-55d0-425e-a1af-a3118ea81d4c --- tools/mmd/MmdDb.py | 31 +++++++++++++++++++++++---- tools/mmd/MmdLocation.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ tools/mmd/MmdUser.py | 14 +++++++++++-- 3 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 tools/mmd/MmdLocation.py (limited to 'tools/mmd') diff --git a/tools/mmd/MmdDb.py b/tools/mmd/MmdDb.py index d561f3f..2ef5824 100644 --- a/tools/mmd/MmdDb.py +++ b/tools/mmd/MmdDb.py @@ -12,14 +12,20 @@ class Db: self.conn.text_factory = str self.conn.row_factory = sqlite3.Row self.cursor = self.conn.cursor () + self.cursor.execute ('PRAGMA foreign_keys = ON') + self.conn.commit () def sessionFind (self, session_id): self.cursor.execute ('SELECT * FROM session WHERE id=?', (session_id,)) return self.cursor.fetchone () def sessionInit (self, session_id, email, expires, status): - self.cursor.execute ('INSERT INTO session (id, email, expires, status) VALUES (?,?,?,?)', (session_id, email, expires, status,)) - self.conn.commit () + try: + self.cursor.execute ('INSERT INTO session (id, email, expires, status) VALUES (?,?,?,?)', (session_id, email, expires, status,)) + self.conn.commit () + return True + except sqlite3.IntegrityError: + return False def sessionDelete (self, session_id): self.cursor.execute ('DELETE FROM session WHERE id=?', (session_id,)) @@ -46,9 +52,13 @@ class Db: # user related methods # def userCreate (self, user_id, firstname, lastname, email, callsign, password, code, regtimeout): - self.cursor.execute ('INSERT INTO user (id, firstname, lastname, email, callsign, password, code, regtimeout) VALUES (?,?,?,?,?,?,?,?)', + try: + self.cursor.execute ('INSERT INTO user (id, firstname, lastname, email, callsign, password, code, regtimeout) VALUES (?,?,?,?,?,?,?,?)', (user_id, firstname, lastname, email, callsign, password, code, regtimeout)) - self.conn.commit () + self.conn.commit () + return True + except sqlite3.IntegrityError: + return False def userConfirm (self, code): self.cursor.execute ('UPDATE user SET status="valid" WHERE code=?', (code, )) @@ -74,6 +84,19 @@ class Db: self.cursor.execute ('SELECT * FROM user WHERE code=? and status="pending"', (code,)) return self.cursor.fetchone () + # location related methods + def locationCreate (self, location_id, name, longitude, latitude, user_id, is_default): + try: + self.cursor.execute ('INSERT INTO location (id, name, longitude, latitude, user_id, is_default) VALUES (?,?,?,?,?,?)', (location_id, name, longitude, latitude, user_id, is_default)) + self.conn.commit () + return True + except sqlite3.IntegrityError: + return False + + def locationFindUserId (self, user_id): + self.cursor.execute ('SELECT * FROM location WHERE user_id=?', (user_id,)) + return self.cursor.fetchone () + def close (self): self.cursor.close () self.conn.close () diff --git a/tools/mmd/MmdLocation.py b/tools/mmd/MmdLocation.py new file mode 100644 index 0000000..67a1a74 --- /dev/null +++ b/tools/mmd/MmdLocation.py @@ -0,0 +1,54 @@ +""" +Location class for MURSAT1 Mission Dashboard +""" +import hashlib +from cgi import parse_qs, escape +from MmdDb import Db + +def checkLonLat (lonlat): + if type (lonlat) != type (0.0): + lonlat = float (lonlat) + if lonlat < 0.0 or lonlat > 360.0: + return False + return lonlat + +class Location: + + def __init__ (self): + self.db = Db () + + def create (self, name, longitude, latitude, is_default, user_id): + self.name = name + self.is_default = is_default + self.longitude = checkLonLat (longitude) + self.latitude = checkLonLat (latitude) + + if not self.longitude: + self.longitude = 15.44226 + if not self.latitude: + self.latitude = 47.06576 + + self.location_id = hashlib.sha1 ('{0}{1}{2}'.format (user_id, self.longitude, self.latitude)).hexdigest () + return self.db.locationCreate (self.location_id, self.name, self.longitude, self.latitude, user_id, self.is_default) + + def findUserId (self, user_id): + l = self.db.locationFindUserId (user_id) + if not l: + return False + self.name = l['name'] + self.longitude = int (l['longitude']) + self.latitude = int (l['latitude']) + self.is_default = l['is_default'] + self.location_id = l['id'] + +if __name__ == "__main__": + l = Location () + try: + assert l, 'Location instantiation failed' + assert l.create ('Graz', '14.5', '47.3', 'yes', '1'), 'Location create failed' + assert l.findUserId (1), 'Location find user by id failed' + except AssertionError as e: + print 'Test failed: {0}'.format (e) + +# vim: tw=0 ts=2 expandtab +# EOF diff --git a/tools/mmd/MmdUser.py b/tools/mmd/MmdUser.py index f429622..d9597b1 100644 --- a/tools/mmd/MmdUser.py +++ b/tools/mmd/MmdUser.py @@ -6,6 +6,7 @@ import smtplib import time from cgi import parse_qs, escape from MmdDb import Db +from MmdLocation import Location class User: @@ -17,7 +18,8 @@ class User: password_hash = hashlib.sha1 (password).hexdigest () code = hashlib.sha1 ('{0}{1}{2}{3}'.format (user_id, firstname, lastname, email)).hexdigest () regtimeout = int (time.strftime ('%s')) + (3600 * 24) - self.db.userCreate (user_id, firstname, lastname, email, callsign, password_hash, code, regtimeout) + if not self.db.userCreate (user_id, firstname, lastname, email, callsign, password_hash, code, regtimeout): + return False self.email = email self.firstname = firstname self.lastname = lastname @@ -73,7 +75,15 @@ class User: return False if __name__ == "__main__": - pass + user = User () + try: + assert user, 'no user object' + code = user.create (1, 'jogi', 'hofmueller', 'jogi@mur.at', '', 14.4, 47.2, 'blah') + assert code, 'could not create user' + assert user.load ('jogi@mur.at'), 'could not load user data from db' + print 'passed all tests' + except AssertionError as error: + print 'Test failed: {0}'.format (error) # vim: tw=0 ts=2 expandtab # EOF -- cgit v1.2.3