diff --git a/main/management/__init__.py b/main/management/__init__.py new file mode 100644 index 0000000..89ba269 --- /dev/null +++ b/main/management/__init__.py @@ -0,0 +1 @@ +__author__ = 'Nikolai' diff --git a/main/management/commands/__init__.py b/main/management/commands/__init__.py new file mode 100644 index 0000000..89ba269 --- /dev/null +++ b/main/management/commands/__init__.py @@ -0,0 +1 @@ +__author__ = 'Nikolai' diff --git a/main/management/commands/facebook_sync.py b/main/management/commands/facebook_sync.py new file mode 100644 index 0000000..9d8c1e6 --- /dev/null +++ b/main/management/commands/facebook_sync.py @@ -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() + diff --git a/requirements.txt b/requirements.txt index 62bc23f..84a3de6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,8 @@ Django>=1.6,<1.7 django-bootstrap-form==3.1 django-compressor==1.4 django-widget-tweaks==1.3 -MySQL-python==1.2.5 +facebook-sdk==0.4.0 +mysqlclient==1.3.6 Pillow==2.5.1 +requests==2.7.0 South==1.0 diff --git a/scripts/facebook_sync.py b/scripts/facebook_sync.py deleted file mode 100755 index b1d8a03..0000000 --- a/scripts/facebook_sync.py +++ /dev/null @@ -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() - diff --git a/static/app/js/app.js b/static/app/js/app.js index 6eb29c9..64481a5 100644 --- a/static/app/js/app.js +++ b/static/app/js/app.js @@ -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 */ diff --git a/templates/registration/create_user.html b/templates/registration/create_user.html index f3d8615..0710129 100644 --- a/templates/registration/create_user.html +++ b/templates/registration/create_user.html @@ -31,16 +31,8 @@
-