# Path: blindr/common/authenticate.py
# def authenticate(func):
#     @wraps(func)
#     def wrapper(*args, **kwargs):
#         user = token_authentication(request.headers.get('X-User-Token'))
#         if user:
#             func.__self__.user = user
#             return func(*args, **kwargs)
#         abort(401)
#     return wrapper
# 
# Path: blindr/models/event.py
# class Event(object):
# 
#     @staticmethod
#     def create(data):
#         data['sent_at'] = time.time()
#         data['event_id'] = str(uuid.uuid4())
#         return events.put_item(data=data)
# 
#     @staticmethod
#     def fetch(user=None, city=None, since=60):
#         adjusted_since = since - 60
#         dst = ''
# 
#         if user:
#             dst = 'user:{}'.format(user)
#         if city:
#             dst = 'city:{}'.format(city)
# 
#         resultset = events.query_2(
#             dst__eq=dst,
#             sent_at__gte=adjusted_since)
# 
#         items = [dict(item) for item in resultset]
#         for item in items:
#             item['sent_at'] = float(item['sent_at'])
# 
#         return items
# 
#     @staticmethod
#     def fetch_history(user, other,since=0):
#         resultset = events.query_2(
#             participants__eq= '{}:{}'.format(*sorted([user,other])),
#             index= 'participants-index',
#             sent_at__gte=since)
# 
#         items = [dict(item) for item in resultset]
#         for item in items:
#             item['sent_at'] = float(item['sent_at'])
# 
#         return list(items)
# 
# Path: blindr/models/user.py
# class User(db.Model):
#     __tablename__ = 'users'
# 
#     id =	db.Column(db.String, primary_key=True)
# 
#     OAuth = 	db.Column(db.String)
#     fake_name =	db.Column(db.String(50), nullable = False)
#     real_name =	db.Column(db.String(50))
#     gender =	db.Column(db.CHAR, nullable = False)
#     looking_for = db.Column(db.String)
#     last_poll =	db.Column(db.TIMESTAMP)
#     facebook_urls = db.Column(db.String)
# 
#     @staticmethod
#     def from_facebook(fb_token):
#         user = None
#         graph = facebook.GraphAPI(access_token=fb_token)
#         fb_user = graph.get_object(id='me')
# 
#         session = db.session
#         user = session.query(User).filter_by(id=fb_user['id']).first()
#         if not user:
#             user = User(id= fb_user['id'],
#                     OAuth= fb_token,
#                     fake_name= name_generator.generate_name(),
#                     real_name= fb_user['name'],
#                     gender= _fb_gender_map[fb_user['gender'] or 'male'],
#                     last_poll= datetime.utcnow(),
#                     facebook_urls= "",
#                     looking_for="")
# 
#             session.add(user)
# 
#         else:
#             user.OAuth= fb_token
#             user.gender= _fb_gender_map[fb_user['gender'] or 'male']
#             user.last_poll= datetime.utcnow()
#             user.real_name= fb_user['name']
# 
#         session.commit()
#         return user


from flask.ext import restful
from blindr.common.authenticate import authenticate
from flask import request, abort
from blindr.models.event import Event
from blindr.models.user import User
import time
import config


class Message(restful.Resource):
    method_decorators=[authenticate]

    def post(self):
        dst_city = request.form.get('dst_city')
        dst_user = request.form.get('dst_user')
        message = request.form.get('message')
        dst = None
        if dst_city:
            dst = 'city:{}'.format(dst_city.lower())
        elif dst_user:
            dst = 'user:{}'.format(dst_user)


# Path: blindr/common/authenticate.py
# def authenticate(func):
#     @wraps(func)
#     def wrapper(*args, **kwargs):
#         user = token_authentication(request.headers.get('X-User-Token'))
#         if user:
#             func.__self__.user = user
#             return func(*args, **kwargs)
#         abort(401)
#     return wrapper
# 
# Path: blindr/models/event.py
# class Event(object):
# 
#     @staticmethod
#     def create(data):
#         data['sent_at'] = time.time()
#         data['event_id'] = str(uuid.uuid4())
#         return events.put_item(data=data)
# 
#     @staticmethod
#     def fetch(user=None, city=None, since=60):
#         adjusted_since = since - 60
#         dst = ''
# 
#         if user:
#             dst = 'user:{}'.format(user)
#         if city:
#             dst = 'city:{}'.format(city)
# 
#         resultset = events.query_2(
#             dst__eq=dst,
#             sent_at__gte=adjusted_since)
# 
#         items = [dict(item) for item in resultset]
#         for item in items:
#             item['sent_at'] = float(item['sent_at'])
# 
#         return items
# 
#     @staticmethod
#     def fetch_history(user, other,since=0):
#         resultset = events.query_2(
#             participants__eq= '{}:{}'.format(*sorted([user,other])),
#             index= 'participants-index',
#             sent_at__gte=since)
# 
#         items = [dict(item) for item in resultset]
#         for item in items:
#             item['sent_at'] = float(item['sent_at'])
# 
#         return list(items)
# 
# Path: blindr/models/user.py
# class User(db.Model):
#     __tablename__ = 'users'
# 
#     id =	db.Column(db.String, primary_key=True)
# 
#     OAuth = 	db.Column(db.String)
#     fake_name =	db.Column(db.String(50), nullable = False)
#     real_name =	db.Column(db.String(50))
#     gender =	db.Column(db.CHAR, nullable = False)
#     looking_for = db.Column(db.String)
#     last_poll =	db.Column(db.TIMESTAMP)
#     facebook_urls = db.Column(db.String)
# 
#     @staticmethod
#     def from_facebook(fb_token):
#         user = None
#         graph = facebook.GraphAPI(access_token=fb_token)
#         fb_user = graph.get_object(id='me')
# 
#         session = db.session
#         user = session.query(User).filter_by(id=fb_user['id']).first()
#         if not user:
#             user = User(id= fb_user['id'],
#                     OAuth= fb_token,
#                     fake_name= name_generator.generate_name(),
#                     real_name= fb_user['name'],
#                     gender= _fb_gender_map[fb_user['gender'] or 'male'],
#                     last_poll= datetime.utcnow(),
#                     facebook_urls= "",
#                     looking_for="")
# 
#             session.add(user)
# 
#         else:
#             user.OAuth= fb_token
#             user.gender= _fb_gender_map[fb_user['gender'] or 'male']
#             user.last_poll= datetime.utcnow()
#             user.real_name= fb_user['name']
# 
#         session.commit()
#         return user


# Path: blindr/api/message.py
from flask.ext import restful
from blindr.common.authenticate import authenticate
from flask import request, abort
from blindr.models.event import Event
from blindr.models.user import User
import time
import config



class Message(restful.Resource):
    method_decorators=[authenticate]

    def post(self):
        dst_city = request.form.get('dst_city')
        dst_user = request.form.get('dst_user')
        message = request.form.get('message')
        dst = None
        if dst_city:
            dst = 'city:{}'.format(dst_city.lower())
        elif dst_user:
            dst = 'user:{}'.format(dst_user)


        if dst is None: