def get_current_group(): ''' Get the group pointed to by the settings object. Settings is a singleton. If settings doesn't exist, create settings and group. ''' if not Settings.objects.count(): # create settings and group if no settings object exists group = Group() group.save() settings = Settings(group=group) settings.save() return group settings = Settings.objects.all()[0] group = get_object_or_404(Group, id=settings.current_group_id) return group
def post(self, request): if request.data.get('type') == 'get': print("inside views post ", request.data) #For getting an individual group that a user is part of g = Group.objects.get(groupName=request.data.get('groupname')) m = MemberShip.objects.filter(group=g) m = m.values_list('person', flat=True) profiles = Profile.objects.filter(pk__in=m) jsonRes = serializers.serialize('json', profiles) #locations - all nearby locations to the midpoint of the group. locations = Location.objects.filter(group=g) locationsJson = serializers.serialize('json', locations) print('locations:', locationsJson) return Response({ 'members': jsonRes, 'locations': locationsJson, 'midpoint': g.midpoint }) else: g = Group() #For creating a new group. print("inside views post ", request.data) g.groupName = request.data.get('groupname') g.save() p = Profile.objects.get(user=request.user) g.midpoint = getCoordinates(p.Location) m1 = MemberShip(group=g, person=p) m1.save() g.save() print('membership', m1, g.midpoint) #midpoint created of the creating users location. places = getPlaces(g.midpoint) print(places) #get locations close to them for place in places: l = Location(locationName=place['name'], latLong=place['coordinates'], rating=place['rating'], types=place['types'], group=g) l.save() serializer = GroupSerializer(g) l = Location.objects.filter(group=g) jsonRes = serializers.serialize('json', l.all()) print("locations:", jsonRes) print('gorupL', serializer.data) return Response({'group': serializer.data, 'locations': jsonRes})
def test_group_user_creation(self): group = Group(name='Group Test') group.save() user = User( name='Johannes', password='******', last_login=timezone.now(), email='*****@*****.**', ) user.save() group_user = GroupUser( group=group, user=user ) group_user.save() gu = GroupUser.objects.get(pk=group_user.pk) self.assertEqual(user.name, gu.user.name) self.assertEqual(group.name, gu.group.name)
def create(self, request): """ Função que cria um novo grupo Parameters: request (HttpRequest): : POST Request contendo o nome do grupo Returns: HttpResponse: Retorna o nome e id do grupo criado na resposta. """ group_name = request.data['name'] try: group = Group(name=group_name) except IntegrityError: return Response({'error': 'Group name alredy exists.'}, status=status.HTTP_406_NOT_ACCEPTABLE) group.save() response = GroupModelSerializer(group) return Response(data=response.data, status=status.HTTP_201_CREATED)
def post(self): args = parser.parse_args() group = Group.objects(name__iexact=args['name']).first() if group: abort(409, message="A group with that name already exist.") group = Group(name=args['name'], full_name=args['full_name']) group.password = bcrypt.generate_password_hash(args['password']) if args['image'] and validate_file(args['image'].filename): group.image.put(args['image'], content_type=args['image'].content_type) group.save() response = { 'name': group.name, 'full_name': group.full_name, 'image': str(group.image.grid_id) if group.image.grid_id else None } return response, 201
def post(self, request): context = get_nav_context(request) name = request.POST.get('group-name') description = request.POST.get('description') agents = request.POST.getlist('agent') modules = request.POST.getlist('module') if name: group = Group(name=name) group.save() for module in modules: module = Module.objects.get(uuid=module) group_module = Group_Module(group_id=group, module_id=module) group_module.save() command_string = { "action": "add", "module": { "uuid": str(module.uuid), "name": module.name, "image": module.image } } new_command = Command(cmd=json.dumps(command_string), group_id=group) new_command.save() for agent in agents: agent = Agent.objects.get(uuid=agent) agent_group = Agent_Group(agent_id=agent, group_id=group) agent_group.save() for module in modules: module = Module.objects.get(uuid=module) agent_module, created = Agent_Module.objects.get_or_create( agent_id=agent, module_id=module) agent_module.save() return render(request, template_name='index.html', context=context)
def test_group_creation(self): group = Group(name='Group Test') group.save() self.assertEqual(Group.objects.get(pk=group.pk).name, 'Group Test')
def post(self, request): check = CheckRequest(request); if check.admin: return JsonResponse({ "status": "error", "msg": "User logined" }) uf = JoinForm(check.jsonForm) if uf.is_valid(): groupId = uf.cleaned_data['groupId'] groupName = uf.cleaned_data['groupName'] qq = uf.cleaned_data['qq'] nick = uf.cleaned_data['nick'] password = uf.cleaned_data['password'] #检测群是否存在 checkGroup = Group.objects.filter(groupId__exact = groupId).first() if checkGroup: if checkGroup.status == 0: return JsonResponse({ "status" : 'error', 'msg' : "此群已被注册,但正在验证中,群ID:%s" % groupId }) elif checkGroup.status == 1: return JsonResponse({ "status" : 'error', 'msg' : "此群已被注册并验证通过,可申请转让,群ID:%s" % groupId }) elif checkGroup.status == 2: GroupAdmin.objects.filter(groupId__exact = groupId).delete() checkGroup.delete() else: return JsonResponse({ "status" : 'error', 'msg' : "此群已被注册" }) group = Group( groupId = groupId, groupName = groupName ) group.save() if not group.id: return JsonResponse({ "status" : 'error', 'msg' : "Save group error:%s" % uf.cleaned_data['groupId'] }) admin = GroupAdmin( groupId = groupId, qq = qq, nick = nick, password = db_password(password), login_random = new_random(), activate_random = new_random(), recover_random = new_random(), userType = 1 ) admin.save() if admin.id: return JsonResponse({ "status" : 'success', 'msg' : "", 'data':{ 'id': admin.id, 'groupId' : admin.groupId, 'qq' : admin.qq, 'nick' : admin.nick, 'userType': admin.userType } }) else: return JsonResponse({ "status" : 'error', 'msg' : "Admin save error" }) else: return JsonResponse({ "status" : 'error', 'msg' : "Illegal post: %s" % uf.errors })
def post(self, request): check = CheckRequest(request) if check.admin: return JsonResponse({"status": "error", "msg": "User logined"}) uf = JoinForm(check.jsonForm) if uf.is_valid(): groupId = uf.cleaned_data['groupId'] groupName = uf.cleaned_data['groupName'] qq = uf.cleaned_data['qq'] password = uf.cleaned_data['password'] #检测群是否存在 checkGroup = Group.objects.filter(groupId__exact=groupId).first() if checkGroup: if checkGroup.status == 0: return JsonResponse({ "status": 'error', 'msg': "此群已被注册,但正在验证中,群ID:%s" % groupId }) elif checkGroup.status == 1: return JsonResponse({ "status": 'error', 'msg': "此群已被注册并验证通过,可申请转让,群ID:%s" % groupId }) elif checkGroup.status == 2: GroupAdmin.objects.filter(groupId__exact=groupId).delete() checkGroup.delete() else: return JsonResponse({"status": 'error', 'msg': "此群已被注册"}) group = Group(groupId=groupId, groupName=groupName) group.save() if not group.id: return JsonResponse({ "status": 'error', 'msg': "Save group error:%s" % uf.cleaned_data['groupId'] }) admin = GroupAdmin(groupId=groupId, qq=qq, password=db_password(password), login_random=new_random(), activate_random=new_random(), recover_random=new_random(), userType=1) admin.save() if admin.id: return JsonResponse({"status": 'success', 'msg': ""}) else: return JsonResponse({ "status": 'error', 'msg': "Admin save error" }) else: return JsonResponse({ "status": 'error', 'msg': "Illegal post: %s" % uf.errors })