Example #1
0
    def get(self, location=''):
        """
        Shows login button if not logged in, otherwise shows logout.
        Creates a new channel for use with live updates and chat.
        If location is unspecified, use a random initial tile.
        Output created using template index.html

        :param location: x, y of tile to display when opening the graffiti wall
        """

        #Check if user is logged in.
        user = users.get_current_user()
        if user:
            #If so, show logout button
            login_url = users.create_logout_url(self.request.uri)
            login_label = 'logout'
            user_login = 1
        else:
            #If not, show login button
            login_url = users.create_login_url(self.request.uri)
            login_label = 'login'
            user_login = 0

        #For live updates
        token = self.session.get('token')
        if token is None:
            channel_id = (str(datetime.now()) + "," +
                str(random.randint(1, 10000)))
            token = channel.create_channel(channel_id)
            self.session['channel_id'] = channel_id
            self.session['token'] = token
            ch = UpdateChannel(channel_id=channel_id)
            ch.put()

        #Get random initial tile
        rand_num = random.random()
        tile = Tile.gql("WHERE rand_num >= :1 ORDER BY rand_num",
            rand_num).get()
        if tile is None:
            locX = 0
            locY = 0
        else:
            locX = tile.x
            locY = tile.y

        #Output from template
        template = jinja_environment.get_template('index.html')
        self.response.out.write(template.render(
            login_url=login_url,
            login_label=login_label,
            token=token,
            locX=locX,
            locY=locY,
            adnum=random.randint(0, 4),
            user_login=user_login))
Example #2
0
    def post(self):
        user = users.get_current_user()
        #Get the contetnts of the message
        message = self.request.get('message')

        #if there is a user, set him as sender, else use guest user
        if user is not None:
            sender = user.nickname()
        else:
            sender = "Guest User"

        #Distribute the message to all users
        channels = UpdateChannel.gql("").fetch(100)
        jpost = json.dumps({
            "Sender": sender,
            "Message": message,
            "Type": "Chat"})
        for ch in channels:
            ch_id = ch.channel_id
            d = parse_datetime(ch_id.split(",")[0])
            if d < datetime.now() + timedelta(hours=-2):
                ch.key.delete()
            channel.send_message(ch.channel_id, jpost)
Example #3
0
    def post(self):
        """
        Saves the image sent via JSON in the blobstore.
        If an entry for (x, y) is not in the Tile table of the datastore,
        create it with the blob key for the image.
        Otherwise update the entry with the new blob key and
        delete the existing associated blob.
        Send messages to any open channel that a change has been made to the
        tile.
        Delete any channels that may have been open for more than two hours.
        """

        #Get JSON data
        x = int(self.request.get('x'))
        y = int(self.request.get('y'))
        data = self.request.get('data')

        #base64 decode image
        data = base64.b64decode(data)

        #Write image to blobstore
        file_name = files.blobstore.create(mime_type='image/png')
        with files.open(file_name, 'a') as f:
            f.write(data)

        files.finalize(file_name)

        #Get blob key for newly created blob in blobstore
        blob_key = files.blobstore.get_blob_key(file_name)

        # Check if tile is already in database
        query = Tile.gql("WHERE x = :1 AND y = :2", x, y)
        myTile = query.get()
        if myTile is None:

            #Create new tile entry in database with key to new blob
            myTile = Tile(x=x, y=y, blob_key=blob_key,
                rand_num=random.random())
            myTile.put()
        else:

            #Update the blob key in the entry for this tile
            old_key = myTile.blob_key
            myTile.blob_key = blob_key
            myTile.put()

            #Delete the blob previously associated with the tile
            google.appengine.ext.blobstore.delete(old_key)

        #For live updates:
        #Send a message to the channels indicating this tile has changed.
        channels = UpdateChannel.gql("").fetch(100)
        message = json.dumps({"x": x, "y": y, "Type": "Tile"})
        for ch in channels:
            ch_id = ch.channel_id

            #if a channel is two hours old, delete it.
            d = parse_datetime(ch_id.split(",")[0])
            if d < datetime.now() + timedelta(hours=-2):
                ch.key.delete()
            elif ch_id != self.session.get("channel_id"):
                channel.send_message(ch.channel_id, message)

        #No response data to send.
        self.response.set_status(200)