def add_consumer(request): """ Piston Consumer form if upon POST save else render form New API key request """ form_class = ConsumerForm entry_class = Consumer if request.method == 'POST': form = form_class(request.POST) if form.is_valid(): entry = Consumer(**form.cleaned_data) entry.user = request.user entry.key, entry.secret = Consumer.objects.generate_random_codes() entry.status = 'pending' # Save the entry to the DB entry.save() return HttpResponseRedirect(reverse('key_list')) else: initial = { } form = form_class(initial=initial) context = { 'title': 'Get an API key', 'form_button': 'Get the key NOW!', 'form': form, } return render_to_response('key/edit.html', context, context_instance=RequestContext(request))
def handle(self, *args, **options): from django.contrib.auth.models import User from piston.models import Consumer app = Consumer(status = 'accepted') username = raw_input('Username of the app\'s owner: ') while not username: username = raw_input('Username of the app\'s owner (required): ') try: app.user = User.objects.get(username = username) except User.DoesNotExist: raise CommandError('User %s not found' % username) name = raw_input('App name: ') while not name: name = raw_input('App name (required): ') app.name = name app.description = raw_input('App description (optional): ') app.generate_random_codes() app.save() print '\nApplication name: %s\nApplication creator: %s\nAPI key: %s\nAPI secret: %s' % ( app.name, app.user.get_full_name() or app.user.username, app.key, app.secret )