def register_new_user(request): json_obj=json.loads(request.body) json_obj=json_obj['userInfo'] if User.objects.filter(username = json_obj['userName']).exists(): print "Username already Exist." return HttpResponse(json.dumps({"validation":"Username is already exist.","status":False}), content_type="application/json") username = json_obj['userName'] first_name = json_obj['firstName'] last_name = json_obj['lastName'] password = json_obj['password'] password1 = json_obj['confirmPassword'] if password != password1: print "Passwords Are not Matching" return HttpResponse(json.dumps({"validation":"Passwords are not Matched","status":False}), content_type="application/json") email = validateEmail(json_obj['email']) if email != True: print "Email is already Exist." return HttpResponse(json.dumps({"validation":"Email is already exist.Try with another Email.","status":False}), content_type="application/json") else: email = json_obj['email'] if json_obj['addressLine1'] is None: return HttpResponse(json.dumps({"validation":"Please Enter Your Address...!","status":False}), content_type="application/json") else: address_line1 = json_obj['addressLine1'] address_line2 = json_obj['addressLine2'] contact_no = json_obj['mobileNo0'] contact_no = int(contact_no) contact_no = validate_mobile(str(contact_no)) if contact_no == False: return HttpResponse(json.dumps([{"validation": "This mobile number is already used..please try with another one.", "status": False}]), content_type = "application/json") else: contact_no1 = json_obj['mobileNo1'] city = json_obj['city'] state = json_obj['state'] country = json_obj['country'] pin_code = json_obj['pincode'] accounttype_obj = AccountType(optionType=1) accounttype_obj.save() group_obj_for_bank_acc = Group(optionType=0) group_obj_for_bank_acc.save() group_obj_for_cash_acc = Group(optionType=4) group_obj_for_cash_acc.save() bank_account_name = "My Bank Account" cash_account_name = "My Cash Account" bank_account_obj = Account(account_name=bank_account_name,first_name=first_name,last_name=last_name,contact_no=contact_no,address_line1=address_line1,city=city,state=state,country=country,pin_code=pin_code,accounttype=accounttype_obj,group=group_obj_for_bank_acc) bank_account_obj.save() cash_account_obj = Account(account_name=cash_account_name,first_name=first_name,last_name=last_name,contact_no=contact_no,address_line1=address_line1,city=city,state=state,country=country,pin_code=pin_code,accounttype=accounttype_obj,group=group_obj_for_cash_acc) cash_account_obj.save() user_obj = User(first_name=first_name,last_name=last_name,username=username,email=email,password=password) user_obj.set_password(password) user_obj.save() userdetail_obj = UserDetail(user=user_obj,address_line1=address_line1,address_line2=address_line2,contact_no=contact_no,city=city, state=state,country=country,pin_code=pin_code,contact_no1=contact_no1,bank_account=bank_account_obj,cash_account=cash_account_obj) userdetail_obj.save() print "Registration Successful" return HttpResponse(json.dumps({"validation":"Registration Successful.","redirecturl":"#/login","status":True}), content_type="application/json")
def add_group(request): if request.user.is_authenticated(): json_obj = json.loads(request.body) group_name = json_obj['group_name'] group_obj = Group(group_name=group_name) group_obj.save() return HttpResponse(json.dumps({"validation":"New Group created Successfully...","status":True}), content_type="application/json") else: return HttpResponse(json.dumps({"validation":"You are not logged in yet.Please login to continue."}), content_type="application/json")
def save_edit_account(request): if request.user.is_authenticated(): json_obj = json.loads(request.body) accountInfo = json_obj['accountInfo'] account_id = json_obj["account_id"] account_name = accountInfo.get("account_name") alias = accountInfo.get("alias") group = accountInfo.get("group") grouptype = group.get("id") first_name = accountInfo.get("firstName") last_name = accountInfo.get("lastName") email = accountInfo.get("email") address_line1 = accountInfo.get("addressLine1") address_line2 = accountInfo.get("addressLine2") city = accountInfo.get("city") state = accountInfo.get("state") country = accountInfo.get("country") pin_code = accountInfo.get("pincode") contact_no = accountInfo.get("mobileNo0") contact_no1 = accountInfo.get("mobileNo1") opening_balance = accountInfo.get("openingBalance") accounttype = accountInfo.get("accounttype") accountTypeOptionId = accounttype.get("id") user_obj = User.objects.get(id=request.user.id) accounttype_obj = AccountType.objects.get(optionType = accountTypeOptionId) group_obj = Group(optionType=grouptype) group_obj.save() account_obj = Account.objects.get(id=account_id) account_obj.account_name = account_name account_obj.first_name = first_name account_obj.last_name = last_name account_obj.alias = alias account_obj.email = email account_obj.address_line1 = address_line1 account_obj.address_line2 = address_line2 account_obj.contact_no = contact_no account_obj.contact_no1 = contact_no1 account_obj.city = city account_obj.state = state account_obj.country = country account_obj.pin_code = pin_code account_obj.opening_balance = opening_balance account_obj.group = group_obj account_obj.accounttype = accounttype_obj account_obj.save() userdetail_obj = UserDetail.objects.get(user__id=request.user.id) userdetail_obj.account.add(account_obj) userdetail_obj.save() return HttpResponse(json.dumps({"validation":"Account details updated Successfully","status":True}), content_type="application/json") else: return HttpResponse(json.dumps({"validation":"You are not logged in yet.Please login to continue."}), content_type="application/json")
def create_new_user_account(request): if request.user.is_authenticated(): print request.body print request.user json_obj = json.loads(request.body) accountInfo = json_obj['accountInfo'] account_name = accountInfo.get("account_name") alias = accountInfo.get("alias") group = accountInfo.get("group") grouptype = group.get("id") first_name = accountInfo.get("firstName") last_name = accountInfo.get("lastName") email = accountInfo.get("email") address_line1 = accountInfo.get("addressLine1") address_line2 = accountInfo.get("addressLine2") city = accountInfo.get("city") state = accountInfo.get("state") country = accountInfo.get("country") try: pin_code = accountInfo.get("pincode") except ValueError: return HttpResponse(json.dumps([{"validation": "Please Enter Valid PinCode.", "status": False}]), content_type = "application/json") contact_no = accountInfo.get("mobileNo0") contact_no1 = accountInfo.get("mobileNo1") opening_balance = accountInfo.get("openingBalance") accounttype = accountInfo.get("accounttype") accounttype = accounttype.get("id") user_obj = User.objects.get(id=request.user.id) accounttype_obj = AccountType(optionType=accounttype) accounttype_obj.save() group_obj = Group(optionType=grouptype) group_obj.save() account_obj = Account(first_name=first_name,last_name=last_name,email=email,address_line1=address_line1, city=city,state=state,country=country,pin_code=pin_code,contact_no=contact_no ,contact_no1=contact_no1,account_name=account_name,opening_balance=opening_balance,group=group_obj,accounttype=accounttype_obj) account_obj.save() userdetail_obj = UserDetail.objects.get(user__id=request.user.id) userdetail_obj.account.add(account_obj) userdetail_obj.save() return HttpResponse(json.dumps({"validation":"New Account created Successfully","status":True}), content_type="application/json") else: return HttpResponse(json.dumps({"validation":"You are not logged in yet.Please login to continue."}), content_type="application/json")
def add_group(request): if request.user.is_authenticated(): json_obj = json.loads(request.body) group_name = json_obj['group_name'] group_obj = Group(group_name=group_name) group_obj.save() return HttpResponse(json.dumps({ "validation": "New Group created Successfully...", "status": True }), content_type="application/json") else: return HttpResponse(json.dumps({ "validation": "You are not logged in yet.Please login to continue." }), content_type="application/json")
def create_group(name, description, parent, parameter_names=None): try: group_type = GroupType.objects.first() group = Group(name=name, description=description, parent=parent, type=group_type) group.save() # set permissions for group gp = GlobalPreferences.get() gp.group_type_order_filter.add(group.type) for profile in UserProfile.objects.all(): GroupRoleMembership.objects.bulk_create([ GroupRoleMembership(group=group, profile=profile, role=role) for role in Role.assignable_objects.all() ]) group.save() if parameter_names: for parameter_name in parameter_names: add_parameter(object=group, parameter_name=parameter_name) return True, group except: return False, name
def _contract_remove_users_view(self, contract_id, dry_run = False): contract = Contract.objects.get(id=contract_id) contract_archive_group = u'Espaço Aberto %s' % (contract.name,) if not Group.objects.filter(name=contract_archive_group).exists(): contract_archive_group = Group(name=contract_archive_group) contract_archive_group.save() else: contract_archive_group = Group.objects.get(name=contract_archive_group) groups_add = [Group.objects.get(name="Espaço Aberto"), Group.objects.get(name="students"), contract_archive_group] errors = { 'num_errors' : 0, 'email_with_error' : [], } stats = { "users_to_add" : 0, } users_to_remove = TimtecUser.objects\ .exclude(is_superuser=True, is_staff=True)\ .exclude(groups=Group.objects.get(name="Espaço Aberto")) stats["users_to_add"] = len(users_to_remove) if errors['num_errors'] == 0: groups_remove = contract.groups \ .exclude(Q(name="Avante") | Q(name="Entremeios")) \ .values_list('id', flat=True) contract_classes = contract.classes.exclude(name__contains="ARQUIVO_") archive_classes = {} for c in contract_classes: archive_class_name = "%s%s@%s" % \ ( CONTRACT_ARCHIVE_CLASS_PREFIX, c.course.name, contract.name ) if not Class.objects \ .filter(name=archive_class_name, course=c.course, contract__id=contract_id).exists(): archive_class = Class(name=archive_class_name, course=c.course,) archive_class.save() archive_class.contract.add(contract_id,) else: archive_class = Class.objects.get(name=archive_class_name, course=c.course, contract__id=contract_id) archive_classes[c.name + c.course.name] = archive_class from django.db import transaction with transaction.atomic(): for u in users_to_remove: for g in groups_remove: if not dry_run: u.groups.remove(g) for g in groups_add: if not dry_run: u.groups.add(g) for c in u.classes.all().exclude(name__contains="ARQUIVO_"): if c.contract.first() and \ c.contract.first().id == contract.id: if not dry_run: c.remove_students(u) archive_classes[c.name + c.course.name] \ .add_students(u) return (stats, errors) else: return errors
def createGroup(request): rtn_dict = {'success': False, "msg": ""} if request.method == 'POST': try: ''' if not request.user.id: user_id = request.POST['user'] else: user_id = request.user.id ''' #account = Account.objects.get(user__id=user_id) account = Account.objects.get(user=request.user) group = Group(group_creator=account) group.name = request.POST['name'] group.save() members = ast.literal_eval(request.POST['members']) for member_id in members: friend = Account.objects.get(pk=member_id, is_active=True) group.members.add(friend) group.members.add(account) ''' r = R.r r_group_key = 'group.{0}.hash'.format(group.id) pushToNOSQLHash(r_group_key, model_to_dict(group)) message = "You have been added to group {0} by {1}".format(group.name, group.group_creator.user_name) custom_payload = {'creator_name': group.group_creator.user_name, 'creator_id': group.group_creator.id, 'group_name': group.name, 'group_id': group.id} custom_payload = json.dumps(custom_payload) notification = createNotification(message, custom_payload) #add group to groups for members for member in group.members.all(): #creating notification to send to group members recipient = member.user notification.recipients.add(recipient) addNotificationToRedis(notification, member.id) r_groups_key = 'account.{0}.groups.set'.format(member.id) pushToNOSQLSet(r_groups_key, group.id, False, 0) ''' ''' Send notifications to group members ''' try: message = "{0} added you to group {1}".format(group.group_creator.user_name, group.name) custom_payload = None members = group.members.all() device_tokens = [] recipients = [] for member in members: try: user = member.user device = Device.objects.get(users__pk=user.id) device_tokens.append(device.token) recipients.append(user) except: pass print 'recipients :{0}'.format(recipients) custom_payload = {'notification_type': 'group_added', 'group_id': group.id, 'group_name': group.name, 'creator_id': account.id} custom_payload = json.dumps(custom_payload) notification = createNotification(message, 'group_added', custom_payload, recipients) sendNotification(notification, device_tokens) except Exception as e: print 'Error sending push notification: {0}'.format(e) except Exception as e: print 'Error creating group: {0}'.format(e) logger.info('Error creating group: {0}'.format(e)) rtn_dict['msg'] = 'Error creating group: {0}'.format(e) return HttpResponse(json.dumps(rtn_dict, cls=DjangoJSONEncoder), content_type="application/json")
def contract_remove_users_view(request): CONTRACT_ARCHIVE_CLASS_PREFIX = "ARQUIVO_" contract_id = request.data.get('contract', None) if not contract_id: return Response({"error" : "Contrato não informado"}, status=status.HTTP_400_BAD_REQUEST) users = request.data.get('users', None) if users: users = users.split('\n') else: return Response({"error" : "Erro ao receber lista de usuários"}, status=status.HTTP_400_BAD_REQUEST) contract = Contract.objects.get(id=contract_id) contract_archive_group = u'Espaço Aberto %s' % (contract.name,) if not Group.objects.filter(name=contract_archive_group).exists(): contract_archive_group = Group(name=contract_archive_group) contract_archive_group.save() else: contract_archive_group = Group.objects.get(name=contract_archive_group) groups_add = [Group.objects.get(name="Espaço Aberto"), Group.objects.get(name="students"), contract_archive_group] errors = { 'num_errors' : 0, 'email_with_error' : [], } users_to_remove = [] for u in users: if TimtecUser.objects.filter(email=u).exists(): user = TimtecUser.objects.get(email=u) users_to_remove.append(user) else: errors['num_errors']+=1 errors['email_with_error'].append(u) if errors['num_errors'] == 0: groups_remove = contract.groups.values_list('id', flat=True) contract_classes = contract.classes.exclude(name__contains="ARQUIVO_") archive_classes = {} for c in contract_classes: archive_class_name = "%s%s@%s" % \ ( CONTRACT_ARCHIVE_CLASS_PREFIX, c.course.name, contract.name ) if not Class.objects \ .filter(name=archive_class_name, course=c.course, contract__id=contract_id).exists(): archive_class = Class(name=archive_class_name, course=c.course,) archive_class.save() archive_class.contract.add(contract_id,) else: archive_class = Class.objects.get(name=archive_class_name, course=c.course, contract__id=contract_id) archive_classes[c.name + c.course.name] = archive_class from django.db import transaction with transaction.atomic(): for u in users_to_remove: for g in groups_remove: u.groups.remove(g) for g in groups_add: u.groups.add(g) for c in u.classes.all().exclude(name__contains="ARQUIVO_"): if c.contract.first().id == contract.id: c.remove_students(u) archive_classes[c.name + c.course.name].add_students(u) return Response({}, status=status.HTTP_200_OK) else: return Response(errors, status=status.HTTP_400_BAD_REQUEST)
def save_edit_account(request): if request.user.is_authenticated(): json_obj = json.loads(request.body) accountInfo = json_obj['accountInfo'] account_id = json_obj["account_id"] account_name = accountInfo.get("account_name") alias = accountInfo.get("alias") group = accountInfo.get("group") grouptype = group.get("id") first_name = accountInfo.get("firstName") last_name = accountInfo.get("lastName") email = accountInfo.get("email") address_line1 = accountInfo.get("addressLine1") address_line2 = accountInfo.get("addressLine2") city = accountInfo.get("city") state = accountInfo.get("state") country = accountInfo.get("country") pin_code = accountInfo.get("pincode") contact_no = accountInfo.get("mobileNo0") contact_no1 = accountInfo.get("mobileNo1") opening_balance = accountInfo.get("openingBalance") accounttype = accountInfo.get("accounttype") accountTypeOptionId = accounttype.get("id") user_obj = User.objects.get(id=request.user.id) accounttype_obj = AccountType.objects.get( optionType=accountTypeOptionId) group_obj = Group(optionType=grouptype) group_obj.save() account_obj = Account.objects.get(id=account_id) account_obj.account_name = account_name account_obj.first_name = first_name account_obj.last_name = last_name account_obj.alias = alias account_obj.email = email account_obj.address_line1 = address_line1 account_obj.address_line2 = address_line2 account_obj.contact_no = contact_no account_obj.contact_no1 = contact_no1 account_obj.city = city account_obj.state = state account_obj.country = country account_obj.pin_code = pin_code account_obj.opening_balance = opening_balance account_obj.group = group_obj account_obj.accounttype = accounttype_obj account_obj.save() userdetail_obj = UserDetail.objects.get(user__id=request.user.id) userdetail_obj.account.add(account_obj) userdetail_obj.save() return HttpResponse(json.dumps({ "validation": "Account details updated Successfully", "status": True }), content_type="application/json") else: return HttpResponse(json.dumps({ "validation": "You are not logged in yet.Please login to continue." }), content_type="application/json")
def create_new_user_account(request): if request.user.is_authenticated(): print request.body print request.user json_obj = json.loads(request.body) accountInfo = json_obj['accountInfo'] account_name = accountInfo.get("account_name") alias = accountInfo.get("alias") group = accountInfo.get("group") grouptype = group.get("id") first_name = accountInfo.get("firstName") last_name = accountInfo.get("lastName") email = accountInfo.get("email") address_line1 = accountInfo.get("addressLine1") address_line2 = accountInfo.get("addressLine2") city = accountInfo.get("city") state = accountInfo.get("state") country = accountInfo.get("country") try: pin_code = accountInfo.get("pincode") except ValueError: return HttpResponse(json.dumps([{ "validation": "Please Enter Valid PinCode.", "status": False }]), content_type="application/json") contact_no = accountInfo.get("mobileNo0") contact_no1 = accountInfo.get("mobileNo1") opening_balance = accountInfo.get("openingBalance") accounttype = accountInfo.get("accounttype") accounttype = accounttype.get("id") user_obj = User.objects.get(id=request.user.id) accounttype_obj = AccountType(optionType=accounttype) accounttype_obj.save() group_obj = Group(optionType=grouptype) group_obj.save() account_obj = Account(first_name=first_name, last_name=last_name, email=email, address_line1=address_line1, city=city, state=state, country=country, pin_code=pin_code, contact_no=contact_no, contact_no1=contact_no1, account_name=account_name, opening_balance=opening_balance, group=group_obj, accounttype=accounttype_obj) account_obj.save() userdetail_obj = UserDetail.objects.get(user__id=request.user.id) userdetail_obj.account.add(account_obj) userdetail_obj.save() return HttpResponse(json.dumps({ "validation": "New Account created Successfully", "status": True }), content_type="application/json") else: return HttpResponse(json.dumps({ "validation": "You are not logged in yet.Please login to continue." }), content_type="application/json")
def register_new_user(request): json_obj = json.loads(request.body) json_obj = json_obj['userInfo'] if User.objects.filter(username=json_obj['userName']).exists(): print "Username already Exist." return HttpResponse(json.dumps({ "validation": "Username is already exist.", "status": False }), content_type="application/json") username = json_obj['userName'] first_name = json_obj['firstName'] last_name = json_obj['lastName'] password = json_obj['password'] password1 = json_obj['confirmPassword'] if password != password1: print "Passwords Are not Matching" return HttpResponse(json.dumps({ "validation": "Passwords are not Matched", "status": False }), content_type="application/json") email = validateEmail(json_obj['email']) if email != True: print "Email is already Exist." return HttpResponse(json.dumps({ "validation": "Email is already exist.Try with another Email.", "status": False }), content_type="application/json") else: email = json_obj['email'] if json_obj['addressLine1'] is None: return HttpResponse(json.dumps({ "validation": "Please Enter Your Address...!", "status": False }), content_type="application/json") else: address_line1 = json_obj['addressLine1'] address_line2 = json_obj['addressLine2'] contact_no = json_obj['mobileNo0'] contact_no = int(contact_no) contact_no = validate_mobile(str(contact_no)) if contact_no == False: return HttpResponse(json.dumps([{ "validation": "This mobile number is already used..please try with another one.", "status": False }]), content_type="application/json") else: contact_no1 = json_obj['mobileNo1'] city = json_obj['city'] state = json_obj['state'] country = json_obj['country'] pin_code = json_obj['pincode'] accounttype_obj = AccountType(optionType=1) accounttype_obj.save() group_obj_for_bank_acc = Group(optionType=0) group_obj_for_bank_acc.save() group_obj_for_cash_acc = Group(optionType=4) group_obj_for_cash_acc.save() bank_account_name = "My Bank Account" cash_account_name = "My Cash Account" bank_account_obj = Account(account_name=bank_account_name, first_name=first_name, last_name=last_name, contact_no=contact_no, address_line1=address_line1, city=city, state=state, country=country, pin_code=pin_code, accounttype=accounttype_obj, group=group_obj_for_bank_acc) bank_account_obj.save() cash_account_obj = Account(account_name=cash_account_name, first_name=first_name, last_name=last_name, contact_no=contact_no, address_line1=address_line1, city=city, state=state, country=country, pin_code=pin_code, accounttype=accounttype_obj, group=group_obj_for_cash_acc) cash_account_obj.save() user_obj = User(first_name=first_name, last_name=last_name, username=username, email=email, password=password) user_obj.set_password(password) user_obj.save() userdetail_obj = UserDetail(user=user_obj, address_line1=address_line1, address_line2=address_line2, contact_no=contact_no, city=city, state=state, country=country, pin_code=pin_code, contact_no1=contact_no1, bank_account=bank_account_obj, cash_account=cash_account_obj) userdetail_obj.save() print "Registration Successful" return HttpResponse(json.dumps({ "validation": "Registration Successful.", "redirecturl": "#/login", "status": True }), content_type="application/json")