def manage_profile(request): """ The default accounts view. Provides user settings for managing profile, authentication, notifications, identities, and applications. """ django_user = request.user user_profile = TASUser(username=request.user.username) try: ds_profile = DesignSafeProfile.objects.get(user__id=django_user.id) except DesignSafeProfile.DoesNotExist: logout(request) return HttpResponseRedirect(reverse('designsafe_auth:login')) try: demographics = django_user.profile except ObjectDoesNotExist as e: demographics = {} logger.info('exception e:{} {}'.format(type(e), e)) context = { 'title': 'Account Profile', 'profile': user_profile, 'ds_profile': ds_profile, 'demographics': demographics, 'user': django_user, } return render(request, 'designsafe/apps/accounts/profile.html', context)
def manage_profile(request): """ The default accounts view. Provides user settings for managing profile, authentication, notifications, identities, and applications. """ django_user = request.user user_profile = TASUser(username=request.user.username) try: demographics = django_user.profile except ObjectDoesNotExist as e: demographics = {} logger.info('exception e:{} {}'.format(type(e), e)) context = { 'title': 'Account Profile', 'profile': user_profile, 'demographics': demographics } return render(request, 'designsafe/apps/accounts/profile.html', context)
def create_report(username, list_name): """ This task runs a celery task that creates a report of all DesignSafe users. It pulls data from both TAS and the Django user model, writes them to a CSV, and imports the CSV to the top-level of the user's My Data directory. """ user_list = get_user_model().objects.all() try: logger.info( "Creating user report for user=%s on " "default storage systemId=%s", username, settings.AGAVE_STORAGE_SYSTEM) csv_file = StringIO.StringIO() writer = csv.writer(csv_file) writer.writerow(["Last Name","First Name","Email","Phone Number","Institution",\ "Title", "Professional Level","Bio","NH_interests","Research Activities","Username",\ "Ethnicity","Gender","Country of residence","Citizenship","Date Account Created",\ ]) for user in user_list: if user.username.encode('utf-8') == 'EF-UF': writer.writerow([ 'Unable to find user data for username "' + user.username.encode('utf-8') + '"', ]) continue try: user_profile = TASUser(username=user) designsafe_user = get_user_model().objects.get(username=user) if hasattr(designsafe_user, "profile"): #making nh_interests QuerySet into list interests = designsafe_user.profile.nh_interests.all( ).values('description') nh_interests = [ interest['description'].encode('utf-8') for interest in interests ] #making research_activities QuerySet into list activities = designsafe_user.profile.research_activities.all( ).values('description') research_activities = [ activity['description'].encode('utf-8') for activity in activities ] # order of items as required by user writer.writerow([ user_profile.lastName.encode('utf-8') if user_profile.lastName else user_profile.lastName, user_profile.firstName.encode('utf-8') if user_profile.firstName else user_profile.firstName, user_profile.email, user_profile.phone, user_profile.institution.encode('utf-8'), user_profile.title, designsafe_user.profile.professional_level, designsafe_user.profile.bio.encode('utf-8') if designsafe_user.profile.bio else designsafe_user.profile.bio, nh_interests if nh_interests else None, research_activities if research_activities else None, user_profile, designsafe_user.profile.ethnicity, designsafe_user.profile.gender, user_profile.country, user_profile.citizenship, designsafe_user.date_joined.date() ]) else: writer.writerow([ 'Unable to find user data for username "' + user.username.encode('utf-8') + '"', ]) except: continue User = get_user_model().objects.get(username=username) client = User.agave_oauth.client setattr(csv_file, 'name', 'user_report.csv') client.files.importData(filePath=username, fileName='user_report.csv', systemId=settings.AGAVE_STORAGE_SYSTEM, fileToUpload=csv_file) csv_file.close() except (HTTPError, AgaveException): logger.exception('Failed to create user report.', extra={ 'user': username, 'systemId': settings.AGAVE_STORAGE_SYSTEM })