def list(self, request, companyid, id): user = CustomUser.objects(id=id).first() if user is not None: serializedData = CustomUserSerializer(user) return Response(serializedData.data) return HttpResponse("No editable record for " + id + " found!", status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, companyid, id): queryset = CustomUser.objects(id=id).first() if queryset is not None and self.request.method == 'DELETE': try: queryset.delete() except Exception as e: return HttpResponse(str(e), status=status.HTTP_400_BAD_REQUEST) return HttpResponse('User with ID ' + id + ' deleted', status=status.HTTP_200_OK)
def _get_superadmin(): try: result = None company = Company.objects(company_id=0).first() user = CustomUser.objects(company=company.id).first() if user is not None and user.is_superadmin == True: result = user return result except Exception as e: print 'exception ' + str(e) return str(e)
def put(self, request, companyid, id): user = CustomUser.objects(id=id).first() if user is not None: try: data = json.loads(request.body) user.email = data.get('email', None) user.username = data.get('username', None) user.timezone = data.get('timezone', None) user.save() password = data.get('password', None) confirm_password = data.get('confirm_password', None) if password and confirm_password and password == confirm_password: user.set_password(password) user.save() update_session_auth_hash(request, user) return HttpResponse('User ' + user.username + ' updated', status=status.HTTP_200_OK) except Exception as e: return HttpResponse(str(e), status=status.HTTP_400_BAD_REQUEST)
def exportToPdf(company_id, user_id, template_name, source_type, content_type, phantomjs_script, url, token, sessionid, authenticatedAccount, file_name): #, output, error try: user_id = ObjectId(user_id) output = NamedTemporaryFile(delete=False) error = NamedTemporaryFile(delete=False) external_process = Popen(["phantomjs", phantomjs_script, url, token, sessionid, authenticatedAccount, file_name], stdout=output, stderr=error) # external_process.communicate(30) export_file = open(file_name, 'rb') exportFile = ExportFile(company_id=company_id, owner_id=user_id, source=template_name, source_type=source_type, type=content_type, file_name=os.path.basename(file_name)) exportFile.file.put(export_file, content_type=content_type) exportFile.save() try: message = 'PDF file successfully exported' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Exports' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() user = CustomUser.objects(id=user_id).first() if user is not None: html_msg = '<p>Hola ' + user['first_name'] + '</p><p>Your export of data from ' + template_name + ' is ready. It is available in My Exports with the file name ' + os.path.basename(file_name) + '.</p><p>Cheers</p><p>The Claritix crew<p>' send_mail('[Claritix] Your PDF export is baked and ready', '', '*****@*****.**', [user['email']], html_message=html_msg) except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) except Exception as e: print 'exception was ' + str(e) return str(e)
def exportToCsv(object_type, system_code, data, source_type, chart_name, user_id, company_id): print 'in export to csv' if object_type is None or system_code is None or data is None: print 'returning due to none' return try: if object_type == 'lead': result = exportLeadsToCsv(system_code, data, chart_name, user_id, company_id) print 'got result ' + str(result) file_name = result['file_name'] if file_name != '': content_type = result['content_type'] export_file = open(file_name, 'rb') exportFile = ExportFile(company_id=company_id, owner_id=user_id, source=chart_name, source_type=source_type, type=content_type, file_name=os.path.basename(file_name)) exportFile.file.put(export_file, content_type=content_type) exportFile.save() try: message = 'CSV file successfully exported' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Exports' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() user = CustomUser.objects(id=user_id).first() if user is not None: html_msg = '<p>Hola ' + user['first_name'] + '</p><p>Your download of data from ' + chart_name + ' is ready. It is available in My Exports with the file name ' + os.path.basename(file_name) + '.</p><p>Cheers</p><p>The Claritix crew<p>' send_mail('[Claritix] Your CSV export is baked and ready', '', '*****@*****.**', [user['email']], html_message=html_msg) except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) else: try: message = 'CSV download failed' notification = Notification() #notification.company_id = company_id notification.owner = user_id notification.module = 'Exports' notification.type = 'Background task' notification.method = os.path.basename(__file__) notification.message = message notification.success = True notification.read = False notification.save() user = CustomUser.objects(id=user_id).first() if user is not None: html_msg = '<p>Hola ' + user['first_name'] + '</p><p>Your download of data from ' + chart_name + ' failed. Please contact the Claritix team so that we can look into this.</p><p>Cheers</p><p>The Claritix crew<p>' send_mail('[Claritix] Oh no! Your CSV download failed', '', '*****@*****.**', [user['email']], html_message=html_msg) except Exception as e: send_notification(dict( type='error', success=False, message=str(e) )) else: return except Exception as e: print 'exception while trying to save CSV file: ' + str(e) send_notification(dict(type='error', success=False, message=str(e)))