Exemplo n.º 1
0
 def on_update(self, updates, original):
     # in the case we have a comment
     if original['post_status'] == 'comment':
         original['blog'] = original['groups'][1]['refs'][0]['item']['client_blog']
         updates['blog'] = original['groups'][1]['refs'][0]['item']['client_blog']
         # if the length of the comment is not between 1 and 300 then we get an error
         check_comment_length(original['groups'][1]['refs'][0]['item']['text'])
     # check if updates `content` is diffrent then the original.
     content_diff = False
     if not updates.get('groups', False):
         content_diff = False
     elif len(original['groups'][1]['refs']) != len(updates['groups'][1]['refs']):
         content_diff = True
     else:
         for index, val in enumerate(updates['groups'][1]['refs']):
             item = get_resource_service('archive').find_one(req=None, _id=val['residRef'])
             if item['text'] != original['groups'][1]['refs'][index]['item']['text']:
                 content_diff = True
                 break
     if(content_diff):
         updates['content_updated_date'] = utcnow()
     # check permission
     post = original.copy()
     post.update(updates)
     self.check_post_permission(post)
     # when publishing, put the published item from drafts and contributions at the top of the timeline
     if updates.get('post_status') == 'open' and original.get('post_status') in ('draft', 'submitted', 'comment'):
         updates['order'] = self.get_next_order_sequence(original.get('blog'))
         # if you publish a post it will save a published date and register who did it
         updates['published_date'] = utcnow()
         updates['publisher'] = getattr(flask.g, 'user', None)
     # when unpublishing
     if original.get('post_status') == 'open' and updates.get('post_status') != 'open':
         updates['unpublished_date'] = utcnow()
     super().on_update(updates, original)
Exemplo n.º 2
0
    def on_update(self, updates, original):
        # check if the timeline is reordered
        if updates.get('order'):
            blog = get_resource_service('blogs').find_one(req=None, _id=original['blog'])
            if blog['posts_order_sequence'] == updates['order']:
                blog['posts_order_sequence'] = self.get_next_order_sequence(original.get('blog'))
        # in the case we have a comment
        if original['post_status'] == 'comment':
            original['blog'] = original['groups'][1]['refs'][0]['item']['client_blog']
            updates['blog'] = original['groups'][1]['refs'][0]['item']['client_blog']
            # if the length of the comment is not between 1 and 300 then we get an error
            check_comment_length(original['groups'][1]['refs'][0]['item']['text'])
        # check if updates `content` is diffrent then the original.
        content_diff = False
        if not updates.get('groups', False):
            content_diff = False
        elif len(original['groups'][1]['refs']) != len(updates['groups'][1]['refs']):
            content_diff = True
        else:
            for index, val in enumerate(updates['groups'][1]['refs']):
                item = get_resource_service('archive').find_one(req=None, _id=val['residRef'])
                if item['text'] != original['groups'][1]['refs'][index]['item']['text']:
                    content_diff = True
                    break
        if content_diff:
            updates['content_updated_date'] = utcnow()

        # check permission
        post = original.copy()
        post.update(updates)
        self.check_post_permission(post)
        # when publishing, put the published item from drafts and contributions at the top of the timeline
        if updates.get('post_status') == 'open' and original.get('post_status') in ('draft', 'submitted', 'comment'):
            updates['order'] = self.get_next_order_sequence(original.get('blog'))
            # if you publish a post it will save a published date and register who did it
            updates['published_date'] = utcnow()
            updates['publisher'] = get_publisher()
            # if you publish a post and hasn't `content_updated_date` add it.
            if not updates.get('content_updated_date', False):
                updates['content_updated_date'] = updates['published_date']
            # assure that the item info is keept if is needed.
            if original.get('post_status') == 'submitted' and original.get('original_creator', False) \
                    and updates.get('groups', False):
                item_resource = get_resource_service('items')
                for container in updates['groups'][1]['refs']:
                    item_id = container.get('residRef')
                    found = item_resource.find_one(req=None, _id=item_id)
                    item_resource.update(item_id, {'original_creator': original.get('original_creator')}, found)

        # when unpublishing
        if original.get('post_status') == 'open' and updates.get('post_status') != 'open':
            updates['unpublished_date'] = utcnow()
        super().on_update(updates, original)
Exemplo n.º 3
0
def create_amp_comment():
    data = request.values
    check_comment_length(data['text'])

    item_data = dict()
    item_data['text'] = data['text']
    item_data['commenter'] = data['commenter']
    item_data['client_blog'] = data['client_blog']
    item_data['item_type'] = "comment"
    items = get_resource_service('client_items')
    item_id = items.post([item_data])[0]

    comment_data = dict()
    comment_data["post_status"] = "comment"
    comment_data["client_blog"] = item_data['client_blog']
    comment_data["groups"] = [{
        "id": "root",
        "refs": [{
            "idRef": "main"
        }],
        "role": "grpRole:NEP"
    }, {
        "id": "main",
        "refs": [{
            "residRef": item_id
        }],
        "role": "grpRole:Main"
    }]

    post_comments = get_resource_service('client_posts')
    post_comment = post_comments.post([comment_data])[0]

    comment = post_comments.find_one(req=None, _id=post_comment)

    resp = api_response(comment, 201)
    resp.headers['Access-Control-Allow-Credentials'] = 'true'
    client_domain = data.get('__amp_source_origin')
    resp.headers['Access-Control-Allow-Origin'] = client_domain
    resp.headers['AMP-Access-Control-Allow-Source-Origin'] = client_domain
    resp.headers[
        'Access-Control-Expose-Headers'] = 'AMP-Access-Control-Allow-Source-Origin'
    return resp
Exemplo n.º 4
0
 def on_create(self, docs):
     for doc in docs:
         check_comment_length(doc['text'])
     super().on_create(docs)
Exemplo n.º 5
0
 def on_create(self, docs):
     for doc in docs:
         check_comment_length(doc['text'])
     super().on_create(docs)