Beispiel #1
0
def addgroup(request):
	if request.user.username == "":
		return HttpResponse('Please sign in!', status=401)
	if request.method == 'POST':
		form = AddgroupForm(request.POST) # A form bound to the POST data
		if form.is_valid():
			try: 
				old_group = MyGroup.objects.get(name=form.cleaned_data['group_name'])
				for name in form.cleaned_data['member_names']:
					user = MyUser.objects.get(username = name)
					old_group.users.add(user)
				old_group.save()
				return HttpResponseRedirect('/frontend/personal')				
			except ObjectDoesNotExist:
				this_user = MyUser.objects.get(username = 
							       request.user.username)
				new_group = MyGroup()
				new_group.creator = this_user.username
				new_group.name = form.cleaned_data['group_name']
				new_group.save()
				for name in form.cleaned_data['member_names']:
					user = MyUser.objects.get(username = name)
					new_group.users.add(user)
				new_group.users.add(this_user)
				new_group.save()
				return HttpResponseRedirect('/frontend/personal')
		else:
			print "not valid"
	return HttpResponseRedirect('/frontend/personal')
Beispiel #2
0
def importgroup(request, group, **kwargs):
    #import a group from Facebook
    def create_ret_user(user_info):
        #if user doesn't exist, create it. Return None if Facebook is
       	#missing information on them; otherwise, return MyUser object
        try:
            result = MyUser.objects.filter(
                username = user_info['username'])
            if len(result) != 0:
                assert(len(result) == 1)
                return result[0]
            new_user = MyUser(username = user_info['username'],
                              first_name = user_info['first_name'],
                              last_name = user_info['last_name'])
        except KeyError:
            return None
        new_user.save()
        return new_user
    if request.user.username == "":
        return HttpResponse('Please sign in!', status=401)
    this_user = MyUser.objects.get(username = request.user.username)	

    token = kwargs['access_token']
    graph = kwargs['graph']

    group_info = graph.get("/" + str(group))
    #check if this group already exists
    db_groups = MyGroup.objects.filter(creator=request.user.username,
                                       name=group_info['name'])
    if len(db_groups) != 0:
        return HttpResponse("Group already exists!", status=401)

    #save group, except for member info
    new_group = MyGroup()
    new_group.name = group_info['name']
    new_group.creator = request.user.username
    new_group.save()
    
    #save member info
    members = graph.get("/" + str(group) + "/members").get('data')
    for member in members:
        fb_user = graph.get("/" + member['id'])
        our_user = create_ret_user(fb_user) #MyUser object
        if our_user != None:
            new_group.users.add(our_user)
    new_group.save()
    return HttpResponseRedirect('/frontend/personal')