Skip to content

Commit

Permalink
Facebook profile update (#2116)
Browse files Browse the repository at this point in the history
* Tests added

* connection with facebook added

* Facebook  info provided to profile page

* One test fixed, error with unicode fixed

* Picture saves from facebook
  • Loading branch information
rafalkowalski authored and niranjan94 committed Aug 9, 2016
1 parent 565920e commit d2fa5cf
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 49 deletions.
6 changes: 0 additions & 6 deletions app/helpers/data_getter.py
Expand Up @@ -112,12 +112,6 @@ def get_event_role_invite(event_id, hash_code, **kwargs):
return RoleInvite.query.filter_by(event_id=event_id,
hash=hash_code, **kwargs).first()

@staticmethod
def get_all_owner_events():
"""Method return all owner events"""
# return Event.query.filter_by(owner=owner_id)
return login.current_user.events_assocs

@staticmethod
def get_email_notification_settings_by_id(email_id):
return EmailNotification.query.get(email_id)
Expand Down
17 changes: 0 additions & 17 deletions app/helpers/helpers.py
Expand Up @@ -444,23 +444,6 @@ def send_notif_invite_papers(user, event_name, cfs_link, submit_link):
send_notification(user, action, title, message)


def is_event_admin(event_id, users):
"""
:param event_id: Event id
:param users: User id
:return: is user admin
"""
is_admin = False
for user_obj in users:
if user_obj.user.id == login.current_user.id:
for ass in login.current_user.events_assocs:
if ass.event_id == int(event_id):
is_admin = ass.admin
if is_event_admin:
return is_admin
return is_admin


def ensure_social_link(website, link):
"""
converts usernames of social profiles to full profile links
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/oauth.py
Expand Up @@ -43,7 +43,7 @@ class FbOAuth(object):
"""Facebook Credentials"""
Fb_AUTH_URI = 'https://www.facebook.com/dialog/oauth'
Fb_TOKEN_URI = 'https://graph.facebook.com/oauth/access_token'
Fb_USER_INFO = 'https://graph.facebook.com/me?fields=email,id,name,picture'
Fb_USER_INFO = 'https://graph.facebook.com/me?fields=email,id,name,picture,bio,last_name,first_name,link'
SCOPE = ['public_profile', 'email']

@classmethod
Expand Down
15 changes: 11 additions & 4 deletions app/templates/gentelella/admin/profile/_form.html
Expand Up @@ -17,15 +17,22 @@

<input name="email" type="hidden" value="{{profile.email}}"/>
<input name="contact" type="hidden" value="{{profile.user_detail.contact | default('', true) }}"/>

<h3>Integrate Social Networks</h3>
<div class="row" style="margin-bottom:20px">

<div class="col-md-6 col-xs-6">
<label >Facebook:
</label>
<input type="url" pattern="https?://.*facebook\..+" name="facebook" class="form-control"
<label >Facebook:</label>
<div class="input-group">
<input type="url" pattern="https?://.*facebook\..+" name="facebook" class="form-control"
value="{{profile.user_detail.facebook|default('https://www.facebook.com/', true)}}" title="Not a valid facebook profile link"/>
<div class="input-group-btn">
<a type="button" class="btn btn-info" href="{{url_for('.connect_facebook')}}">Connect</a>
</div>


</div>

</div>

</div>
Expand Down
67 changes: 46 additions & 21 deletions app/views/api_v1_views.py
Expand Up @@ -19,6 +19,7 @@
from ..helpers.object_formatter import ObjectFormatter
from ..helpers.helpers import get_serializer
from ..helpers.data_getter import DataGetter
from ..helpers.data import DataManager, save_to_db
from views_helpers import event_status_code, api_response
from flask import Blueprint
from flask.ext.autodoc import Autodoc
Expand Down Expand Up @@ -397,7 +398,25 @@ def callback():

@app.route('/fCallback/', methods=('GET', 'POST'))
def facebook_callback():
print request.args
if login.current_user is not None and login.current_user.is_authenticated:
try:
facebook, __ = get_fb_auth()
response = facebook.get(FbOAuth.get_user_info())
if response.status_code == 200:
user_info = response.json()
user = login.current_user
if not user.user_detail.facebook:
user.user_detail.facebook = user_info['link']
if not user.user_detail.firstname:
user.user_detail.firstname = user_info['first_name']
if not user.user_detail.lastname:
user.user_detail.lastname = user_info['last_name']
if not user.user_detail.avatar_uploaded:
user.user_detail.avatar_uploaded = save_file_provided_by_url(user_info['picture']['data']['url'])
save_to_db(user)
except Exception:
pass
return redirect(url_for('admin.index'))
elif 'error' in request.args:
if request.args.get('error') == 'access denied':
Expand All @@ -406,17 +425,7 @@ def facebook_callback():
elif 'code' not in request.args and 'state' not in request.args:
return redirect(url_for('admin.login_view'))
else:
facebook = get_facebook_auth()
state = facebook.authorization_url(FbOAuth.get_auth_uri(), access_type='offline')[1]
facebook = get_facebook_auth(state=state)
if 'code' in request.url:
code_url = (((request.url.split('&'))[0]).split('='))[1]
try:
token = facebook.fetch_token(FbOAuth.get_token_uri(), authorization_url=request.url,
code=code_url, client_secret=FbOAuth.get_client_secret())
except HTTPError:
return 'HTTP Error occurred'
facebook = get_facebook_auth(token=token)
facebook, token = get_fb_auth()
response = facebook.get(FbOAuth.get_user_info())
if response.status_code == 200:
user_info = response.json()
Expand All @@ -433,6 +442,18 @@ def facebook_callback():
return redirect(intended_url())
return 'did not find user info'

def get_fb_auth():
facebook = get_facebook_auth()
state = facebook.authorization_url(FbOAuth.get_auth_uri(), access_type='offline')[1]
facebook = get_facebook_auth(state=state)
if 'code' in request.url:
code_url = (((request.url.split('&'))[0]).split('='))[1]
try:
token = facebook.fetch_token(FbOAuth.get_token_uri(), authorization_url=request.url,
code=code_url, client_secret=FbOAuth.get_client_secret())
except HTTPError:
return 'HTTP Error occurred'
return get_facebook_auth(token=token), token

@app.route('/iCallback/', methods=('GET', 'POST'))
def instagram_callback():
Expand All @@ -447,20 +468,24 @@ def instagram_callback():
client_secret=InstagramOAuth.get_client_secret())
response = instagram.get('https://api.instagram.com/v1/users/self/media/recent/?access_token=' + token.get('access_token', '')).json()
for el in response.get('data'):
response_file = urlopen(el['images']['standard_resolution']['url'])

filename = str(time.time()) + '.jpg'
file_path = os.path.realpath('.') + '/static/temp/' + filename
fh = open(file_path, "wb")
fh.write(response_file.read())
fh.close()
img = UploadedFile(file_path, filename)
print img
background_url = upload(img, '/image/' + filename)
background_url = save_file_provided_by_url(el['images']['standard_resolution']['url'])
print background_url

return 'Not implemented'


def save_file_provided_by_url(url):
response_file = urlopen(url)
filename = str(time.time()) + '.jpg'
file_path = os.path.realpath('.') + '/static/temp/' + filename
fh = open(file_path, "wb")
fh.write(response_file.read())
fh.close()
img = UploadedFile(file_path, filename)
background_url = upload(img, '/image/' + filename)
return background_url


@app.route('/pic/<path:filename>')
@auto.doc()
def send_pic(filename):
Expand Down
1 change: 1 addition & 0 deletions tests/functionality/test_data_getter.py
Expand Up @@ -60,5 +60,6 @@ def test_get_roles_by_name(self):
def test_get_services(self):
with app.test_request_context():
self.assertTrue(DataGetter.get_services())

if __name__ == '__main__':
unittest.main()

0 comments on commit d2fa5cf

Please sign in to comment.