Add commands for drawing the invite graph, and importing from drupal
This commit is contained in:
0
local/management/commands/__init__.py
Normal file
0
local/management/commands/__init__.py
Normal file
20
local/management/commands/create_invite_graph.py
Normal file
20
local/management/commands/create_invite_graph.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from datetime import datetime
|
||||
from django.db import connections, connection
|
||||
from local.models import Invite
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Generate a dot graph of invited users'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
print "digraph G {"
|
||||
print "rankdir=\"LR\""
|
||||
for i in Invite.objects.all():
|
||||
if i.claimer is None:
|
||||
continue
|
||||
if i.creator is None:
|
||||
continue
|
||||
print "\"%s\" -> \"%s\""%(i.creator.__unicode__(), i.claimer.__unicode__())
|
||||
print "}"
|
38
local/management/commands/import_invite_graph.py
Normal file
38
local/management/commands/import_invite_graph.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from datetime import datetime
|
||||
from django.db import connections, connection
|
||||
from local.models import Invite
|
||||
|
||||
class Command(BaseCommand):
|
||||
args = 'dbConfig'
|
||||
help = 'Synchronize from a drupal mysql database'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
if len(args) == 0:
|
||||
c = connection.cursor()
|
||||
else:
|
||||
c = connections[args[0]].cursor()
|
||||
c.execute("SELECT src.name AS inviter, dst.name AS invitee FROM users AS src LEFT JOIN invite ON invite.uid=src.uid LEFT JOIN users AS dst ON invite.invitee=dst.uid WHERE src.name IS NOT null AND dst.name IS NOT null")
|
||||
for link in c:
|
||||
if len(link[0]) == 0 or len(link[1]) == 0:
|
||||
continue
|
||||
try:
|
||||
sourceUser = User.objects.get(username__iexact=link[0])
|
||||
except ObjectDoesNotExist, e:
|
||||
print "Source user not found: %s"%(link[0])
|
||||
continue
|
||||
try:
|
||||
destUser = User.objects.get(username__iexact=link[1])
|
||||
except ObjectDoesNotExist, e:
|
||||
print "Destination user not found: %s"%(link[1])
|
||||
continue
|
||||
try:
|
||||
i = Invite.objects.get(creator=sourceUser, claimer=destUser)
|
||||
except ObjectDoesNotExist, e:
|
||||
Invite.objects.create(creator=sourceUser, claimer=destUser)
|
||||
print "Linked %s -> %s"%(sourceUser, destUser)
|
||||
continue
|
||||
print "Users already linked: %s -> %s"%(sourceUser, destUser)
|
||||
|
Reference in New Issue
Block a user