def complete(request): """ Receive user from Open Humans. Store data, start data upload task. """ logger.debug("Received user returning from Open Humans.") form = None if request.method == 'GET': # Exchange code for token. # This creates an OpenHumansMember and associated User account. code = request.GET.get('code', '') oh_member = oh_code_to_member(code=code) if oh_member: # Log in the user. user = oh_member.user login(request, user, backend='django.contrib.auth.backends.ModelBackend') elif not request.user.is_authenticated: logger.debug('Invalid code exchange. User returned to start page.') return redirect('/') else: oh_member = request.user.openhumansmember if get_file_url(oh_member.oh_id) is not None: return redirect('dashboard') form = UploadFileForm() context = { 'oh_id': oh_member.oh_id, 'oh_member': oh_member, 'oh_proj_page': settings.OH_ACTIVITY_PAGE, 'form': form } return render(request, 'users/complete.html', context=context) elif request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): metadata = { 'tags': ['twitter', 'twitter-archive'], 'description': 'Twitter achive file.' } upload_file_to_oh(request.user.openhumansmember, request.FILES['file'], metadata) else: logger.debug('INVALID FORM') import_data.delay(request.user.openhumansmember.oh_id) return redirect('dashboard')
def dashboard(request): """ Give options to delete account, make data public/private, reupload archive, trigger new parsing of archive. """ if request.user.is_authenticated: oh_member = request.user.openhumansmember has_data = bool(get_file_url(oh_member.oh_id)) context = {'client_id': settings.OH_CLIENT_ID, 'oh_proj_page': settings.OH_ACTIVITY_PAGE, 'oh_member': oh_member, 'has_data': has_data} return render(request, 'users/dashboard.html', context=context) return redirect("/")