summaryrefslogtreecommitdiff
path: root/tools/mmd/MmdUser.py
blob: c5947feddba40618270b08d0babf2b2b912217ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""
User class for MURSAT1 Mission Dashboard
"""
import hashlib
import smtplib
import time
from cgi import parse_qs, escape
from MmdDb import Db
from MmdLocationList import LocationList

class User:

  def __init__ (self, email = 'Anonymous'):
    self.db = Db ()
    self.email = email
    self.location_list = LocationList ()

  def create (self, user_id, firstname, lastname, email, callsign, password):
    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)
    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
    self.user_id = user_id
    return code

  def addLocation (self, name, longitude, latitude, altitude, is_default):
    self.location_list = LocationList (self.user_id)
    return self.location_list.addLocation (name, longitude, latitude, altitude, is_default)

  def findPending (self, code):
    u = self.db.userFindPending (code)
    if not u:
      return False
    if int (u['regtimeout']) < int (time.strftime ('%s')):
      return False
    return True

  def confirm (self, code):
    u = self.db.userFindPending (code)
    if not u:
      return False
    if int (u['regtimeout']) < int (time.strftime ('%s')):
      return False
    self.db.userConfirm (code)
    self.email = u['email']
    self.firstname = u['firstname']
    self.lastname = u['lastname']
    return True

  def load (self, email = False):
    if email:
      self.email = email
    u = self.db.userFindEmail (self.email)
    if not u:
      return False
    self.user_id = u['id']
    self.firstname = u['firstname']
    self.lastname = u['lastname']
    self.password = u['password']
    self.location_list = LocationList (self.user_id)
    return True

  def checkPassword (self, password):
    if hashlib.sha1 (password).hexdigest () == self.password:
      return True
    return False

  def cancel (self, code):
    self.deleteLocationList ()
    self.db.userCancel (code)

  def deleteLocationList (self):
    self.location_list.delete ()

  def getDefaultLocation (self):
    return self.location_list.getDefaultLocation ()

  def sendEmail (self, message):
    server = smtplib.SMTP ('localhost')
    try:
      server.sendmail ('noreply@mur.at', self.email, message)
      server.quit ()
      return True
    except (smtplib.SMTPRecipientsRefused, smtplib.SMTPHeloError, smtplib.SMTPSenderRefused, smtplib.SMTPDataError):
      server.quit ()
      return False

if __name__ == "__main__":
  user = User ()
  email = 'jfh@mur.at'
  try:
    assert user, 'no user object'
    code = user.create (1, 'jogi', 'hofmueller', email, '', 'blah')
    assert code, 'could not create user'
    assert user.load (email), 'could not load user data from db'
    print 'User:', user.firstname, user.lastname, user.email
    assert user.addLocation ('Graz', '14.4', '47.2', '376', 'yes'), 'could not add location'
    location = user.getDefaultLocation ()
    assert location, 'could not get default location'
    print 'Location:', location.name, location.longitude, location.latitude, location.is_default
    user.cancel (code)
    print 'passed all tests and removed objects from database'
  except AssertionError as error:
    print 'Test failed: {0}'.format (error)

# vim: tw=0 ts=2 expandtab
# EOF