Changeset - 6ac5ee600993
[Not reviewed]
default
0 1 0
Dennis Fink - 9 years ago 2016-03-22 12:39:53
dennis.fink@c3l.lu
Fix some stuff
1 file changed with 8 insertions and 8 deletions:
0 comments (0 inline, 0 general)
spaceapi/utils.py
Show inline comments
 
@@ -53,110 +53,110 @@ class ActiveStatus(Singleton, dict):
 
            json.dump(last_state, f, sort_keys=True)
 

	
 
    def add_user_present(self, username):
 
        if self['state']['open']:
 
            if 'people_now_present' not in self['sensors']:
 
                self['sensors']['people_now_present'] = [{'value': 0}]
 

	
 
            people_now_present = self['sensors']['people_now_present'][0]
 

	
 
            if 'names' in people_now_present and username not in people_now_present['names']:
 
                people_now_present['value'] += 1
 

	
 
                if username in current_app.config['PEOPLE_NOW_PRESENT_ALLOWED']:
 
                    people_now_present['names'].append(username)
 

	
 
            elif 'names' not in people_now_present:
 
                people_now_present['value'] += 1
 

	
 
                if username in current_app.config['PEOPLE_NOW_PRESENT_ALLOWED']:
 
                    people_now_present['names'] = [username]
 

	
 
            self['sensors']['people_now_present'][0] = people_now_present
 
        else:
 
            pass
 

	
 
    def remove_user_present(self, username):
 
        if self['state']['open'] and 'people_now_present' in self['sensors']:
 
            people_now_present = self['sensors']['people_now_present'][0]
 

	
 
            if people_now_present['value'] > 0:
 
                people_now_present['value'] -= 1
 

	
 
            if 'names' in people_now_present:
 
                if people_now_present['value'] == 0:
 
                    del people_now_present['names']
 
                elif username in people_now_present['names']:
 
                    people_now_present['names'].remove(username)
 
                    if not people_now_present['names']:
 
                        del people_now_present['names']
 
            self['sensors']['people_now_present'][0] = people_now_present
 
        else:
 
            pass
 

	
 
    def set_new_state(self, value=None, trigger_person=None, message=None):
 

	
 
        if value is not None:
 
            self['state']['open'] = value
 

	
 
            if 'twitter_consumer_key' in current_app.config:
 
            if 'TWITTER_CONSUMER_KEY' in current_app.config:
 
                auth = tweepy.OAuthHandler(
 
                    current_app.config['twitter_consumer_key'],
 
                    current_app.config['twitter_consumer_secret']
 
                    current_app.config['TWITTER_CONSUMER_KEY'],
 
                    current_app.config['TWITTER_CONSUMER_SECRET']
 
                )
 
                auth.set_access_token(
 
                    current_app.config['twitter_access_token_key'],
 
                    current_app.config['twitter.access_token_secret']
 
                    current_app.config['TWITTER_ACCESS_TOKEN_KEY'],
 
                    current_app.config['TWITTER_ACCESS_TOKEN_SECRET']
 
                )
 
                api = tweepy.API(auth)
 

	
 
                if value:
 
                    api.update_status('The space is now open!')
 
                else:
 
                    api.update_status('The space is now closed!')
 

	
 
            if not value:
 
                if 'people_now_present' in self['sensors']:
 
                    self['sensors']['people_now_present']['value'] = 0
 
                    if 'names' in self['sensors']['people_now_present']:
 
                        del self['sensors']['people_now_present']['names']
 
                    self['sensors']['people_now_present'][0]['value'] = 0
 
                    if 'names' in self['sensors']['people_now_present'][0]:
 
                        del self['sensors']['people_now_present'][0]['names']
 

	
 
                if 'message' in self['state']:
 
                    del self['state']['message']
 

	
 
            if trigger_person is None:
 
                del self['state']['trigger_person']
 

	
 
        if trigger_person is not None:
 
            self['state']['trigger_person'] = trigger_person
 

	
 
        if message is not None:
 
            self['state']['message'] = message
 

	
 
        if value is not None or trigger_person is not None or message is not None:
 
            self['state']['lastchange'] = int(time())
 

	
 

	
 
def request_wants_json():
 
    best = request.accept_mimetypes.best_match(
 
        ['application/json', 'text/html']
 
    )
 
    return best == 'application/json' and \
 
        request.accept_mimetypes[best] > \
 
        request.accept_mimetypes['text/html']
 

	
 

	
 
def fuzzy_list_find(lst, key, value):
 

	
 
    for i, dic in enumerate(lst):
 
        if dic[key] == value:
 
            return i
 

	
 
    raise ValueError
 

	
 

	
 
def first(iterable, keys):
 
    for key in keys:
 
        if key in iterable:
 
            return key
 

	
 
    raise ValueError
0 comments (0 inline, 0 general)