def save_contact(contact): contact.name = request.form.get('name') contact.image = request.form.get('image') contact.url = request.form.get('url') for nick in contact.nicks: db.session.delete(nick) db.session.commit() contact.nicks = [Nick(name=nick.strip()) for nick in request.form.get('nicks', '').split(',') if nick.strip()] contact.social = util.filter_empty_keys({ 'twitter': request.form.get('twitter'), 'facebook': request.form.get('facebook'), }) if not contact.id: db.session.add(contact) db.session.commit() if contact.nicks: return redirect(url_for('.contact_by_name', name=contact.nicks[0].name)) else: return redirect(url_for('.contacts'))
def handle_new_or_edit(post, message, link, name, picture, is_photo, album_id): current_app.logger.debug('publishing to facebook') # TODO I cannot figure out how to tag people via the FB API post_args = { 'access_token': get_settings().facebook_access_token, 'message': message.strip(), 'privacy': json.dumps({'value': 'EVERYONE'}), #'privacy': json.dumps({'value': 'SELF'}), } if is_photo and picture: post_args['url'] = picture current_app.logger.debug( 'Sending photo %s to album %s', post_args, album_id) response = requests.post( 'https://graph.facebook.com/v2.0/{}/photos'.format( album_id if album_id else 'me'), data=post_args) else: post_args.update(util.filter_empty_keys({ 'link': link, 'name': name, 'picture': picture, })) current_app.logger.debug('Sending post %s', post_args) response = requests.post('https://graph.facebook.com/v2.0/me/feed', data=post_args) response.raise_for_status() current_app.logger.debug("Got response from facebook %s", response) if 'json' in response.headers['content-type']: result = response.json() current_app.logger.debug( 'published to facebook. response {}'.format(result)) if result: if is_photo: facebook_photo_id = result['id'] facebook_post_id = result['post_id'] # actually the album split = facebook_post_id.split('_', 1) if split and len(split) == 2: user_id, post_id = split fb_url = 'https://facebook.com/{}/posts/{}'.format( user_id, facebook_photo_id) post.add_syndication_url(fb_url) return fb_url else: facebook_post_id = result['id'] split = facebook_post_id.split('_', 1) if split and len(split) == 2: user_id, post_id = split fb_url = 'https://facebook.com/{}/posts/{}'.format( user_id, post_id) post.add_syndication_url(fb_url) return fb_url
from redwind.models import Contact, Nick, AddressBook from redwind import db from redwind.util import filter_empty_keys for name, entry in AddressBook().entries.items(): contact = Contact() contact.name = name contact.image = entry.get('photo') contact.url = entry.get('url') contact.social = filter_empty_keys({ 'twitter': entry.get('twitter'), 'facebook': entry.get('facebook') }) if 'twitter' in entry: nick = Nick() nick.name = entry.get('twitter') contact.nicks = [nick] db.session.add(nick) db.session.add(contact) db.session.commit()
def micropub_endpoint(): current_app.logger.info( "received micropub request %s, args=%s, form=%s, headers=%s", request, request.args, request.form, request.headers) if request.method == 'GET': current_app.logger.debug('micropub GET request %s -> %s', request, request.args) q = request.args.get('q') if q == 'syndicate-to': current_app.logger.debug('returning syndication targets') response = make_response(urllib.parse.urlencode([ ('syndicate-to[]', target) for target in SYNDICATION_TARGETS])) response.headers['Content-Type'] = 'application/x-www-form-urlencoded' return response elif q in ('actions', 'json_actions'): current_app.logger.debug('returning action handlers') reply_url = url_for('admin.new_post', type='reply', _external=True) repost_url = url_for('admin.new_post', type='share', _external=True) like_url = url_for('admin.new_post', type='like', _external=True) payload = { 'reply': reply_url + '?url={url}', 'repost': repost_url + '?url={url}', 'favorite': like_url + '?url={url}', 'like': like_url + '?url={url}', } accept_header = request.headers.get('accept', '') if q == 'json_actions' or 'application/json' in accept_header: return jsonify(payload) else: response = make_response(urllib.parse.urlencode(payload)) response.headers['Content-Type'] = 'application/x-www-form-urlencoded' return response else: abort(404) bearer_prefix = 'Bearer ' header_token = request.headers.get('authorization') if header_token and header_token.startswith(bearer_prefix): token = header_token[len(bearer_prefix):] else: token = request.form.get('access_token') if not token: current_app.logger.warn('hit micropub endpoint with no access token') abort(401) try: decoded = util.jwt_decode(token) except jwt.DecodeError as e: current_app.logger.warn('could not decode access token: %s', e) abort(401) me = decoded.get('me') client_id = decoded.get('client_id') parsed = urllib.parse.urlparse(me) user = auth.load_user(parsed.netloc) if not user or not user.is_authenticated(): current_app.logger.warn( 'received valid access token for invalid user: %s', me) abort(401) h = request.form.get('h') in_reply_to = request.form.get('in-reply-to') like_of = request.form.get('like-of') photo_file = request.files.get('photo') bookmark = request.form.get('bookmark') or request.form.get('bookmark-of') repost_of = request.form.get('repost-of') post_type = ('event' if h == 'event' else 'article' if 'name' in request.form else 'photo' if photo_file else 'reply' if in_reply_to else 'like' if like_of else 'bookmark' if bookmark else 'share' if repost_of else 'note') latitude = None longitude = None location_name = None venue_id = None loc_str = request.form.get('location') geo_prefix = 'geo:' if loc_str: if loc_str.startswith(geo_prefix): loc_str = loc_str[len(geo_prefix):] loc_params = loc_str.split(';') if loc_params: latitude, longitude = loc_params[0].split(',', 1) location_name = request.form.get('place_name') else: venue_prefix = urllib.parse.urljoin(get_settings().site_url, 'venues/') if loc_str.startswith(venue_prefix): slug = loc_str[len(venue_prefix):] venue = Venue.query.filter_by(slug=slug).first() if venue: venue_id = venue.id # url of the venue, e.g. https://kylewm.com/venues/cafe-trieste-berkeley-california venue = request.form.get('venue') syndicate_to = request.form.getlist('syndicate-to[]') syndication = request.form.get('syndication') # TODO check client_id if client_id == 'https://kylewm-responses.appspot.com/' and syndication: current_app.logger.debug( 'checking for existing post with syndication %s', syndication) existing = Post.query.filter( Post.syndication.like(db.literal('%"' + syndication + '"%')) ).first() if existing: current_app.logger.debug( 'found post for %s: %s', syndication, existing) return redirect(existing.permalink) else: current_app.logger.debug( 'no post found with syndication %s', syndication) # translate from micropub's verbage.TODO unify translated = util.filter_empty_keys({ 'post_type': post_type, 'published': request.form.get('published'), 'start': request.form.get('start'), 'end': request.form.get('end'), 'title': request.form.get('name'), 'content': request.form.get('content'), 'venue': venue_id, 'latitude': latitude, 'longitude': longitude, 'location_name': location_name, 'syndication': syndication, 'in_reply_to': in_reply_to, 'like_of': like_of, 'repost_of': repost_of, 'bookmark_of': bookmark, 'photo': photo_file, 'syndicate-to': [SYNDICATION_TARGETS.get(to) for to in syndicate_to], 'hidden': 'true' if like_of or bookmark else 'false', }) with current_app.test_request_context( base_url=get_settings().site_url, path='/save_new', method='POST', data=translated ): current_app.logger.debug('received fake request %s: %s', request, request.args) login_user(user) current_app.logger.debug('successfully authenticated as user %s => %s', me, user) from . import admin resp = admin.save_new() return make_response('Created', 201, {'Location': resp.location})
from redwind.models import Contact, Nick, AddressBook from redwind import db from redwind.util import filter_empty_keys for name, entry in AddressBook().entries.items(): contact = Contact() contact.name = name contact.image = entry.get('photo') contact.url = entry.get('url') contact.social = filter_empty_keys( {'twitter': entry.get('twitter'), 'facebook': entry.get('facebook')}) if 'twitter' in entry: nick = Nick() nick.name = entry.get('twitter') contact.nicks = [nick] db.session.add(nick) db.session.add(contact) db.session.commit()