def create(text: str, tags: List[str], link_to_post: str, image: str,
            source_group: str):
     return Post(text=text,
                 tags=tags,
                 link_to_post=link_to_post,
                 image=image,
                 source_group=source_group)
Exemple #2
0
def post_post_meta(event, context):
    table = __get_table_client()
    company_id = event["pathParameters"]["company_id"]
    # post_id = event["pathParameters"]["post_id"]
    payload = json.loads(event["body"])
    payload.update({"company_id": company_id})
    post = Post(payload)

    keys = post.get_keys()
    PK, SK = keys["PK"], keys["SK"]
    print("Key: ", json.dumps({"PK": PK, "SK": SK}, indent=4))
    table_record = post.get_record()

    try:
        table.put_item(
            Item=table_record,
            ConditionExpression=
            "attribute_not_exists(PK) and attribute_not_exists(SK)",
            ReturnConsumedCapacity="TOTAL",
        )
    except ClientError as e:
        if e.response["Error"]["Code"] == "ConditionalCheckFailedException":
            return _response(409, {"status": "Item already exists"})
        print(e.response["Error"]["Message"])
        return _response(500, {"status": "DynamoDB Client Error"})
    else:
        print("PutItem succeeded:")
        print(json.dumps(table_record, indent=4, cls=DecimalEncoder))
    item = post.get_item()
    return _response(201, item)
    def new_post(self, title, content, date=datetime.datetime.utcnow()):

        post = Post(blog_id=self._id,
                    title=title,
                    content=content,
                    author=self.author,
                    created_date=date)
        post.save_to_mongo()  #we save it to the database
Exemple #4
0
 def get(self):
     """
     Renders a new post page if user is authenticated, 
     if no redirects to login route
     :return: 
     """
     if User.is_authenticated(self.request.cookies.get('user_id')):
         self.render('post.html', post=Post(title="", content=""))
     else:
         self.redirect('/login')
    def post(self):

        current_user = self.current_user
        title = self.request.get("title")
        text = self.request.get("content")

        if title and text:
            post = Post(author=current_user, title=title, content=text)
            post.put()
            self.redirect("/post/" + str(post.key().id()))
        else:
            error = "Title and Text must be provided to submit an article!"
            self.render("newpost.html", error=error)
    def get_latest_group_post(self, group_id):
        response = self.__api.wall.get(
            owner_id=group_id,
            count=1,
            v='5.101'
        )

        last_post = self.extract_last_post(response)

        if not last_post:
            return None

        return Post(
            last_post.get('id'),
            last_post.get('text')
        )
Exemple #7
0
    def post(self):
        """
        Creates or update a new post entry if user is 
        authenticated, if no redirects to login route
        :return: 
        """
        user_id = self.request.cookies.get('user_id')
        post_id = self.request.get('id')
        title = self.request.get('title')
        content = self.request.get('content')

        if 'update' in self.request.POST:
            post = Post.get_by_id(post_id)

            if Post.is_post_owner(user_id, post):
                post.title = title
                post.content = content
                post.last_modified = datetime.datetime.now()

                post.put()

                time.sleep(2)
        elif 'create' in self.request.POST:
            title = self.request.get('title')
            content = self.request.get('content')

            user = User.get_by_hash(user_id)

            p = Post(author=user.user,
                     title=title,
                     content=content,
                     created=datetime.datetime.now(),
                     last_modified=datetime.datetime.now())
            p.put()

            time.sleep(2)

        self.redirect('/main')
Exemple #8
0
def get_post_meta(event, context):

    table = __get_table_client()
    company_id = event["pathParameters"]["company_id"]
    post_id = event["pathParameters"]["post_id"]
    created_at = event["queryStringParameters"]["created_at"]
    PK, SK = Post.keys_from_ids_and_date(company_id, post_id, created_at)
    print("Key: ", json.dumps({"PK": PK, "SK": SK}, indent=4))

    try:
        data = table.get_item(
            Key={
                "PK": PK,
                "SK": SK
            },
            ProjectionExpression=
            "company_id, user_id, post_id, post_title, post_content, points_map, can_share_on, created_at, updated_at",
            ReturnConsumedCapacity="TOTAL",
        )
        if not data.get("Item"):
            raise KeyError
        # response = table.get_item(Key={"PK":"COMPANY#8fd4728b-89b6-40aa-a57a-85a4672ec9a0", "SK":"#METADATA#8fd4728b-89b6-40aa-a57a-85a4672ec9a0"}, ReturnConsumedCapacity='TOTAL')

    except ClientError as e:
        print(e.response["Error"]["Message"])
        return _response(500, {"status": "DynamoDB Client Error"})
    except KeyError as e:
        print(e)
        return _response(404, {"status": "ITEM NOT FOUND"})
    else:
        post = Post(data["Item"])
        consumed_cap = data["ConsumedCapacity"]
        print("GetItem succeeded:")
        print(json.dumps(data, indent=4, cls=DecimalEncoder))

    return _response(200, post.get_item())
