def get_or_create_account(protocol, uid, owner_account = None): user_accounts = Account.objects.filter(uid=uid).filter(protocol=protocol) if len(user_accounts) == 0: user_account = Account() if owner_account is not None: if owner_account == uid: user_account.owned = True user_account.protocol = protocol user_account.uid = uid user_account.save() else: user_account = user_accounts[0] return user_account