def _is_in_range_from_ssid_list(self, ssid_list, my_ssids): ssid_list = self._parse_ssid_location_from_json(ssid_list) for ssid in my_ssids: if ssid in ssid_list: logging.debug('\t* {} is in {}'.format(ssid, ssid_list)) return True logging.debug('\t* {} is not in list {}'.format(ssid, ssid_list)) return False
def add_location(self, username, token, name, is_gps, location_json): author = self.get_user(username) logging.debug('Adding location "{}" is_gps: {},' 'author: {}'.format(name, is_gps, author)) loc = Location(author=author, name=name, is_gps=is_gps, location=location_json) return loc
def get_ssid_messsages(self, args): # {username, token, my_ssids} logging.debug('### get_ssid_messsages() ###') username, token = self._parse_auth(args) my_ssids = args['my_ssids'] msgs = lm.get_available_messages_by_ssid(username, token, my_ssids) json_msgs = [json.dumps(self._msg_to_json_dict(msg)) for msg in msgs] msgs_dict = { 'messages': json_msgs, } self._respond_json(msgs_dict)
def get_available_messages_by_gps(self, username, token, curr_coord): """ Gets a list of available messages for the user in the provided location. Args: curr_coord - tuple of (latitude, longitude) of the current user's location. """ logging.debug('Request for available messages by GPS corrdinates') msgs = self._get_messages_in_range(curr_coord) msgs = self._filter_messages_by_properties(msgs, username, token) return msgs
def get_key_value_bin(self, username, token): """ Returns the key-value dictionary for this user. """ logging.debug('### get_key_value_bin ###') u = User.get(username=username) kvp = u.kvp if kvp is None: logging.warning('\tKVP is None for user {}'.format(username)) return {} else: kvp = json.loads(kvp) return kvp
def do_GET(self): logging.debug(self.path) logging.debug(self.headers['content']) path = self.path json_content = self.headers['content'] json_dict = json.loads(json_content) logging.debug('Loaded json: ') logging.debug(json_dict)
def add_message(self, args): # { # username, token, title, location_name, text, is_centralized, # is_black_list, properties, valid_from?, valid_until? # } logging.info("### add_message ###") username, token = self._parse_auth(args) title = args['title'] location_name = args['location_name'] location = None with db_session: location = lm.get_location_by_name(username, token, location_name) text = args['text'] is_centralized = args['is_centralized'] is_black_list = args['is_black_list'] properties = args['properties'] valid_from = None try: valid_from = args['valid_from'] logging.debug("\tRaw valid_from: {}".format(valid_from)) valid_from = dateutil.parser.parse(valid_from) logging.debug("\tParsed valid_from: {}".format(valid_from)) except KeyError as e: pass valid_until = None try: valid_until = args['valid_until'] valid_until = dateutil.parser.parse(valid_until) except KeyError as e: pass lm.add_message(username, token, title=title, location=location, text=text, is_centralized=is_centralized, is_black_list=is_black_list, valid_from=valid_from, valid_until=valid_until, properties=properties) self._send_OK_headers()
def delete_message(self, username, password, msg_id): msg_obj = Message.get(id=msg_id) if not msg_obj: # if no message with such id found, just ignore logging.warning( '[!!] Request to delete message with id {},' ' which does NOT exist. Ignoring request...'.format(msg_id)) return requestor = User.get(username=username) if msg_obj.author != requestor: raise AuthorizationError( 'User \'{}\' cannot delete message with ' 'id {}, since that message was created by \'{}\''.format( username, msg_id, msg_obj.author)) msg_obj.delete() logging.debug('Deleted message with id {}'.format(msg_id))
def delete_location(self, username, password, location_name): location = Location.get(name=location_name) if not location: # if no message with such id found, just ignore logging.warning( '[!!] Request to delete location with name {},' ' which does NOT exist. Ignoring request...'.format( location_name)) return requestor = User.get(username=username) if location.author != requestor: raise AuthorizationError( 'User \'{}\' cannot delete location with ' 'name {}, since that location was created by \'{}\''.format( username, location_name, location.author)) location.delete() logging.debug('Deleted location with name {}'.format(location_name))
def add_message(self, username, token, title, location, text, is_centralized=True, is_black_list=True, valid_from=None, valid_until=None, properties=None): """ Adds a new message. """ if valid_from is None: valid_from = datetime.now() if valid_until is None: valid_until = datetime.max if properties is None: properties = json.dumps({}) author = None with db_session: author = self.get_user(username=username) logging.debug('Adding message "{}" for user {}'.format(title, author)) msg = None with db_session: msg = Message(author=author, title=title, location=location, text=text, is_centralized=is_centralized, is_black_list=is_black_list, valid_from=valid_from, valid_until=valid_until, properties=properties) return msg
def get_value(self, username, token, key): """ Returns the value indexed by the specified key. If there is no entry indexed by that key, None is returned. """ logging.debug('### get_value() ###') u = User.get(username=username) kvp = u.kvp if kvp is None: logging.warning('\tKVP is None for user {}'.format(username)) return None else: kvp = json.loads(kvp) try: logging.debug('\tGetting value for key {} for user {}'.format( key, username)) value = kvp[key] logging.debug('\t\tValue for key {} is {}'.format(key, value)) return value except KeyError: # key-value entry does not exist, return None logging.warning('\t\t[!!]Key {} not found'.format(key)) return None
def update_key(self, username, token, key, value): """ Updates or adds (if does not exist) the key-value pair to the user's profile. To remove a key, simply update it with the None value. """ logging.debug('### update_key() ###') u = User.get(username=username) kvp = u.kvp if kvp is None: logging.warning( '\tKVP for user "{}" is None, instantiating'.format(username)) kvp = {} else: kvp = json.loads(kvp) logging.debug('\tAssociating {}:{} for user {}'.format( key, value, username)) kvp[key] = value u.kvp = json.dumps(kvp)
def delete_key(self, username, token, key): """ Deltes the key-value pair with the provided key from the user's profile. If a pair with that key does not exist, it's ignored. """ logging.debug('### delete_key() ###') u = User.get(username=username) kvp = u.kvp if kvp is None: logging.warning('\tKVP is None for user {}'.format(username)) return else: kvp = json.loads(kvp) try: logging.debug('\tDeleting key {} for user {}'.format( key, username)) del kvp[key] u.kvp = json.dumps(kvp) except KeyError: # all good: deleting a non-existing key, just ignore logging.debug('\t\t[!!] Key {} was non-existent'.format(key)) pass
def do_POST(self): logging.debug(self.path) logging.debug(self.headers['content']) path = self.path json_content = self.rfile.read(int(self.headers['Content-Length'])) logging.debug('Raw Content') logging.debug(json_content) json_dict = json.loads(json_content) logging.debug('Loaded json: ') logging.debug(json_dict) dispatch_function = self.RES_FUNC.get(self.path, self.invalid_endpoint_err) dispatch_function(json_dict)
def _is_in_range_from_json_coord(self, json_coord, curr_coord): loc_coord, radius = self._parse_location_from_json(json_coord) logging.debug('Parsed location: ' 'latitude={}, longitude={}, radius={}'.format( loc_coord[0], loc_coord[1], radius)) return self._is_in_range(loc_coord, curr_coord, radius)
def get_available_messages_by_ssid(self, username, token, my_ssids): logging.debug('Request for available messages by SSID list') logging.debug('\t My SSID List: {}'.format(my_ssids)) msgs = self._get_messages_in_ssid_range(my_ssids) msgs = self._filter_messages_by_properties(msgs, username, token) return msgs
def get_location_by_name(self, username, token, name): logging.debug('Getting location "{}" by name'.format(name)) return Location.get(name=name)