def upgrade_trial(request): """ View that handles upgrading from a Free account to a Trial account. """ userprofile = request.user.userprofile if userprofile.account_level.level == 'Paid': messages.error(request, 'You already have a Paid account.') return redirect('features') elif userprofile.account_level.level == 'Trial': messages.error(request, 'You already have a Trial account.') return redirect('features') # An account can only be upgraded to a Trial account once. elif userprofile.trial_signed_up: messages.error(request, 'You already tried a Trial account.') return redirect('features') trial_account = AccountLevel.objects.get(pk=4) userprofile.account_level = trial_account userprofile.trial_signed_up = True userprofile.trial_expiry_date = (datetime.date.today() + datetime.timedelta(days=DAYS_BEFORE_EXPIRATION)) userprofile.api_key = generate_api_key(API_KEY_LEN) userprofile.save() # Update user's videos expiration dates vids = Video.objects.filter(uploader=request.user) for vid in vids: is_expired(vid) messages.success(request, 'You have successfully upgraded to a Trial account.') return redirect('account_info')
def generate_admin_api_key(**kwargs): """ A function that checks if admin has no api key. If so, screenbird generates one for them randomly on the spot. """ try: if kwargs['request'].META['HTTP_REFERER'].split('/')[3] == 'admin': user_id = User.objects.get(username=kwargs['request'].POST['username']).id user = UserProfile.objects.get(user=user_id) if user.api_key == "" and len(user.api_key) == 0: user.api_key = generate_api_key(API_KEY_LEN) user.save() except: pass return
def test_generate_admin_api_key(self): """def generate_admin_api_key(request): if request.META['HTTP_REFERER'].split('/')[3] == 'admin': user_id = User.objects.get(username=request.POST['username']).id print request user = UserProfile.objects.get(user=user_id) if user.api_key == "" and len(user.api_key) == 0: user.api_key = generate_api_key(12) user.save() """ user = User.objects.create_superuser(username="******", email="*****@*****.**", password="******") # create fake request using django test Client c = Client() test_request = c.post('/', { 'username': user.username, }) print test_request.request environ = { 'HTTP_COOKIE': test_request.cookies, 'HTTP_REFERER': 'http://localhost:8000/admin/', 'PATH_INFO': '/', 'QUERY_STRING': '', 'REQUEST_METHOD': 'GET', 'SCRIPT_NAME': '', 'SERVER_NAME': 'testserver', 'SERVER_PORT': 80, 'SERVER_PROTOCOL': 'HTTP/1.1', } environ.update({}) environ.update(test_request.request) wsgi_request = WSGIRequest(environ) #this is the generate admin api key function in account app that needs to be tested if wsgi_request.META['HTTP_REFERER'].split('/')[3] == 'admin': user_id = User.objects.get( username=wsgi_request.POST['username']).id user = UserProfile.objects.get(user=user_id) if user.api_key == "" and len(user.api_key) == 0: user.api_key = generate_api_key(12) user.save() print user.api_key admin_user = UserProfile.objects.get(user=user.id) # check results self.assertEqual(len(admin_user.api_key), 12) logger.info('Api generate_admin_api_key test successful.')
def generate_admin_api_key(**kwargs): """ A function that checks if admin has no api key. If so, screenbird generates one for them randomly on the spot. """ try: if kwargs['request'].META['HTTP_REFERER'].split('/')[3] == 'admin': user_id = User.objects.get( username=kwargs['request'].POST['username']).id user = UserProfile.objects.get(user=user_id) if user.api_key == "" and len(user.api_key) == 0: user.api_key = generate_api_key(API_KEY_LEN) user.save() except: pass return
def test_generate_admin_api_key(self): """def generate_admin_api_key(request): if request.META['HTTP_REFERER'].split('/')[3] == 'admin': user_id = User.objects.get(username=request.POST['username']).id print request user = UserProfile.objects.get(user=user_id) if user.api_key == "" and len(user.api_key) == 0: user.api_key = generate_api_key(12) user.save() """ user = User.objects.create_superuser(username="******",email="*****@*****.**",password="******") # create fake request using django test Client c = Client() test_request = c.post('/', {'username': user.username,}) print test_request.request environ = { 'HTTP_COOKIE': test_request.cookies, 'HTTP_REFERER': 'http://localhost:8000/admin/', 'PATH_INFO': '/', 'QUERY_STRING': '', 'REQUEST_METHOD': 'GET', 'SCRIPT_NAME': '', 'SERVER_NAME': 'testserver', 'SERVER_PORT': 80, 'SERVER_PROTOCOL': 'HTTP/1.1', } environ.update({}) environ.update(test_request.request) wsgi_request = WSGIRequest(environ) #this is the generate admin api key function in account app that needs to be tested if wsgi_request.META['HTTP_REFERER'].split('/')[3] == 'admin': user_id = User.objects.get(username=wsgi_request.POST['username']).id user = UserProfile.objects.get(user=user_id) if user.api_key == "" and len(user.api_key) == 0: user.api_key = generate_api_key(12) user.save() print user.api_key admin_user = UserProfile.objects.get(user=user.id) # check results self.assertEqual(len(admin_user.api_key),12) logger.info('Api generate_admin_api_key test successful.')
def create_channel(request): """ Creates a group. """ if request.method == 'POST': form = ChannelForm(request.POST) if form.is_valid(): channel = form.save(commit=False) channel.owner = request.user channel.channel_slug = generate_channel_slug(settings.CHANNEL_SLUG_LENGTH) channel.api_link = generate_api_key(API_KEY_LEN) channel.save() channel.owner.userprofile.channels.add(channel.id) return redirect(reverse('update_channel', args=[channel.id,])) else: context = {'form': form} else: form = ChannelForm() context = {'form': form} return render(request, 'create_channel.html', context)
def create_channel(request): """ Creates a group. """ if request.method == 'POST': form = ChannelForm(request.POST) if form.is_valid(): channel = form.save(commit=False) channel.owner = request.user channel.channel_slug = generate_channel_slug( settings.CHANNEL_SLUG_LENGTH) channel.api_link = generate_api_key(API_KEY_LEN) channel.save() channel.owner.userprofile.channels.add(channel.id) return redirect(reverse('update_channel', args=[ channel.id, ])) else: context = {'form': form} else: form = ChannelForm() context = {'form': form} return render(request, 'create_channel.html', context)
def upgrade_trial(request): """ View that handles upgrading from a Free account to a Trial account. """ userprofile = request.user.userprofile if userprofile.account_level.level == 'Paid': messages.error(request, 'You already have a Paid account.') return redirect('features') elif userprofile.account_level.level == 'Trial': messages.error(request, 'You already have a Trial account.') return redirect('features') # An account can only be upgraded to a Trial account once. elif userprofile.trial_signed_up: messages.error(request, 'You already tried a Trial account.') return redirect('features') trial_account = AccountLevel.objects.get(pk=4) userprofile.account_level = trial_account userprofile.trial_signed_up = True userprofile.trial_expiry_date = ( datetime.date.today() + datetime.timedelta(days=DAYS_BEFORE_EXPIRATION)) userprofile.api_key = generate_api_key(API_KEY_LEN) userprofile.save() # Update user's videos expiration dates vids = Video.objects.filter(uploader=request.user) for vid in vids: is_expired(vid) messages.success(request, 'You have successfully upgraded to a Trial account.') return redirect('account_info')
def upgrade_account(request): """ View that handles upgrading from a Free account to a Paid account via Authorize.net Note: Authorize.net payment option is currently on backlog """ error_message = "" success_message = "" if request.user.userprofile.account_level.level == 'Paid': error_message = "You already have a Paid account." if request.method == 'POST': form = PaymentInformationForm(request.POST) if form.is_valid(): paid_account = AccountLevel.objects.get(pk=2) r = authorize.call_auth( paid_account, unicode(paid_account.authnet_item_price), data = { 'card_number':form.cleaned_data['card_number'], 'expiry':form.cleaned_data['expiry_date'], 'card_code':form.cleaned_data['card_code'], 'first_name':form.cleaned_data['first_name'], 'last_name':form.cleaned_data['last_name'], 'company':form.cleaned_data['company'], 'address':form.cleaned_data['address'], 'city':form.cleaned_data['city'], 'state':form.cleaned_data['state'], 'province':form.cleaned_data['province'], 'country':form.cleaned_data['country'], 'zip_code':form.cleaned_data['zip_code'], 'email':form.cleaned_data['email'], 'phone':form.cleaned_data['phone'] }, ) success_message = "" if r.split('|')[0] == '1': trans_id = r.split('|')[6] r = authorize.call_capture(trans_id) if r.split('|')[0] == '1': error_message = "" userprofile = UserProfile.objects.get(user__id=request.user.id) if userprofile.account_level.level == 'Trial': userprofile.trial_ended = True userprofile.trial_expiry_date = None userprofile.account_level = paid_account api_key_len = UserProfile._meta.get_field('api_key') userprofile.api_key = generate_api_key(api_key_len) userprofile.save() form = PaymentInformationForm(request.POST) success_message = "Payment Accepted" form = PaymentInformationForm(request.POST) # Update user's videos expiration dates vids = Video.objects.filter(uploader=request.user) for vid in vids: is_expired(vid) else: error_message = r.split('|')[3] form = PaymentInformationForm(request.POST) else: error_message = "%s" % (r.split('|')[3]) form = PaymentInformationForm(request.POST) else: form = PaymentInformationForm() context = { 'form': form, 'error_message':error_message, 'success_message':success_message, } return render(request, 'purchase.html', context)
def upgrade_account(request): """ View that handles upgrading from a Free account to a Paid account via Authorize.net Note: Authorize.net payment option is currently on backlog """ error_message = "" success_message = "" if request.user.userprofile.account_level.level == 'Paid': error_message = "You already have a Paid account." if request.method == 'POST': form = PaymentInformationForm(request.POST) if form.is_valid(): paid_account = AccountLevel.objects.get(pk=2) r = authorize.call_auth( paid_account, unicode(paid_account.authnet_item_price), data={ 'card_number': form.cleaned_data['card_number'], 'expiry': form.cleaned_data['expiry_date'], 'card_code': form.cleaned_data['card_code'], 'first_name': form.cleaned_data['first_name'], 'last_name': form.cleaned_data['last_name'], 'company': form.cleaned_data['company'], 'address': form.cleaned_data['address'], 'city': form.cleaned_data['city'], 'state': form.cleaned_data['state'], 'province': form.cleaned_data['province'], 'country': form.cleaned_data['country'], 'zip_code': form.cleaned_data['zip_code'], 'email': form.cleaned_data['email'], 'phone': form.cleaned_data['phone'] }, ) success_message = "" if r.split('|')[0] == '1': trans_id = r.split('|')[6] r = authorize.call_capture(trans_id) if r.split('|')[0] == '1': error_message = "" userprofile = UserProfile.objects.get( user__id=request.user.id) if userprofile.account_level.level == 'Trial': userprofile.trial_ended = True userprofile.trial_expiry_date = None userprofile.account_level = paid_account api_key_len = UserProfile._meta.get_field('api_key') userprofile.api_key = generate_api_key(api_key_len) userprofile.save() form = PaymentInformationForm(request.POST) success_message = "Payment Accepted" form = PaymentInformationForm(request.POST) # Update user's videos expiration dates vids = Video.objects.filter(uploader=request.user) for vid in vids: is_expired(vid) else: error_message = r.split('|')[3] form = PaymentInformationForm(request.POST) else: error_message = "%s" % (r.split('|')[3]) form = PaymentInformationForm(request.POST) else: form = PaymentInformationForm() context = { 'form': form, 'error_message': error_message, 'success_message': success_message, } return render(request, 'purchase.html', context)