def api(request): roles = request.user.groups.filter(name='Admins').exists() form = APIConsumerForm() if roles: messages.error(request, _('Users with the admin role cannot use the API.')) elif request.method == 'POST': if 'delete' in request.POST: try: consumer = Access.objects.get(pk=request.POST.get('consumer'), user=request.user) consumer.delete() except Access.DoesNotExist: messages.error(request, _('No such API key.')) else: access = Access.create_for_user(request.user) form = APIConsumerForm(request.POST, instance=access) if form.is_valid(): form.save() messages.success(request, _('New API key generated.')) else: access.delete() consumers = list(Access.objects.filter(user=request.user)) return render( request, 'developers/api.html', { 'consumers': consumers, 'roles': roles, 'form': form, 'domain': settings.DOMAIN, 'site_url': settings.SITE_URL })
def api(request): roles = request.user.groups.filter(name='Admins').exists() form = APIConsumerForm() if roles: messages.error(request, _('Users with the admin role cannot use the API.')) elif request.method == 'POST': if 'delete' in request.POST: try: consumer = Access.objects.get(pk=request.POST.get('consumer'), user=request.user) consumer.delete() except Access.DoesNotExist: messages.error(request, _('No such API key.')) else: access = Access.create_for_user(request.user) form = APIConsumerForm(request.POST, instance=access) if form.is_valid(): form.save() messages.success(request, _('New API key generated.')) else: access.delete() consumers = list(Access.objects.filter(user=request.user)) return render(request, 'developers/api.html', {'consumers': consumers, 'roles': roles, 'form': form, 'domain': settings.DOMAIN, 'site_url': settings.SITE_URL})
def api(request): roles = request.user.groups.filter(name="Admins").exists() form = APIConsumerForm() if roles: messages.error(request, _("Users with the admin role cannot use the API.")) elif request.method == "POST": if "delete" in request.POST: try: consumer = Access.objects.get(pk=request.POST.get("consumer"), user=request.user) consumer.delete() except Access.DoesNotExist: messages.error(request, _("No such API key.")) else: access = Access.create_for_user(request.user) form = APIConsumerForm(request.POST, instance=access) if form.is_valid(): form.save() messages.success(request, _("New API key generated.")) else: access.delete() consumers = list(Access.objects.filter(user=request.user)) return render( request, "developers/api.html", { "consumers": consumers, "roles": roles, "form": form, "domain": settings.DOMAIN, "site_url": settings.SITE_URL, }, )
def create_user(email, group_name=None, overwrite=False, oauth_key=None, oauth_secret=None): """Create an user if he doesn't exist already, assign him to a group and create a token for him. if ``overwrite=True`` then existing OAuth credentials for this user will be deleted, if any. If OAuth credentials are not specified, random key and secret will be generated. """ # Create the user. profile, created = UserProfile.objects.get_or_create( email=email, source=mkt.LOGIN_SOURCE_UNKNOWN, display_name=email) if not profile.read_dev_agreement: profile.read_dev_agreement = datetime.datetime.now() profile.save() # Now, find the group we want. if (group_name and not profile.groups.filter( groupuser__group__name=group_name).exists()): group = Group.objects.get(name=group_name) GroupUser.objects.create(group=group, user=profile) if overwrite: Access.objects.filter(user=profile.user).delete() if not Access.objects.filter(user=profile).exists(): if oauth_key and oauth_secret: Access.objects.create(user=profile, key=oauth_key, secret=oauth_secret) else: if oauth_key or oauth_secret: raise ValueError("Specify both of oauth_key and oauth_secret, " "or neither") Access.create_for_user(profile) return profile