def create(self, payloads): response = ResponseBuilder() feed = Feed() sponsor = db.session.query(Sponsor).filter_by( id=payloads['sponsor_id']).first() attachment = self.save_file( payloads['attachment'] ) if payloads['attachment'] is not None else None feed.message = payloads['message'] feed.attachment = attachment feed.user_id = payloads['user_id'] feed.type = payloads['type'] feed.redirect_url = payloads['redirect_url'] feed.sponsor_id = payloads['sponsor_id'] db.session.add(feed) try: db.session.commit() user = feed.user.include_photos().as_dict() del user['fcmtoken'] data = feed.as_dict() data['attachment'] = Helper().url_helper( data['attachment'], current_app.config['GET_DEST'] ) if data['attachment'] is not None else None if 'user' in payloads['type']: data['user'] = user elif 'sponsor' in payloads['type']: data['user'] = sponsor.as_dict() return response.set_data(data).build() except SQLAlchemyError as e: data = e.orig.args return response.set_data(data).set_error(True).build()
def sponsor_create(self, payloads): response = ResponseBuilder() feed = Feed() for key in payloads: setattr(feed, key, payloads[key]) db.session.add(feed) try: db.session.commit() user = feed.user.include_photos().as_dict() sponsor = db.session.query(Sponsor).filter_by(id=payloads['sponsor_id']).first() del user['fcmtoken'] data = feed.as_dict() data['attachment'] = Helper().url_helper(data['attachment'], current_app.config['GET_DEST']) if data['attachment'] is not None else None if 'user' in payloads['type']: data['user'] = user elif 'sponsor' in payloads['type']: data['user'] = sponsor.as_dict() return response.set_data(data).build() except SQLAlchemyError as e: data = e.orig.args return response.set_data(data).set_error(True).build()