def _create_unique_key(team: Group): team_id_changed = team.pk % 1000 + 1000 owner_id_changed = team.owner.pk % 1000 + 1000 date_now = datetime.today() date_changed_str = '{}{}{}{}{}{}{}'.format(date_now.day, date_now.month, date_now.year, date_now.hour, date_now.minute, date_now.second, date_now.microsecond) key = '{}{}{}'.format(team_id_changed, owner_id_changed, date_changed_str) team.key = key team.save()
def confirm_group(request): if request.user.is_authenticated(): if is_firewallz_admin(request.user): pass else: logout(request) return HttpResponseRedirect('/regsoft/') else: return HttpResponseRedirect('/regsoft/') grp = Group() grp.group_code = group_code_generator() grp.save() if request.method == 'POST': data = json.loads(request.body.decode('utf-8')) print(data) for dt in data['data']: print(dt['pk']) Player = Regplayer.objects.get(pk=dt['pk']) print(Player.name.name) Player.entered = True Player.unbilled_amt = 1100 - int(Player.name.pcramt) Player.name.grp_leader = dt['groupleader'] Player.save() pl = Enteredplayer() pl.regplayer = Player pl.group = grp pl.save() if dt['groupleader'] == 1: grp.group_leader = int(dt['pk']) grp.save() data = {"groupcode": grp.group_code, "pk": grp.pk} return HttpResponse(json.dumps(data), content_type='application/json')
def distribute_records_among_existing_groups(**kwargs): print("Starting distribution among existing groups") print("Extracting records") unresolved_records = list(GroupRecord.objects.filter(group__isnull=True)) print("Making dictionary of groups") group_dict = Group.get_dictionary() if len(group_dict) == 0: print("No groups found. Finishing") return records_to_update = [] groups_to_update = set() print("Handling records") ttl = len(unresolved_records) cntr = 0 now = time() for record in unresolved_records: cntr += 1 if cntr % 100 == 0: print("{} of {} records handled {}".format(cntr, ttl, time() - now)) now = time() suitable_group = record.seek_for_group(group_dict, **kwargs) if suitable_group is not None: record.group = suitable_group records_to_update.append(record) groups_to_update.add(suitable_group) print("Have {0} records to update".format(len(records_to_update))) if len(records_to_update) > 0: bulk_update(records_to_update, update_fields=['group']) print("Have {0} groups to update".format(len(groups_to_update))) if len(groups_to_update) > 0: mark_inconsistency(groups=list(groups_to_update)) print("Distribution among existing groups: done")
def merge_consistent_groups(group_dict=None): print("Starting consistent groups merge") if group_dict is None: print("Extracting groups") group_dict = Group.get_dictionary() records_for_update = [] hypostases_for_update = [] groups_for_update = [] persons_to_delete = set() print("Iterating") for group, records in group_dict.items(): print(group.id) if not group.inconsistent and len(records) > 1: changed_records, changed_hypostases, unnecessary_persons = \ records[0].merge_records_by_hypostases(records[1:], save=False) group.person = records[0].person groups_for_update.append(group) hypostases_for_update.extend(changed_hypostases) records_for_update.extend(changed_records) persons_to_delete = persons_to_delete.union(unnecessary_persons) print("Saving") bulk_update(records_for_update, update_fields=['person']) bulk_update(hypostases_for_update, update_fields=['person']) bulk_update(list(group_dict.keys()), update_fields=['person']) for person in persons_to_delete: person.delete() print("Consistent groups merge: done")
def save_to_db(group: str, param: str, value: str): """ :param group: str :param param: str :param value: str :return: none """ group = Group.get_or_create(group) Chart.objects.create(region=group, param=param, value=value)
def update_persons_in_groups(): """Use in case some groups were merged, but person was not appropriately set.""" group_dict = Group.get_dictionary() cntr = 0 ttl = len(group_dict) for group, records in group_dict.items(): cntr += 1 print("{} of {}".format(cntr, ttl)) if group.person is None: unique_persons = set() for r in records: unique_persons.add(r.person) if len(unique_persons) == 1: group.person = unique_persons.pop() group.save()
def create_team(request): if auth.get_user(request).is_anonymous: return redirect('/') profile = get_user_profile(request) if profile.company is None: return render(request, 'main/errors/global_error.html', {'global_error': "403"}) new_group = Group() new_group.owner = profile new_group.name = 'Новая команда' new_group.description = 'Измените описание в настройках' new_group.company = profile.company new_group.save() _create_unique_key(new_group) profile.groups.add(new_group) return redirect('/team/{}/'.format(new_group.pk))
def creategroup(): form = GroupForm(request.form) if request.method == 'POST' and form.validate(): group_title = form.group_title.data creator = session['username'] person1 = form.person1.data person2 = form.person2.data person3 = form.person3.data person4 = form.person4.data group = Group(group_title, creator, person1, person2, person3, person4) db.session.add(group) db.session.commit() flash('Succesfully, created group') return redirect(url_for('allgroups')) return render_template('creategroup.html', form=form) """
def drop_from_group(): print("Droping one record from big groups") dct = Group.get_dictionary() records = [] i = 0 ttl = len(dct) for v in dct.values(): i += 1 if i % 100 == 0: print("{} of {}".format(i, ttl)) if len(v) > 2: v[0].group = None records.append(v[0]) print("Saving") bulk_update(records, update_fields=['group'], batch_size=1000) print("Done")
def mark_inconsistency(groups=None, group_dict=None): """Update inconsistency flag of chosen groups""" def check_group_consistency(group_record_list): """Returns True, if all records in list are fully equal""" first = group_record_list[0] for other in group_record_list[1:]: if not first.completely_equal_for_consistency( another_record=other): return False return True print("Starting procedure of inconsistency marking") if group_dict is None: print("Making dictionary of groups") group_dict = Group.get_dictionary() groups_to_update = set() print("Iterating through groups") if groups is None: groups = group_dict.keys() else: for group in groups: if not isinstance(group, Group): raise TypeError("groups must contain Group instances") for group in groups: records = group_dict[group] if check_group_consistency(group_record_list=records): if group.inconsistent: group.inconsistent = False groups_to_update.add(group) else: if not group.inconsistent: group.inconsistent = True groups_to_update.add(group) print("In-memory changes done") print("{} groups will be changed".format(len(groups_to_update))) bulk_update(list(groups_to_update), update_fields=['inconsistent']) print("Inconsistency marking: done")
def create_new_groups(*args, **kwargs): """Puts records into new groups if they do not have one yet and can meld with one another. Each record will have only one group (person) or none if it's alone. Predicate methods must contain one or more method names from GroupRecord class. Predicate methods must be defined and maintained in GroupRecord model. Call with "satisfies_new_group_condition" to form new groups. """ def key_function(gr): """For sorting by birth date""" if gr.birth_date is None: return date.today() else: return gr.birth_date predicate_methods = kwargs.pop( 'predicate_methods', ['satisfies_new_group_condition', 'not_forbidden']) if len(predicate_methods) == 0: raise AttributeError( "Predicate methods must contain at least one method") print("Starting creation of new groups") print("Extracting records") key = key_function unresolved_records = list(GroupRecord.objects.filter(group__isnull=True)) unresolved_records.sort(key=key) record_groups = [] print("Sorting") for k, g in groupby(unresolved_records, key=key): record_groups.append(list(g)) records_to_update = [] groups_to_update = [] print("Iterating through groups") cntr = 0 total = len(record_groups) for same_date_group in record_groups: cntr += 1 if cntr % 100 == 0: print("{} of {} date groups handled".format(cntr, total)) new_groups = [] for record_pair in combinations(same_date_group, 2): a = record_pair[0] b = record_pair[1] if a.check_predicates(predicate_methods=predicate_methods, another_record=b, **kwargs): a_in_group = False b_in_group = False new_group = None if a.group is not None and a.group in new_groups: a_in_group = True new_group = a.group if b.group is not None and b.group in new_groups: b_in_group = True new_group = b.group if new_group is None: new_group = Group() new_groups.append(new_group) new_group.save() if not a_in_group: a.group = new_group records_to_update.append(a) if not b_in_group: b.group = new_group records_to_update.append(b) groups_to_update.extend(new_groups) for group in new_groups: for record in same_date_group: if record.group == group and record.birth_date is not None: group.birth_date = record.birth_date bulk_update(new_groups, update_fields=['birth_date']) print("Have {0} records to save".format(len(records_to_update))) bulk_update(records_to_update, update_fields=['group']) print("Have {0} groups to update inconsistency".format( len(groups_to_update))) mark_inconsistency(groups_to_update) print("Creation of new groups: done")
def initialize_data(self, data): group = Group(week=data['week'], group=data['group']) group.save() for player in data['players']: group.players.add(Player.objects.get(id=player['id']))
def initialize_data(self, data): group = Group(week=data['week'], group=data['group']) group.save() for player in data['players']: group.players.add(Player.objects.get(id=player['id']))
"11-803", "11-811", "11-811", "11-806", "11-804", "11-805", "11-804", "11-807", "11-811", "11-809", "11-801", "11-807", "11-809", "11-810", "11-807", "11-805", "11-806", "11-806", "11-804", "11-804", "11-802", "11-807", "11-801", "11-805", "11-807", "11-801", "11-810", "11-801", "11-803", "11-804", "11-804", "11-807", "11-807", "11-804", "11-801", "11-801", "11-803" ] ress = [ "не использую", "stepik", "youtube", "матпрофи", "гуглю", "javarush", "acmp" ] maxSem = 6 print("started generating groups (группы)") for gr in groups: g = Group(name=gr) g.save() print("groups generated") print("started generating subjects") for sb in subjects: s = Subject(name=sb) s.save() print("subjects generated") print("started generating res") for re in ress: r = AdditionalEduResource(name=re, url="somesite.com") r.save() i = 0 print("res generated") print("started generating users") while i < len(surnames):
def create_group(self, week, group): group = Group(week=week, group=group) group.save() for player_id in [1, 2, 3, 4]: group.players.add(Player.objects.get(id=player_id)) return group
def create_group(self, week, group): group = Group(week=week, group=group) group.save() for player_id in [1,2,3,4]: group.players.add(Player.objects.get(id=player_id)) return group
def create_new_group(request, listing_id): try: listing = Listing.objects.get(pk=listing_id) except Listing.DoesNotExist: viewlog.error( "{request.path}: Listing with PK {listing_id} does not exist") return Response({'error': "Listing not found"}, status=status.HTTP_404_NOT_FOUND) if listing.group is not None: viewlog.error("{request.path}: Listing already in a Group") return Response({'error': "Listing is already in a group"}, status=status.HTTP_400_BAD_REQUEST) if listing.lister != request.user: viewlog.error( "{request.path}: Only lister of a listing can create a group for listing" ) return Response( {'error': "Only lister can create a group for a listing"}, status=status.HTTP_400_BAD_REQUEST) group = Group() group.start = listing.start group.end = listing.end group.to_location = listing.to_location group.from_location = listing.from_location group.save() group.members.add(listing) group.save() return Response(group.to_dict(), status=status.HTTP_201_CREATED)
def create_group(name='group_name_test'): group = Group(**locals()) group.save() return group