def post_activitylog(request): if request.method != 'PATCH': return HttpResponse(status=405) json_data = json.loads(request.body) messages_delegate = MessagesDelegate(request) success = process_activitylog(messages_delegate, request.body) users = [] if 'users' in json_data: users = [user['username'] for user in json_data['users']] post_user = None for user in users: post_user = User.objects.filter(username=user).first() if post_user: break # If none of the users included exists in the server, we don't save the # file if post_user: username = users[0] if len(users) == 1 else 'activity' filename = '{}_{}.json'.format(username, timezone.now().strftime('%Y%m%d%H%M%S')) uploaded_activity_log = UploadedActivityLog(create_user=post_user) uploaded_activity_log.file.save(name=filename, content=ContentFile(request.body)) uploaded_activity_log.save() if success: return HttpResponse() else: return HttpResponseBadRequest()
def upload_view(request): if not request.user.userprofile.get_can_upload_activitylog(): raise exceptions.PermissionDenied if request.method == 'POST': form = UploadActivityLogForm(request.POST, request.FILES) if form.is_valid(): activity_log_file = request.FILES["activity_log_file"] # save activity_log_file uploaded_activity_log = UploadedActivityLog(create_user=request.user, file=activity_log_file) uploaded_activity_log.save() # open file and process file_data = open(uploaded_activity_log.file.path, 'rb').read() success = process_activitylog(request, file_data) if success: return HttpResponseRedirect(reverse('oppia_activitylog_upload_success')) form = UploadActivityLogForm() return render(request, 'oppia/activitylog/upload.html', {'form': form, 'title': _(u'Upload Activity Log')})
def upload_view(request): if not request.user.userprofile.get_can_upload_activitylog(): raise exceptions.PermissionDenied if request.method == 'POST': form = UploadActivityLogForm(request.POST, request.FILES) if form.is_valid(): activity_log_file = request.FILES["activity_log_file"] # save activity_log_file uploaded_activity_log = UploadedActivityLog( create_user=request.user, file=activity_log_file) uploaded_activity_log.save() # open file and process file_data = open(uploaded_activity_log.file.path, 'rb').read() success = process_activitylog(request, file_data) if success: return HttpResponseRedirect( reverse('oppia_activitylog_upload_success')) form = UploadActivityLogForm() return render(request, 'oppia/activitylog/upload.html', { 'form': form, 'title': _(u'Upload Activity Log') })
def form_valid(self, form): activity_log_file = self.request.FILES["activity_log_file"] # save activity_log_file uploaded_activity_log = UploadedActivityLog( create_user=self.request.user, file=activity_log_file) uploaded_activity_log.save() # open file and process with open(uploaded_activity_log.file.path, 'rb') as file: file_data = file.read() messages_delegate = MessagesDelegate(self.request) success, errors = process_activitylog(messages_delegate, file_data) if success: return super().form_valid(form) else: return super().form_invalid(form)
def post_activitylog(request): if request.method != 'PATCH': return HttpResponse(status=405) json_data = json.loads(request.body) messages_delegate = MessagesDelegate(request) success, errors = process_activitylog(messages_delegate, request.body) users = [] if 'users' in json_data: users = [user['username'] for user in json_data['users']] post_user = None for user in users: post_user = User.objects.filter(username=user).first() if post_user: break # If none of the users included exists in the server, we don't save the # file if post_user: username = users[0] if len(users) == 1 else 'activity' filename = '{}_{}.json'.format(username, timezone.now().strftime('%Y%m%d%H%M%S')) uploaded_activity_log = UploadedActivityLog(create_user=post_user) uploaded_activity_log.file.save(name=filename, content=ContentFile(request.body)) uploaded_activity_log.save() else: errors.append( DataRecovery.Reason.NONE_OF_THE_INCLUDED_USERS_EXIST_ON_THE_SERVER) if errors: DataRecovery.create_data_recovery_entry( user=post_user, data_type=DataRecovery.Type.ACTIVITY_LOG, reasons=errors, data=json_data) if success: return HttpResponse() else: return HttpResponseBadRequest()
def post(self, request): form = UploadActivityLogForm(request.POST, request.FILES) if form.is_valid(): activity_log_file = request.FILES["activity_log_file"] # save activity_log_file uploaded_activity_log = \ UploadedActivityLog(create_user=request.user, file=activity_log_file) uploaded_activity_log.save() # open file and process with open(uploaded_activity_log.file.path, 'rb') as file: file_data = file.read() success = process_activitylog(request, file_data) if success: return HttpResponseRedirect( reverse('activitylog:upload_success')) return render(request, 'common/upload.html', {'form': form, 'title': _(u'Upload Activity Log')})