Profile pic sync from KAK FB group is improved
PS: Tried using windows, are line endings borked?
This commit is contained in:
1 parent
14c57bf9eb
commit
4488acdfcb
7 files changed
+53
-90
No files matched your search
@@ -0,0 +1 @@
|
|||||||
|
__author__ = 'Nikolai'
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
__author__ = 'Nikolai'
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
# coding: utf-8
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
|
import difflib
|
||||||
|
from django.core.management.base import BaseCommand, CommandError
|
||||||
|
import facebook
|
||||||
|
|
||||||
|
from main.models import UserProfile
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
"""
|
||||||
|
- Fetches members of KAK Facebook group
|
||||||
|
- Matches names against the local DB
|
||||||
|
- Saves the users Facebook picture in his/her user profile.
|
||||||
|
"""
|
||||||
|
KAK_GROUP_ID = '108265352570560'
|
||||||
|
graph = None
|
||||||
|
|
||||||
|
def get_kak_members(self):
|
||||||
|
members = self.graph.get_connections(id=self.KAK_GROUP_ID, connection_name='members')
|
||||||
|
return [(member['name'], member['id']) for member in members['data']]
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
if len(args) != 1:
|
||||||
|
raise CommandError('Missing access token')
|
||||||
|
|
||||||
|
access_token = args[0]
|
||||||
|
self.graph = facebook.GraphAPI(access_token=access_token)
|
||||||
|
|
||||||
|
group_members = self.get_kak_members()
|
||||||
|
|
||||||
|
names = [] # to fuzzy search for a name
|
||||||
|
name_lookup = {} # to update the matching UserProfile object
|
||||||
|
for u in User.objects.all().order_by('id'):
|
||||||
|
fullname = '{} {}'.format(u.first_name, u.last_name)
|
||||||
|
names.append(fullname)
|
||||||
|
name_lookup[fullname] = u.id
|
||||||
|
|
||||||
|
for member in group_members:
|
||||||
|
match = difflib.get_close_matches(member[0], names, 1) # max 1 match
|
||||||
|
if match:
|
||||||
|
u = UserProfile.objects.get(pk=name_lookup[match[0]])
|
||||||
|
if str(u.image) is "":
|
||||||
|
u.image = 'https://graph.facebook.com/{0}/picture'.format(member[1])
|
||||||
|
u.save()
|
||||||
|
|
||||||
+3
-1
@@ -2,6 +2,8 @@ Django>=1.6,<1.7
|
|||||||
django-bootstrap-form==3.1
|
django-bootstrap-form==3.1
|
||||||
django-compressor==1.4
|
django-compressor==1.4
|
||||||
django-widget-tweaks==1.3
|
django-widget-tweaks==1.3
|
||||||
MySQL-python==1.2.5
|
facebook-sdk==0.4.0
|
||||||
|
mysqlclient==1.3.6
|
||||||
Pillow==2.5.1
|
Pillow==2.5.1
|
||||||
|
requests==2.7.0
|
||||||
South==1.0
|
South==1.0
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
# coding: utf-8
|
|
||||||
|
|
||||||
"""
|
|
||||||
This script fetches the members of the KAK facebook group, matches names against the local db, and then saves the users facebook-picture in his/her user profile.
|
|
||||||
|
|
||||||
REQ:
|
|
||||||
https://github.com/iplatform/pyFaceGraph
|
|
||||||
INSTALL:
|
|
||||||
$ pip install pyfacegraph
|
|
||||||
"""
|
|
||||||
|
|
||||||
import os,sys,difflib
|
|
||||||
from facegraph import Graph
|
|
||||||
|
|
||||||
ACCESS_TOKEN = ''
|
|
||||||
PATH = '.'
|
|
||||||
KAK_GROUP_ID = '108265352570560'
|
|
||||||
if PATH not in sys.path:
|
|
||||||
sys.path.append(PATH)
|
|
||||||
|
|
||||||
from django.core.management import setup_environ
|
|
||||||
import settings
|
|
||||||
|
|
||||||
setup_environ(settings)
|
|
||||||
|
|
||||||
from main.models import *
|
|
||||||
|
|
||||||
g = Graph(ACCESS_TOKEN)
|
|
||||||
members = g[KAK_GROUP_ID]['members']()
|
|
||||||
group_members = [(member.name,member.id) for member in members.data]
|
|
||||||
names = [] # to fuzzy search for a name
|
|
||||||
name_lookup = {} # to update the matching UserProfile object
|
|
||||||
for u in User.objects.all().order_by('id'):
|
|
||||||
fullname = u.first_name + " " + u.last_name
|
|
||||||
names.append(fullname)
|
|
||||||
name_lookup[fullname] = u.id
|
|
||||||
|
|
||||||
for member in group_members:
|
|
||||||
match = difflib.get_close_matches(member[0], names, 1) # max 1 match
|
|
||||||
if match:
|
|
||||||
u = UserProfile.objects.get(pk=name_lookup[match[0]])
|
|
||||||
if str(u.image) is "":
|
|
||||||
# Note: This is not correct, image is only kind of a textfield
|
|
||||||
u.image = 'https://graph.facebook.com/{0}/picture'.format(member[1])
|
|
||||||
u.save()
|
|
||||||
|
|
||||||
@@ -385,39 +385,6 @@ $(document).ready(function() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* View: Add user
|
|
||||||
*/
|
|
||||||
var input_selector = "#id_facebook_username";
|
|
||||||
/* Facebook username check */
|
|
||||||
$(input_selector).on('keyup', _.debounce(function(event) {
|
|
||||||
var user_id = event.target.value;
|
|
||||||
if(user_id.trim().length === 0) {
|
|
||||||
$(input_selector).parent().removeClass('has-success has-feedback has-error');
|
|
||||||
$(input_selector).parent().find(".facebook-image img").attr('src', no_picture_url);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var picture_url = "https://graph.facebook.com/" + user_id + "/picture";
|
|
||||||
var static_url = $("meta[name=x-django-static-url]").attr('content');
|
|
||||||
var no_picture_url = static_url +"img/unknown_person.png";
|
|
||||||
$.ajax({
|
|
||||||
url: picture_url,
|
|
||||||
statusCode: {
|
|
||||||
200: function() {
|
|
||||||
$(input_selector).parent().addClass('has-success has-feedback');
|
|
||||||
$(input_selector).parent().removeClass('has-error');
|
|
||||||
$(input_selector).parent().find(".facebook-image img").attr('src', picture_url);
|
|
||||||
|
|
||||||
},
|
|
||||||
404: function() {
|
|
||||||
$(input_selector).parent().addClass('has-error has-feedback');
|
|
||||||
$(input_selector).parent().removeClass('has-success');
|
|
||||||
$(input_selector).parent().find(".facebook-image img").attr('src', no_picture_url);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, 300)); // Only every 300ms
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* View: Inventory report
|
* View: Inventory report
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -31,16 +31,8 @@
|
|||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="id_email">{{ form.email.label }}</label>{{ form.email|add_class:"form-control"}}{{ form.email.errors }}
|
<label for="id_email">{{ form.email.label }}</label>{{ form.email|add_class:"form-control"}}{{ form.email.errors }}
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
|
||||||
<label for="id_facebook_username">{{ form.facebook_username.label }}</label>
|
|
||||||
<div class="input-group">
|
|
||||||
{{ form.facebook_username|add_class:"form-control input-lg"|attr:"placeholder: F.eks simbras"}}{{form.facebook_username.error}}
|
|
||||||
<span class="input-group-addon facebook-image"><img src="{{ STATIC_URL}}img/unknown_person.png" /></span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
<input type="submit" value="Legg til" class="btn btn-primary btn-lg" />
|
<input type="submit" value="Legg til" class="btn btn-primary btn-lg" />
|
||||||
<a href="{% url 'register' %}" class="btn btn-default" />Tilbake</a>
|
<a href="{% url 'register' %}" class="btn btn-default">Tilbake</a>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
Reference in new issue
Block a user