def user_create_message(request): try: post_data = json.loads(request.body) user_id = post_data['user_id'] nickname = post_data['nickname'] #redundant but needed room = post_data['room_id'] message = post_data['message'] company_id = post_data['company_id'] if 'snapshot_id' in post_data: snapshot_id = post_data['snapshot_id'] else: snapshot_id = None company = Company.objects(company_id=company_id).first() company_id = company.id if snapshot_id is not None: chatUserMessage = ChatUserMessage(user=user_id, room=room, message=message, company=company_id, nickname=nickname, snapshot=snapshot_id) #, else: chatUserMessage = ChatUserMessage(user=user_id, room=room, message=message, company=company_id, nickname=nickname) chatUserMessage.save() serializer = ChatusermessageSerializer(chatUserMessage, many=False) return JsonResponse({"message": "Message created", "message" : serializer.data}, safe=False) except Exception as e: return JsonResponse({'Error' : str(e)})
def list(self, request, id=None): try: qryset = {'company_id__ne': 0} companies = Company.objects(**qryset).all() serializedList = CompanySerializer(companies, many=True) return Response(serializedList.data) except Exception as e: return Response(str(e))
def weeklyEmailCronJob(): print 'in email cron' try: logs = [] #holds all error messages for the job # first get the superadmin user and the companies user = _get_superadmin() if user is None: mail_admins('Could not find super admin!', 'Check settings') return # no superadmin found # remotely login the user host = settings.BASE_URL url = host + '/api/v1/auth/login/' creds = {'email': '*****@*****.**', 'password':'******'} s = requests.Session() resp = s.post(url, data=json.dumps(creds)) print 'resp is ' + str(resp.status_code) if not resp.status_code == 200: mail_admins('Could not login super admin!', 'Check credentials') logs.append('Could not login super admin!') return else: logs.append('Superadmin logged in') cookies = dict(sessionid = resp.cookies['sessionid']) url = host + '/api/v1/users/' resp = s.get(url, cookies=cookies) #print 'resp2 is ' + str(resp.status_code) #print str(logs) querydict = {'company_id__ne' : 0} companies = Company.objects(**querydict) print 'found companies ' + str(len(companies)) #now loop through each company find which systems are connected for company in companies: if not company.weekly_email: continue company_id = company.company_id company_name = company.name print 'in company ' + company.name # get dates from last Sunday to this Saturday today = datetime.datetime.now() start = today - datetime.timedelta((today.weekday() + 1) % 7) #this will give Mon = 0 all the way to Sat = 6 sat = start + relativedelta.relativedelta(weekday=relativedelta.SA(-1)) sun = sat + relativedelta.relativedelta(weekday=relativedelta.SU(-1)) #convert date strings to timestamps start_date = time.mktime(sun.timetuple()) * 1000 end_date = time.mktime(sat.timetuple()) * 1000 print 'start date ' + str(start_date) print 'end date ' + str(end_date) companyWeeklyEmail(company_id=company_id, start_date=start_date, end_date=end_date) except Exception as e: logs.append(str(e)) print 'exception during weekly cron job is ' + str(e)
def rooms(request, company_id): """ Homepage - lists all rooms """ #company_id = request.user.company company = Company.objects(company_id=company_id).first() company_id = company.id rooms = [] rooms = ChatRoom.objects(company=company_id).all() serializer = ChatroomSerializer(rooms, many=True) return Response(serializer.data)
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 getUserRooms(request, company_id): try: #print 'in user room' user_id = request.user.id #company_id = request.user.company company = Company.objects(company_id=company_id).first() company_id = company.id chatUsersRoomIds = ChatUser.objects(Q(user=user_id) & Q(company=company_id)) #subscribedRooms = ChatRoom.objects(id__in=chatUsersRoomIds) #print 'rooms are ' + str(len(subscribedRooms)) serializer = ChatuserSerializer(chatUsersRoomIds, many=True) return Response(serializer.data) except Exception as e: return Response('Error: ' + str(e))
def user_join_room(request): try: post_data = json.loads(request.body) user_id = post_data['user_id'] nickname = post_data['nickname'] room = post_data['room_id'] company_id = post_data['company_id'] company = Company.objects(company_id=company_id).first() company_id = company.id chatUser = ChatUser(user=user_id, room=room, company=company_id, nickname=nickname) chatUser.save() return JsonResponse({"message": "You have joined the channel", "roomId" : room}, safe=False) except Exception as e: return JsonResponse({'Error' : str(e)})
def getUserNotJoinedRooms(request, company_id): try: #print 'in user room' user_id = request.user.id #company_id = request.user.company company = Company.objects(company_id=company_id).first() company_id = company.id #print ' user id is ' + str(user_id) roomIds = [] chatUsers = ChatUser.objects(Q(user=user_id) & Q(company=company_id)).all() for chatUser in chatUsers: roomIds.append(chatUser.room.id) #print 'rooms1 are ' + str(len(chatUsersRoomIds)) notJoinedRooms = ChatRoom.objects(Q(id__nin=roomIds) & Q(company=company_id)) #print 'rooms are ' + str(len(subscribedRooms)) serializer = ChatroomSerializer(notJoinedRooms, many=True) return Response(serializer.data) except Exception as e: return Response(str(e))
def user_create_room(request): try: post_data = json.loads(request.body) user_id = post_data['user_id'] nickname = post_data['nickname'] #redundant but needed room_name = post_data['room_name'] room_description = post_data['room_description'] company_id = post_data['company_id'] company = Company.objects(company_id=company_id).first() company_id = company.id chatRoom = ChatRoom(owner=user_id, name=room_name, description=room_description, company=company_id, nickname=nickname) chatRoom.save() serializer = ChatroomSerializer(chatRoom, many=False) chatUser = ChatUser(user=user_id, room=chatRoom.id, company=company_id, nickname=nickname) chatUser.save() return JsonResponse({"message": "Channel created and you have joined it", "room" : serializer.data}, safe=False) except Exception as e: return JsonResponse({'Error' : str(e)})
def dailyCronJob(): print 'in cron' try: logs = [] #holds all error messages for the job # first get the superadmin user and the companies user = _get_superadmin() if user is None: mail_admins('Could not find super admin!', 'Check settings') return # no superadmin found # remotely login the user host = settings.BASE_URL url = host + '/api/v1/auth/login/' creds = {'email': '*****@*****.**', 'password':'******'} s = requests.Session() resp = s.post(url, data=json.dumps(creds)) print 'resp is ' + str(resp.status_code) if not resp.status_code == 200: mail_admins('Could not login super admin!', 'Check credentials') logs.append('Could not login super admin!') return else: logs.append('Superadmin logged in') cookies = dict(sessionid = resp.cookies['sessionid']) url = host + '/api/v1/users/' resp = s.get(url, cookies=cookies) print 'resp2 is ' + str(resp.status_code) print str(logs) querydict = {'company_id__ne' : 0} companies = Company.objects(**querydict) print 'found companies ' + str(len(companies)) #now loop through each company find which systems are connected for company in companies: company_id = company.company_id company_name = company.name print 'in company ' + company.name existingIntegration = CompanyIntegration.objects(company_id = company_id).first() if existingIntegration is None: # no integration found so move to next company logs.append('No integration record for company ' + str(company_name)) continue else: #skip this company if initial or delta run are in progress if 'initial_run_in_process' in existingIntegration and existingIntegration['initial_run_in_process'] == True: logs.append('Initial run in process for company ' + str(company_name)) continue if 'delta_run_in_process' in existingIntegration and existingIntegration['delta_run_in_process'] == True: logs.append('Delta run in process for company ' + str(company_name)) continue # look for either the last delta run date or the last initial run date sinceDateTime = None if 'delta_run_done' in existingIntegration: if 'delta_run_last_date' in existingIntegration: sinceDateTime = existingIntegration['delta_run_last_date'] if sinceDateTime is None: if 'initial_run_done' in existingIntegration: if 'initial_run_last_date' in existingIntegration: sinceDateTime = existingIntegration['initial_run_last_date'] if sinceDateTime is None: logs.append('No start date for delta run for company ' + str(company_name)) continue sinceDateTime = int(time.mktime(time.strptime(_str_from_date(sinceDateTime), '%Y-%m-%dT%H:%M:%SZ'))) sinceDateTime -= int(12*60*60) #move back 12 hours as a safety measure sinceDateTime = sinceDateTime * 1000 print 'calling extract with ' + str(sinceDateTime) companyDataExtract(user_id=None, company_id=company_id, run_type='delta', sinceDateTime=sinceDateTime) except Exception as e: logs.append(str(e)) print 'exception is ' + str(e)