Exemple #9
0
def create_post(post_id: str, content: str, user: User, secret: bool):
    new_post = Post(post_id=post_id, owner=user, text=content, secret=secret)
    logger.logging(new_post)
    return new_post
Exemple #10
0
def put_post_meta(event, context):

    # table = __get_table_client()

    # payload = json.loads(event["body"])

    # (
    #     PK,
    #     SK,
    #     company_id,
    #     user_id,
    #     post_id,
    #     post_title,
    #     post_content,
    #     can_share_on,
    #     points_map,
    #     created_at,
    #     updated_at,
    # ) = _get_post_meta(payload)

    # print(
    #     PK,
    #     SK,
    #     company_id,
    #     user_id,
    #     post_id,
    #     post_title,
    #     post_content,
    #     can_share_on,
    #     points_map,
    #     created_at,
    #     updated_at,
    # )
    # import time

    # post_created_at = int(time.time())
    # try:
    #     table.update_item(
    #         Key={"PK": PK, "SK": SK},
    #         UpdateExpression="SET #company_id = :company_id, #user_id = :user_id, #post_id = :post_id, #post_title = :post_title, #post_content = :post_content, #can_share_on = :can_share_on, #points_map = :points_map, #created_at = if_not_exists(#created_at, :created_at), #post_created_at = if_not_exists(#post_created_at, :post_created_at), #updated_at = :updated_at",
    #         ExpressionAttributeNames={
    #             "#company_id": "company_id",
    #             "#user_id": "user_id",
    #             "#post_id": "post_id",
    #             "#post_title": "post_title",
    #             "#post_content": "post_content",
    #             "#can_share_on": "can_share_on",
    #             "#points_map": "points_map",
    #             "#post_created_at": "post_created_at",
    #             "#created_at": "created_at",
    #             "#updated_at": "updated_at",
    #         },
    #         ExpressionAttributeValues={
    #             ":company_id": company_id,
    #             ":user_id": user_id,
    #             ":post_id": post_id,
    #             ":post_title": post_title,
    #             ":post_content": post_content,
    #             ":can_share_on": can_share_on,
    #             ":points_map": points_map,
    #             ":created_at": created_at,
    #             ":updated_at": updated_at,
    #             ":post_created_at": post_created_at,
    #         },
    #         ReturnConsumedCapacity="TOTAL",
    #     )
    #     payload["post_id"] = post_id

    # except ClientError as e:
    #     print(e.response["Error"]["Message"])
    #     return _response(500, {"status": "DynamoDB Client Error"})
    # except KeyError as e:
    #     print(e)
    #     return _response(404, {"status": "ITEM NOT FOUND"})
    # else:
    #     print("PutItem succeeded:")
    #     print(json.dumps(payload, indent=4, cls=DecimalEncoder))

    # if json.loads(event["body"]).get("post_id"):
    #     return _response(200, payload)
    # return _response(201, payload)
    table = __get_table_client()
    company_id = event["pathParameters"]["company_id"]
    post_id = event["pathParameters"]["post_id"]
    created_at = event["queryStringParameters"]["created_at"]

    payload = json.loads(event["body"])
    payload.update({
        "company_id": company_id,
        "post_id": post_id,
        created_at: "created_at"
    })
    post = Post(payload)
    table_record = post.get_record()
    # PK = table_record["PK"]
    # SK = table_record["SK"]
    try:
        response = table.put_item(
            ConditionExpression="attribute_exists(PK) and attribute_exists(SK)",
            Item=table_record,
            ReturnConsumedCapacity="TOTAL",
        )

    except ClientError as e:
        if e.response["Error"]["Code"] == "ConditionalCheckFailedException":
            return _response(404, {"status": "ITEM NOT FOUND"})

        print(e.response["Error"]["Message"])
        return _response(500, {"status": "DynamoDB Client Error"})
    else:
        print("PutItem succeeded:")
        print(json.dumps(table_record, indent=4, cls=DecimalEncoder))
    # print("Item update response: ", response)
    # if not response.get("Attributes"):
    #     return _response(404, {"status": "ITEM NOT FOUND"})
    # item = UserMeta(response["Attributes"]).get_item()
    item = post.get_item()
    return _response(200, item)