Пример #1
0
    def index(cls, group_id):
        # confirm participant id in cookie
        c_group_id = request.cookies.get("group_id")
        participant_id = request.cookies.get("participant_id")

        # get group
        group = Group.get_by_id(group_id)

        # if participant is none or login to another group, create new participant in group
        if not participant_id or c_group_id != str(group_id):
            # create new participant todo:consider the case that group is None
            participant = Participant()
            participant.group_key = group.key
            participant_id = participant.put().id()  # save it to datastore

        # create channel
        participant_id_str = str(participant_id)
        cache = GAEMemcachedCache()
        token = cache.get(participant_id_str)
        if token is None:
            token = channel.create_channel(participant_id_str)
            # expiration of channel api token is 2 hour
            # https://developers.google.com/appengine/docs/python/channel/?hl=ja#Python_Tokens_and_security
            cache.set(participant_id_str, token, 3600 * 2)

        # return response
        resp = make_response(render_template('chat.html', token=token, group_name=group.name))

        # set participant_id to cookie
        resp.set_cookie("group_id", str(group_id),  expires=Config.calculate_expiration())
        resp.set_cookie("participant_id", participant_id_str, expires=Config.calculate_expiration())

        return resp
Пример #2
0
    def getParticipantInfo(self, participantUsername, insertOnBd=False):
        payload = {'format': 'json', 'username': participantUsername}
        r = requests.get(self.baseUrlAPI + 'participant/', params=payload)
        print r.status_code
        print r.json()
        aux = r.json()['objects']
        if len(aux) == 0:
            stderr.write('Participant with username: \"' +
                         participantUsername +
                         '\" doesn\'t exist in SIOrg+Pub')
            return None

        data = aux[0]
        contact = data['contact']['id']
        c = Contact(id=contact)
        p = Participant(username=data['username'],
                        password=data['password'],
                        phone_number=data['phone_number'],
                        qrcode=data['qrcode'],
                        name=data['name'],
                        email=data['email'],
                        work_place=data['work_place'],
                        country=data['country'],
                        photo=data['photo'],
                        contact=c)

        if insertOnBd:
            p.save()
        return p
Пример #3
0
    def create(cls, group_id, msg_type):
        group = Group.get_by_id(group_id)
        participant_key = None
        participant_id = request.cookies.get("participant_id")

        if participant_id:
            participant_key = Participant.get_by_id(long(participant_id)).key

        message = request.form.get("message", u"", type=unicode)
        reference_id = request.form.get("reference", u"", type=unicode)

        chat = Chat(
            group_key=group.key,
            participant_key=participant_key,
            type=msg_type,
            message=message
        )

        #set reference if exist
        if reference_id:
            reference = Chat.get_by_id(long(reference_id))
            if reference is not None:
                chat.reference = reference.key.id()

        chat.put()

        # send same group members (include myself)
        cls.__broadcast(group_id, chat)

        # message is send by channel, so you don't need return
        return ""
Пример #4
0
 def getParticipantInfo(self,participantUsername,insertOnBd=False):
     payload = {'format': 'json','username':participantUsername}
     r = requests.get(self.baseUrlAPI+ 'participant/', params=payload)
     print r.status_code
     print r.json()
     aux = r.json()['objects']
     if len(aux) == 0 :
         stderr.write('Participant with username: \"'+participantUsername + '\" doesn\'t exist in SIOrg+Pub')
         return None
     
     data = aux[0]
     contact= data['contact']['id']
     c = Contact(id = contact)
     p = Participant(username=data['username'],password=data['password'],phone_number=data['phone_number'],qrcode=data['qrcode'],name=data['name'],email=data['email'],work_place=data['work_place'],country=data['country'],photo=data['photo'],contact=c)
     
     if insertOnBd :
         p.save()
     return p
Пример #5
0
 def __broadcast(cls, group_id, chat):
     group = Group.get_by_id(group_id)
     participants_in_group = Participant.query(Participant.group_key == group.key)
     send = lambda p: channel.send_message(str(p.key.id()), json.dumps(chat.to_dict()))
     map(send, participants_in_group)