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()
Exemplo n.º 2
0
def submit():
    form = SubmissionForm()
    if form.validate_on_submit():
        if current_user.is_authenticated:
            feed = Feed(
                owner=form.owner.data,
                rss=form.rss.data,
                html=form.html.data,
                user_id=current_user.id,
            )
            db.session.add(feed)
            db.session.commit()
        return redirect(url_for("home.index"))
    return render_template("/feed/submit.html", title="submission", form=form)
Exemplo n.º 3
0
    def create_feed(cls, db, mc, cfg, user_id):
        def get_random_key():
            return ''.join(
                random.SystemRandom().choice(string.ascii_lowercase +
                                             string.ascii_uppercase +
                                             string.digits) for _ in range(12))

        try:
            # get user details from Google Plus
            service = cls._get_plus_service(cfg.get('feed.api_key'))
            person = service.people().get(userId=user_id).execute()

            # change photo size
            photo_url_site = person['image']['url']
            photo_url_db = person['image']['url']
            idx = photo_url_site.index('?')
            if idx != -1:
                photo_url_site = '%s?sz=%s' % (
                    photo_url_site[:idx], cfg.get('feed.photo_size.website'))
                photo_url_db = '%s?sz=%s' % (
                    photo_url_db[:idx], cfg.get('feed.photo_size.database'))

            # check feed existence
            uid = person['id']
            feed = Feed.get_by_user_id(db, uid)
            if not feed:
                # create a new feed
                rnd_key = get_random_key()
                while Feed.get_by_pkey(db, rnd_key):
                    rnd_key = get_random_key()
                feed = Feed(rnd_key, uid)
                feed.save(db)

            else:
                mc.delete(cls.MEMCACHE_KEY % feed.pkey)

            # update photo
            feed.photo_url = photo_url_db
            feed.save(db)

            return {
                'pkey': feed.pkey,
                'photo': photo_url_site,
                'name': person['displayName']
            }

        except HttpError as e:
            if e.resp.status == 404:
                raise cls.UserIdNotFoundException
            raise e
Exemplo n.º 4
0
	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()
Exemplo n.º 5
0
def submit_feed_form():
    print(request.form)
    form_data = request.get_json()
    print(form_data)
    if form_data is not None and form_data.get('formData') is not None:
        duck_feed_form = DuckFeedForm()
        validation_result = duck_feed_form.is_valid(form_data.get('formData'))
        if validation_result.get('valid') is False:
            return jsonify({'success': False, 'message':validation_result.get('message')}), 400
        # validation successful
        permitted_params = duck_feed_form.get_permitted_params(form_data.get('formData'))
        try:
            permitted_params['feedTime'] = datetime.strptime(permitted_params.pop('dateTime'), '%Y-%m-%dT%H:%M:%S.%fZ')
        except Exception as e:
            return jsonify({'success': False, 'message': 'Bad request. Invalid format of dateTime. Expected format is %Y-%m-%dT%H:%M:%S.%fZ'}), 400
            print(e)
        print(permitted_params)
        # insert in mongo
        feed_id = Feed().insert_one(permitted_params)
        if feed_id is not None:
            return jsonify({'success': True, 'message':'Feed Form successfully submitted', 'feed_id':str(feed_id)}), 200
        else:
            return jsonify({'success': False, 'message': 'Error while submitting feed form data'}), 400
    return jsonify({'success': False, 'message':'Bad request. Form data cannot be empty'}), 400
Exemplo n.º 6
0
def get_feed_form_submissions():
    form_data = Feed().fetch_all()
    if form_data is not None:
        return jsonify({'success': True, 'message':'successfully fetched feed form data', 'data':form_data}), 200
    else:
        return jsonify({'success': False, 'message': 'Something went wrong.'}), 400