summaryrefslogtreecommitdiff
path: root/tools/mmd/MmdLocation.py
blob: 67a1a74c64aacf940c0abc13d833e0d604cc890d (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
"""
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