def fillData(): generateGalaxy() neutral = Faction(name=" ", race=u" ") madulased = Faction(name="shshhh", race=u"madulased") inimesed = Faction(name="inimesed", race=u"humanoidid") db.session.add(neutral) db.session.add(madulased) engine = ShipPartClass(name="engine") indep = ShipClass(name="Independence") ship = Ship(name="Enterprise", faction=inimesed, body=Body(10, 10, 100), shipclass=indep, shipparts=[ShipPart(shippartclass=engine)]) db.session.add(ship) kirk = Person() kirk.firstname="James T." kirk.surename="Kirk" kirk.faction=inimesed kirk.ship=ship db.session.add(kirk) ship1 = Ship(name="Illustrious", faction=inimesed, body=Body(10, 40, 100), shipclass=indep, shipparts=[ShipPart(shippartclass=engine)]) db.session.add(ship1) gorath = ShipClass(name="Gorath") ship2 = Ship(name="dZahadum", faction=madulased, body=Body(67, 40, 100), shipclass=gorath, shipparts=[ShipPart(shippartclass=engine)]) db.session.add(ship2) ship3 = Ship(name="dYummy", faction=madulased, body=Body(69, 60, 100), shipclass=gorath, shipparts=[ShipPart(shippartclass=engine)]) db.session.add(ship3) db.session.commit()
def change_view(self, request, object_id, form_url='', extra_context=None): person = Person.objects.get(id=object_id) enable_ldap_connection = LdapConn.enable() exists_in_ldap = None groups = None suggested_ldap_name = '' suggested_ldap_email = '' if enable_ldap_connection: exists_in_ldap = Person.exists_in_ldap( person.ldap_user_name ) groups = Group.all() suggested_ldap_name = Person.suggested_name(object_id) if hasattr(settings, 'LDAP_DOMAIN_MAIL') and settings.LDAP_DOMAIN_MAIL: suggested_ldap_email = "{}@{}".format(suggested_ldap_name, settings.LDAP_DOMAIN_MAIL) else: messages.warning(request, _('ldap_without_connection')) context = {'suggested_ldap_name': suggested_ldap_name, 'suggested_ldap_email': suggested_ldap_email, 'groups': groups, 'hide_save_box': (not enable_ldap_connection), 'exists_in_ldap': exists_in_ldap } return super(PersonAdmin, self).change_view(request, object_id,'',context)
def after_jmp(self,response): xml = WebPage(response.body) data = dict(xml.parse(['input'],['title','value'],VERBOSE = True)) data = dict( (k.split(':')[0],k.split(':')[1].strip()) if ':' in k else (k,'') if not v else (k.split(':')[0],v.strip()) for k,v in data.items() ) print data d = data td = xml.parse(['textarea'],['text'],VERBOSE = True) print '-'*30,td,'-'*30 if td: tdetails= xml.parse(['textarea'],['text'],VERBOSE = True)[0][0] else: tdetails = '' p = Person.objects.filter(name=d['Name']) if not p: p = Person(name=d['Name'],affected_email=d['Affected E-mail'],reported_email=d['Reported E-mail']) p.save() else: p = p[0] tf = self.convertdate(d['Target Finish']) af = self.convertdate(d['Actual Finish']) tt = TicketItem(ticket=d['Incident'],owner=d['Owner'],target_finish = tf,summary=d['Summary'],owner_group=d['Owner Group'],priority=d['Internal Priority'],user=p,status=d['Status'],details=tdetails,actual_finish=af) yield(tt)
def generate_fakes(): Author.generate_fake() BookDetails.generate_fake() BookInventory.generate_fake() Client.generate_fake() Person.generate_fake() Volunteer.generate_fake() Donor.generate_fake()
def save_page(self,data): print 'in save-page' for i,d in enumerate(data): p = Person.objects.filter(name=d['Affected Person Name']) if not p: p = Person(name=d['Affected Person Name']) p.save() tf = datetime.datetime.strptime(d['Target Finish'],'%y-%m-%d %H:%M:%S').strftime('%Y-%m-%d %H:%M:%S') tt = TicketItem(ticket=d['Incident'],owner=d['Owner Name'],target_finish = tf,summary=d['Summary'],owner_group=d['Owner Group'],priority=d['Internal Priority'],user=p,status=d['Status']) yield(tt)
def register(request): if request.method == 'POST': form = UserCreationForm(request.POST) if form.is_valid(): new_user = form.save() new_person = Person(user=new_user, name=new_user.username) new_person.save() return HttpResponseRedirect("/accounts/login/") else: form = UserCreationForm() return render(request, 'register.html', {'form': form})
def remove_relationships(request, user_id): current_user_id = request.session['member_id'] current_user = User.objects.get(id=current_user_id) page_owner = User.objects.get(id=user_id) owner_person = Person.objects.get(user=page_owner) current_person = Person.objects.get(user=current_user) Person.remowe_relationship(current_person, owner_person, 1) return HttpResponseRedirect ("/accounts/profile/"+user_id)
def get_next_page(self,response): #print type(response),dir(response) xml = WebPage(response.body) data = response.meta['data'] print 'Size of data<get_next_page>:',len(data) if data: data += parse_list(xml) else: data = parse_list(xml) for i,d in enumerate(data): p = Person(name=d['Affected Person Name']) p.save() tf = datetime.datetime.strptime(d['Target Finish'],'%y-%m-%d %H:%M:%S').strftime('%Y-%m-%d %H:%M:%S') tt = TicketItem(ticket=d['Incident'],owner=d['Owner Name'],target_finish = tf,summary=d['Summary'],owner_group=d['Owner Group'],priority=d['Internal Priority'],user=p,status=d['Status']) yield(tt)
def clean(self): if "received_application" in self.cleaned_data \ and self.cleaned_data["received_application"]: # email required if received if not "email" in self.cleaned_data \ or not self.cleaned_data["email"]: self.add_error('email',_('required_attribute') ) if not "ldap_user_name" in self.cleaned_data \ or not self.cleaned_data["ldap_user_name"]: self.add_error('ldap_user_name',_('required_attribute') ) # email should not exist before if received request email = '' exists = False if "email" in self.cleaned_data and self.cleaned_data["email"]: email = self.cleaned_data['email'] exists = Person.ldap_uid_by_email(email) if exists: logging.warning(_('the_mail_is_required') % {'email':email}) logging.warning('The email should not exist before if received request.') #self.add_error('email', _('the_mail_is_required') % {'email':email} ) if "ldap_user_name" in self.cleaned_data and self.cleaned_data["ldap_user_name"] \ and not self.cleaned_data["received_application"]: self.add_error('received_application',_('received_application_required') ) if self.instance.pk and not self.cleaned_data["group_id"]: self.add_error('group_id',_('required_attribute_group') ) raise ValidationError(_('required_attribute_group')) if len(self.cleaned_data['ldap_user_password']) < settings.MIN_LENGTH_LDAP_USER_PASSWORD: raise ValidationError(_('ldap_user_password_too_short')) if "ldap_user_name" in self.cleaned_data \ and self.cleaned_data["ldap_user_name"]: existing_names_in_ldap = Person.ldap_uid_by_id( self.cleaned_data['document_number'], self.cleaned_data['document_type'] ) if existing_names_in_ldap and (self.cleaned_data["ldap_user_name"] not in existing_names_in_ldap): logging.info("User has already exists in Ldap with uid '%s'. it was not updated!" \ % ','.join(existing_names_in_ldap)) self.add_error('document_number', _("user_ID_ldap_already_exists") % {'uid':','.join(existing_names_in_ldap)})
def save_profile(backend, user, response, *args, **kwargs): if backend.name == 'facebook': profile = user try: person = Person.objects.get(user=profile) except: person = Person(user=profile) person.email = user.email person.name = response.get('name') person.save() elif backend.name == 'google-oauth2': profile = user try: person = Person.objects.get(user=profile) except: person = Person(user=profile) person.email = user.email person.name = response.get('name')['givenName'] + " " + response.get('name')['familyName'] person.save()
def check_ldap(request): existing_name_in_ldap = None doc_type = None result = False if 'ldap_user_name' in request.POST and request.POST['ldap_user_name']: if Person.exists_in_ldap( request.POST['ldap_user_name'] ): existing_name_in_ldap = request.POST['ldap_user_name'] if 'doc_type' in request.POST and request.POST['doc_type']: doc_type = DocumentType.objects.get(pk=request.POST['doc_type']) if 'doc_num' in request.POST and request.POST['doc_num']: existing_name_in_ldap = Person.ldap_uid_by_id( request.POST['doc_num'], doc_type.name.upper() ) if existing_name_in_ldap: result = True topic_list = json.dumps({'exists': result, 'uid_in_ldap': existing_name_in_ldap}) return HttpResponse(topic_list)
def test_print_room(self): person = Person.add_person("New", "Person", "staff") self.arguments = { '<first_name>': None, '<last_name>': None, '<role>': None, '<room_name>': [person.office.name], '<room_type>': None, '<wants_accommodation>': None, 'add_person': False, 'create_room': False, 'print_room': True } self.assertIn('%s, %s' % (person.name, person.role), handle(self.arguments))
def test_valid_book_object2(self): au = Person(nombre='Fiodor', apellidos='Dostoyevski') fp = datetime.date(1867, 1, 1) cw = CreativeWork(nombre='Crimen y Castigo', autor=au, fechapub=fp) b = Book(nombre=cw.nombre, autor=cw.autor.nombre + ' ' + cw.autor.apellidos, fechapub=cw.fechapub) b.add_isbn('0-7582-3013-3') db.session.add(b) db.session.commit() self.assertTrue(b.fechapub == datetime.date(1867, 1, 1)) self.assertTrue(b.id is not None) self.assertTrue(b.nombre == 'Crimen y Castigo') self.assertTrue(b.autor == 'Fiodor Dostoyevski') self.assertTrue(b.isbn == '0-7582-3013-3')
def order(): if request.method == 'GET': session.clear() data = db.session.query(Config).first() if data.state != 'ordering': return redirect("/") form = OrderForm(request.form) if request.method == 'POST' and form.validate(): db.session.rollback() db.session.expunge_all() if form.item.data == 'half': order = Half() order.email = form.email.data + EMAIL_DOMAIN order.location = form.location.data order.topping1 = form.topping1.data order.topping2 = form.topping2.data order.topping3 = form.topping3.data elif form.item.data == 'whole': order = Pizza() person = Person() order.topping1_left = form.topping1.data order.topping2_left = form.topping2.data order.topping3_left = form.topping3.data order.topping1_right = form.topping4.data order.topping2_right = form.topping5.data order.topping3_right = form.topping6.data order.sauce = form.sauce.data order.size = 'Large' person.email = form.email.data + EMAIL_DOMAIN person.location = form.location.data order.person1 = person db.session.add(person) elif form.item.data == 'medium': order = Pizza() person = Person() order.topping1_left = form.topping1.data order.topping2_left = form.topping2.data order.topping1_right = form.topping4.data order.topping2_right = form.topping5.data order.sauce = form.sauce.data order.size = 'Medium' person.email = form.email.data + EMAIL_DOMAIN person.location = form.location.data order.person1 = person db.session.add(person) order.time_added = datetime.datetime.now() db.session.add(order) db.session.flush() session['order_id'] = order.id session['order_type'] = type(order).__name__ session['payment_amount'] = set_price(form) url = 'https://api.venmo.com/v1/oauth/authorize?client_id={}&scope=make_payments&response_type=code'.format(CONSUMER_ID) return redirect(url) return render_template('order.html', data=data, form=form, domain=EMAIL_DOMAIN, large_price=LARGE_PRICE, medium_price=MEDIUM_PRICE)
def parse_company(c): try: city = City.objects.get(name_ru__icontains=c.city_text) c.city = city c.save() except: print('City not found') namear = c.faunders_text.split(',') p = Person() p.raw_name = namear[0] p.role = namear[1] p.source = 'flagma.kz' p.save() p.parse() p2c = Person2Company() p2c.company = c p2c.person = p p2c.save() print('Saving person %s' % namear[0])
def test_print_allocations(self): self.arguments = { '<first_name>': None, '<last_name>': None, '<role>': None, '<room_name>': [], '<room_type>': None, '<wants_accommodation>': None, 'add_person': False, 'create_room': False, 'print_allocations': True, 'print_room': False } self.assertListEqual([ "%s in %s Office and %s Living Space" % allocation for allocation in Person.get_allocations() ], handle(self.arguments))
def test_person(self): with self.app.app_context(): p0 = Person(first_name='zack', last_name='zhu') d0 = Dependent(first_name='adela', last_name='zhu') print(p0.type) print(d0.type) u0 = User(username='******', email='*****@*****.**', first_name='zack', last_name='zhu') u0.dependents.append(d0) db.session.add(u0) db.session.commit() print(p0.type) print(d0.type)
def test_subgraph_edge_count_parent_in_law(self): relative = fake.family_member(sex='Female') a1 = Person(first_name=relative['first_name'], last_name=relative['last_name'], ethnic_name=relative['ethnic_name'], sex=relative['sex'], birth_date=relative['birth_date'], email=relative['email'], confirmed=False) db.session.add(a1) db.session.commit() self.p3.get_or_create_relationship(a1, 1) n1_subgraph = self.p1.get_graph() c1 = graph.count_relationship_weights(self.p1, n1_subgraph) self.assertEqual(c1.get(1), 2) self.assertIsNone(c1.get(2)) self.assertEqual(c1.get(3), 2) self.assertIsNone(c1.get(4))
def register(): if current_user.is_authenticated: return redirect(url_for('index')) form = RegistrationForm() if form.validate_on_submit(): user = User(username=form.username.data, email=form.email.data) user.set_password(form.password.data) db.session.add(user) db.session.commit() person = Person(name=form.name.data, user_id=user.id, sex_id=form.sex.data, birthdate=form.birthdate.data) db.session.add(person) db.session.commit() flash('El usuario ha sido registrado exitosamente!') return redirect(url_for('login')) return render_template('register.html', form=form)
def __call__(name, *styles): accepted_styles = {} name = name.capitalize() print('{} пытается пройти фейс-контроль.'.format(name)) for style in styles: checking_style = EXISTING_STYLES.get(style) if not checking_style: print('Здесь нельзя танцевать "{}"!'.format(style)) continue accepted_styles[style] = checking_style if not accepted_styles: print('{} пришел сюда только пить.'.format(name)) person = Person(name=name, styles=accepted_styles) CLUB.people.append(person) CLUB.update_persons_activities(person) print(CLUB)
def _cross(self, args): row_key = args.get('row') if not row_key: return { 'status': 'failure', 'message': 'Parameter "row" not defined.', } col_key = args.get('col') if not col_key: return { 'status': 'failure', 'message': 'Parameter "col" not defined.', } persons = Person.find() total = len(persons) df = pd.DataFrame(persons) current_date = df['release_date'].max() row_total = df[row_key].value_counts().to_dict() table = pd.crosstab(df[col_key], df[row_key]) data = table.to_dict() rows = [] for key in data: row = { row_key: key, 'values': list(map(lambda name: {'name': name, 'count': data[key][name]}, data[key])), 'total': row_total.get(key) or 0 } rows.append(row) col_total = df[col_key].value_counts().to_dict() col_total = list(map(lambda name: {'name': name, 'count': col_total[name]}, col_total)) result = { 'status': 'success', 'current_date': current_date, 'rows': rows, 'col_total': col_total, 'total': total } return result
def test_befriend(self): person = Person(index=0, name='Test', age=30, has_died=False, eye_color='black', phone='000', address='test') friend = Person(index=1, name='Friend', age=30, has_died=False, eye_color='blue', phone='000', address='test') person.befriend(friend) self.assertEqual(1, len(person.friends)) self.assertEqual(person.friends[0].to_dict(), friend.to_dict())
def targetlists(workspace_id): ''' For GET requests, return all target lists associated with the given workspace. For POST requests, add a new list to the given workspace. ''' if not validate_workspace(workspace_id): return 'workspace does not exist', 404 # request is a GET if request.method == 'GET': workspace_lists = List.query.filter_by(workspace_id=workspace_id).all() schema = ListSchema(many=True, strict=True) list_data = schema.dump(workspace_lists) return jsonify(list_data[0]) # request is a POST elif request.method == 'POST': req_json = request.get_json() print(req_json) name = req_json['name'] targets = req_json['targets'] list_name = List.query.filter_by(workspace_id=workspace_id, name=name).first() if list_name is not None: return json.dumps({'success': False}), 200, { 'ContentType': 'application/json' } new_list = List(name=name, workspace_id=workspace_id) for target in targets: person = Person(first_name=target['first_name'], last_name=target['last_name'], email=target['email']) new_list.targets.append(person) db.session.add(new_list) update_workspace_ts(Workspace.query.filter_by(id=workspace_id).first()) db.session.commit() schema = ListSchema(strict=True) list_data = schema.dump(new_list) return jsonify(list_data[0]), 201
def create_user(): if current_user.is_authenticated and current_user.clearance == 3: # if current_user.clearance == 0: create_userform = AdministrationForm() check_userform = GetUserForm() if create_userform.user_submit.data and create_userform.validate(): if create_userform.clearance.data == 1: group = 'Opettaja' elif create_userform.clearance.data == 2: group = 'Oppilas' elif create_userform.clearance.data == 3: group = 'Admin' user = User(username=create_userform.lastname.data + '.' + create_userform.firstname.data, clearance=create_userform.clearance.data, group=group) check_user = User.query.filter_by( username=create_userform.lastname.data + '.' + create_userform.firstname.data).first() if check_user is None: user.set_password(create_userform.password.data) db.session.add(user) db.session.flush() person = Person(first_name=create_userform.firstname.data, last_name=create_userform.lastname.data, user_id=user.id) db.session.add(person) db.session.commit() flash('Uusi käyttäjä luotu!') return redirect('/admin/create_user') else: flash( 'Hupsista, käyttäjätunnuksessa tai salasanassa on vikaa. Valitse jokin muu tunnus.' ) return redirect(url_for('create_user')) return render_template('user_management.html', title='Käyttäjänhallinta', create_userform=create_userform, check_userform=check_userform) else: flash('Luvaton pääsy!') return redirect('/')
def test_subgraph_null_relations(self): relative = fake.family_member(sex='Female') a1 = Person(first_name=relative['first_name'], last_name=relative['last_name'], ethnic_name=relative['ethnic_name'], sex=relative['sex'], birth_date=relative['birth_date'], email=relative['email'], confirmed=False) db.session.add(a1) db.session.commit() with self.assertRaises(KeyError): graph.get_relationship(source_person=a1, target_person=self.p1) graph.get_relationship(source_person=a1, target_person=self.p2) graph.get_relationship(source_person=a1, target_person=self.p3) graph.get_relationship(source_person=a1, target_person=self.p4) graph.get_relationship(source_person=self.p1, target_person=a1) graph.get_relationship(source_person=self.p2, target_person=a1) graph.get_relationship(source_person=self.p3, target_person=a1) graph.get_relationship(source_person=self.p4, target_person=a1)
def test_to_jsonld(self): print('Iniciando el test to json-ld') au = Person(nombre='Fiodor', apellidos='Dostoyevski') fp = datetime.date(1867, 1, 1) cw = CreativeWork(nombre='Crimen y Castigo', autor=au, fechapub=fp) b = Book(nombre=cw.nombre, autor=cw.autor.nombre + ' ' + cw.autor.apellidos, fechapub=cw.fechapub) b.add_isbn('0-7582-3013-3') db.session.add(b) db.session.commit() with self.app.test_request_context('/'): json_book = b.to_jsonld() expected_keys = [ 'url', '@context', '@type', 'author', 'datePublished', 'isbn', 'name' ] self.assertEqual(sorted(json_book.keys()), sorted(expected_keys)) self.assertEqual('/python-api/v1/book/' + str(b.id), json_book['url'])
def create_person(first_name, last_name, full_name, originator_id, photo_url=''): matching_persons = Person.query.filter(Person.name == full_name) #assume we found a dumplicate person if matching_persons.count() > 0: #pdb.set_trace() person = matching_persons[0] tag = Tag() tag.initialize('possible duplicate', originator_id, person.id, type="metadata", publicity="private") if Tag.query.filter(Tag.slug == tag.slug).count() == 0: db.session.add(tag) if photo_url and not person.photo_url: person.photo_url = photo_url db.session.add(person) print("creating a tag to mark %s as a possible duplicate" % person.slug) else: person = Person() person.name = full_name person.first_name = first_name person.last_name = last_name person.slug = person.create_slug() db.session.add(person) db.session.commit() tag = Tag() tag.initialize('added', originator_id, person.id, subject_slug=person.slug, type="metadata", publicity="private") db.session.add(tag) db.session.commit() print("adding person %s" % person.slug) return person
def parseCSV(filePath): # CVS Column Names col_names = [ 'fname', 'lname', 'email', 'username', 'is_active', 'role', 'platform', 'shift', 'group' ] # Use Pandas to parse the CSV file csvData = pd.read_csv(filePath, names=col_names, header=0) # Loop through the Rows for i, row in csvData.iterrows(): person = Person(fname=row['fname'], lname=row['lname'], email=row['email'], username=row['username'], is_active=row['is_active'], group=row['group'], role=row['role'], shift=row['shift'], platform=row['platform']) db.session.add(person) db.session.commit() return redirect('/person')
def scrape(face_book_cookie, people_search_session_cookie, csrf_token): print('Initializing sources.') cache = Cache() directory = sources.Directory(cache, people_search_session_cookie, csrf_token) face_book = sources.FaceBook(cache, face_book_cookie, directory) name_coach = sources.NameCoach(cache, people_search_session_cookie, csrf_token) departmental = sources.Departmental(cache) print('Beginning scrape.') cache_key = 'scraped_data' people = cache.get(cache_key) if not people: people = [] thread_fb_dir_nc = Thread(target=scrape_face_book_directory_name_coach, args=(face_book, directory, name_coach)) thread_departmental = Thread(target=departmental.pull, args=(people, )) thread_fb_dir_nc.start() thread_departmental.start() thread_fb_dir_nc.join() thread_departmental.join() # TODO: find a cleaner way to exchange this data people = name_coach.people people = departmental.merge(people) cache.set(cache_key, people) # Store people into database Person.query.delete() for person_dict in people: db.session.add( Person( ** {k: v for k, v in person_dict.items() if v or type(v) == bool})) db.session.commit() print('Done.')
def add_student(): if current_user.is_authenticated and current_user.clearance == 3: form = StudentForm() form.group.choices = groups_query() if form.validate_on_submit(): student = Student(group_id=form.group.data, full_name=form.firstname.data + ' ' + form.lastname.data) db.session.add(student) db.session.flush() person = Person(first_name=form.firstname.data, last_name=form.lastname.data, student_id=student.id) db.session.add(person) db.session.commit() flash('Uusi oppilas lisätty!') return redirect('/admin/add_student') return render_template('student_management.html', title='Lisää oppilas', form=form) else: flash('Luvaton pääsy!') return redirect('/')
def showPersons(region_name, clinic_id): form = NewPerson() if form.validate_on_submit(): if not form.picture_url.data: filename = '-.png' url = 'images/-.png' else: filename = images.save(request.files['picture_url']) url = images.url(filename) newPerson = Person(name=form.name.data, comments=form.comments.data, picture_filename=filename, picture_url=url, phone=form.phone.data, email=form.email.data, department=form.department.data, date_of_request=form.date_of_request.data, date_of_request2=form.date_of_request2.data, author=current_user, region_name=region_name, clinic_id=clinic_id) db.session.add(newPerson) db.session.commit() flash('Новый клиент "{}" добавлен!'.format(form.name.data)) return redirect( url_for('clients.showPersons', clinic_id=clinic_id, region_name=region_name)) persons = Person.query.filter_by(clinic_id=clinic_id).order_by( Person.last_visit.desc()) clinic = Clinic.query.filter_by(id=clinic_id).one() return render_template("/clients/persons.html", title='Клиенты', persons=persons, form=form, region_name=region_name, clinic=clinic)
def test_forge_count_is_valid(self): relative = fake.family_member(sex='Female') a1 = Person( first_name=relative['first_name'], last_name=relative['last_name'], ethnic_name=relative['ethnic_name'], sex=relative['sex'], birth_date=relative['birth_date'], email=relative['email'], confirmed=False ) db.session.add(a1) db.session.commit() self.p1.get_or_create_relationship(self.p2, 1) self.p1.get_or_create_relationship(self.p3, 3) self.p1.get_or_create_relationship(self.p4, 3) self.p1.get_or_create_relationship(a1, 3) self.p2.get_or_create_relationship(self.p3, 3) self.p2.get_or_create_relationship(self.p4, 3) self.p2.get_or_create_relationship(a1, 3) self.p3.get_or_create_relationship(self.p4, 2) self.p3.get_or_create_relationship(a1, 2) self.p4.get_or_create_relationship(a1, 2) self.assertEqual(Link.query.count(), 20)
def run(): # Data to initialize database with PEOPLE = [{ 'fname': 'Doug', 'lname': 'Farrell' }, { 'fname': 'Kent', 'lname': 'Brockman' }, { 'fname': 'Bunny', 'lname': 'Easter' }] # Delete database file if it exists currently if not os.path.exists('people.db'): # Create the database db.create_all() # Iterate over the PEOPLE structure and populate the database for person in PEOPLE: p = Person(lname=person['lname'], fname=person['fname']) db.session.add(p) db.session.commit()
def save_profile(backend, user, response, *args, **kwargs): if backend.name == 'facebook': profile = user try: person = Person.objects.get(user=profile) except: person = Person(user=profile) person.email = user.email person.name = response.get('name') person.save() elif backend.name == 'google-oauth2': profile = user try: person = Person.objects.get(user=profile) except: person = Person(user=profile) person.email = user.email person.name = response.get( 'name')['givenName'] + " " + response.get('name')['familyName'] person.save()
def handle(self, *args, **options): print('Parsing persons.') print("Deleting all persons") Person.objects.filter(source='zakon.kz').delete() Person2Document.objects.all().delete() paginator = Paginator(MainDocuments.objects.all().order_by('-id'), 1000) for page in range(1, paginator.num_pages + 1): for md in paginator.page(page).object_list: match = re.search('Судья:(.*)', md.title) try: rezult = match.group(1) try: exist = Person.objects.get(raw_name=rezult, source='zakon.kz') except Exception as e: p = Person() p.raw_name = rezult p.role = 'СУДЬЯ' p.source = 'zakon.kz' p.save() p.parse() p2d = Person2Document() p2d.person = p p2d.document = md p2d.save() print("%s Судья ... %s" % (md.id, rezult)) except Exception as e: pass #print(str(e)) match = re.search(': (.*)Судья:', md.title) try: rezult = match.group(1) for nm in rezult.split(','): nm.replace('.', '') if len(nm) > 2: checking_words = nm.split(' ') if len(checking_words) < 5: try: exist = Person.objects.get( raw_name=rezult, source='zakon.kz') except Exception as e: p = Person() p.raw_name = nm p.role = 'ОТВЕТЧИК' p.source = 'zakon.kz' p.save() p.parse() p2d = Person2Document() p2d.person = p p2d.document = md p2d.save() print("%s Истец ... %s" % (md.id, rezult)) except Exception as e: #print(str(e)) pass
def create_fake_documents(db, nb_docs=1000, nb_correspondents=None, fake=None): from app.models import Document from app.models import Institution from app.models import User from app.models import Whitelist from app.models import Image from app.models import Note from app.models import Language from app.models import PersonRole from app.models import Person from app.models import Witness if fake is None: fake = Faker() logging.getLogger('faker.factory').setLevel(logging.ERROR) users = User.query.all() whitelists = Whitelist.query.all() # add some languages db.session.add(Language(code="FRO")) db.session.add(Language(code="ENG")) db.session.add(Language(code="OCC")) db.session.add(Language(code="CZC")) db.session.add(Language(code="ITA")) db.session.commit() languages = Language.query.all() # add fake collections for i in range(1, 10): db.session.add( Collection(title=fake.sentence(), description=fake.text())) db.session.commit() collections = Collection.query.all() # add fake correspondent roles for i in range(5, 20): db.session.add(PersonRole(label=fake.word())) db.session.flush() roles = PersonRole.query.all() # add fake persons if nb_correspondents is None: nb_correspondents = nb_docs * 2 for i in range(0, nb_correspondents): db.session.add( Person(firstname=fake.first_name(), lastname=fake.last_name(), key=fake.name(), ref=fake.uri())) db.session.flush() correspondents = Person.query.all() # add fake Institutions institutions = [] for i in range(0, 20): ins = Institution(name=fake.sentence(nb_words=3), ref=fake.uri()) db.session.add(ins) institutions.append(ins) db.session.flush() # add fake documents last_progress = -1 for n_doc in range(0, nb_docs): try: doc = Document(title=fake.sentence(), transcription=fake.text(max_nb_chars=1000), argument=fake.text()) doc.owner_id = users[0].id doc.whitelist_id = whitelists[0].id doc.languages = [languages[0], languages[1]] doc.collections = collections db.session.add(doc) db.session.flush() # add fake witnesses witnesses = [] for i in range(0, 3): wit = Witness(document_id=doc.id, content=fake.sentence(), tradition=random.choice(TRADITION_VALUES), status=random.choice(WITNESS_STATUS_VALUES), institution_id=random.choice(institutions).id, classification_mark=fake.sentence()) db.session.add(wit) witnesses.append(wit) db.session.flush() # add fake Images for w in range(0, len(witnesses)): for i in range(0, 5): img = Image(canvas_idx=random.randint(1, 100), manifest_url=fake.uri(), witness_id=witnesses[w].id) db.session.add(img) # add fake Notes nb_notes = 50 for i in range(0, nb_notes): n = Note(label=fake.sentence(), content=fake.paragraph(), document_id=doc.id) db.session.add(n) db.session.flush() # add fake correspondent to the doc from app.models import PersonHasRole correspondents_have_roles = [] nb_corr = 3 for i in range(0, nb_corr): role = roles[0] co = correspondents[i] correspondents_have_roles.append((role.id, co.id)) c_h_roles = [] for (role_id, co_id) in set(correspondents_have_roles): chr = PersonHasRole(document_id=doc.id, correspondent_id=co_id, correspondent_role_id=role_id) db.session.add(chr) c_h_roles.append(chr) db.session.flush() doc.correspondents_have_roles = c_h_roles docs = Document.query.filter(Document.id != doc.id).all() if len(docs) > 0: if len(docs) > doc.id - 1 > 0: doc.next_document = docs[doc.id - 1] db.session.add(doc) db.session.commit() except IntegrityError as e: db.session.rollback() print("Warning:", e) progress = int(n_doc / nb_docs * 100) if progress % 10 == 0 and last_progress != progress: print("%s..." % progress, end="", flush=True) last_progress = progress db.session.commit()
admin = User(username='******', email='*****@*****.**', password=make_password('hello')) yun_hua.save() yonghua_xu.save() yinye_fan.save() admin.save() # yun_hua = User.objects.get(username='******') engineering = Department(dept_id=600800, name='技术工程部', summary='Engineering') engineering.save() admin_dept = Department(dept_id=600310, name='行政部', summary='Admin') admin_dept.save() # engineering = Department.objects.get(dept_id='600800') person1 = Person(user=yun_hua, employee_id=149, privilege=1, department=engineering) person1.save() person2 = Person(user=yonghua_xu, employee_id=61, privilege=1, department=engineering) person2.save() person3 = Person(user=yinye_fan, employee_id=99, privilege=1, department=engineering) person3.save() person_admin = Person(user=admin, employee_id=0, privilege=1, department=engineering) person_admin.save() type1 = ProjectType(typename='FA', summary='客户项目') type1.save() type2 = ProjectType(typename='BD', summary='商务拓展') type2.save() type3 = ProjectType(typename='Internal R&D', summary='内部研发') type3.save() type4 = ProjectType(typename='customer service', summary='客户服务')
def save_profile(backend, user, response, *args, **kwargs): if backend.name == 'facebook': person = Person.objects.filter(user=user).first() if not person: person = Person(user=user) person.email = user.email person.user_name = user.username person.name = response.get('name') person.save() elif backend.name == 'google-oauth2': person = Person.objects.filter(user=user).first() if not person: person = Person(user=user) person.email = user.email person.user_name = user.username person.name = response.get('name') person.save()
import django from app.models import Person f = open("dataPy.csv", mode='r') toBeAdded = [] # pTest = Person(ImgNumber=1, Form=18, FirstName="James", LastName="Hovet", NickName="James", NumCorrect=0, NumIncorrect=0, UnformattedName="shown 100",NumShown=100) # '2988311,"Abby ""Abby"" Hannah Kong \'17",Abby,Kong,Abby,17,F' f.readline() for line in f: d = line.strip().split(',') tmp = Person(ImgNumber=d[0], Form=d[5], FirstName=d[2], LastName=d[3], NickName=d[4], NumCorrect=0, NumIncorrect=0, UnformattedName=d[1],NumShown=0, Gender=d[6]) tmp.save()
def userpage(request, user_id): tag_user = User.objects.all() if request.method == "POST": income_title = request.POST['title'] income_text = request.POST['text'] if income_title and income_text: message = Message() message.author = request.user message.date = datetime.datetime.now() message.title = income_title message.text = income_text message.save() user_id = int(user_id) current_user_id = request.session['member_id'] current_user = User.objects.get(id=current_user_id) page_owner = User.objects.get(id=user_id) message = Message.objects.filter(author=page_owner).order_by('-id') owner_person = Person.objects.get(user=page_owner) current_person = Person.objects.get(user=current_user) subscribings_user = Person.get_following(current_person) subscribers_user = Person.get_followers(current_person) subscribings_owner = Person.get_following(owner_person) subscribers_owner = Person.get_followers(owner_person) condition=False i=0 for subscribings in subscribings_user: if page_owner.id != subscribings.user.id: i += 1 if i == len(subscribings_user): condition=True last_message=datetime.datetime.now() for one in message: last_message=one.date if current_user_id == user_id: message = Message.objects.filter(author=current_user).order_by('-id') mesform = MessageForm() last_message=datetime.datetime.now() for one in message: last_message=one.date return render(request, 'userpage.html', {'mesform': mesform, 'message': message, 'page_owner': page_owner, 'cur_user_id': current_user_id, 'subscribings_user': subscribings_user, 'subscribers_user': subscribers_user, 'last_message': last_message, 'tag_user': tag_user}, ) else: return render(request, 'user.html', {'message': message, 'page_owner': page_owner, 'cur_user_id': current_user_id, 'subscribings_owner': subscribings_owner, 'subscribers_owner': subscribers_owner, 'subscribings_user': subscribings_user, 'condition': condition, 'last_message': last_message, 'tag_user': tag_user}, )
import datetime db.drop_all() db.create_all() # if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): # api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository') # api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) # else: # api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO)) # Add Default person person = Person(person_id=1, first_name="Colin", last_name="Hunt", address="Follow the yellow brick road", email="*****@*****.**", phone="1231231234") # Add Admin admin = User(user_name="admin", password="******", date_registered=datetime.date.today(), person_id=1, user_class='a') db.session.add(person) db.session.add(admin) db.session.commit()
def init_persons(): Person.generate_fake(100) Donor.generate_fake() Volunteer.generate_fake()
def save_model(self, request, obj, form, change): ldap_user_name = str(obj.ldap_user_name) if obj.ldap_user_name else None udn = Person.ldap_udn_for( ldap_user_name ) try: if (not ldap_user_name) or (ldap_user_name is None): logging.warning("An LDAP user was not given. It is not updated!") super(PersonAdmin, self).save_model(request, obj, form, change) return if Person.exists_in_ldap(ldap_user_name): # actualizar ldap_person = Person.get_from_ldap(ldap_user_name) # update data ldap_person.update_ldap_data_from(obj) # update password only for superuser if str(ldap_person.ldap_user_password) != str(obj.ldap_user_password) \ and request.POST.has_key('ldap_user_password_check'): if request.user.is_superuser: logging.warning("User '%s' already exists in Ldap. changing password.." % ldap_user_name) Person.update_ldap_user_password ( ldap_user_name, str(obj.ldap_user_password) ) else: raise ValidationError( _('cant_change_password') ) # update group only for superuser if str(ldap_person.group_id) != str(obj.group_id): if request.user.is_superuser: logging.warning("User '%s' already exists in Ldap. Changing group '%s' by '%s'.." % \ (ldap_user_name,ldap_person.group_id, obj.group_id ) ) Group.add_member_to(ldap_user_name, {'group_id': str(obj.group_id)}) Group.remove_member_of(ldap_user_name, ldap_person.group_id) ldap_person.update_ldap_gidgroup( str(obj.group_id) ) else: raise ValidationError( _('cant_change_group') ) else: # crear nuevo new_uid_number = Person.next_ldap_uidNumber() if not (new_uid_number > 0): logging.error( "The following 'ldap user uid' could not be determined. " \ "The value obtained was %s" % str(new_uid_number)) raise ValidationError( _('without_ldap_user_uid') ) if Person.exist_ldap_uidNumber(new_uid_number): logging.error("The ldap user uidNumber '%s' already exist!." % str(new_uid_number)) new_uid_number = 0 raise ValidationError( _('uidnumber_already_exist') % {'uidnumber':str(new_uid_number)} ) # Create new ldapp user cnuser = LdapConn.parseattr( "%s %s" % (obj.name, obj.surname) ) snuser = LdapConn.parseattr( "%s" % obj.surname ) new_user = [ ('objectclass', settings.LDAP_PEOPLE_OBJECTCLASSES), ('cn', [cnuser]), ('sn', [snuser]), ('givenName', [ LdapConn.parseattr(obj.name)] ), ('paisdoc', [settings.LDAP_PEOPLE_PAISDOC] ), ('tipodoc', [str(obj.document_type)] ), ('numdoc', [str(obj.document_number)] ), ('uidNumber', [str(new_uid_number)] ), ('userPassword', [str(obj.ldap_user_password)] ), ('homedirectory', [str('%s%s' % ( settings.LDAP_PEOPLE_HOMEDIRECTORY_PREFIX, ldap_user_name))]), ('gidNumber', [str(obj.group_id)] ), ('loginShell', [str(settings.LDAP_PEOPLE_LOGIN_SHELL)]),] if obj.position: new_user.append(('employeeType', [str(obj.position)] )) if obj.area: new_user.append(('destinationIndicator', [str(obj.area)])) if obj.floor: new_user.append(('departmentNumber', [str(obj.floor)] )) if obj.work_phone: new_user.append(('telephoneNumber', [str(obj.work_phone)])) if obj.home_phone: new_user.append(('homePhone', [str(obj.home_phone)])) if obj.office_name(): new_user.append(('physicalDeliveryOfficeName', [str(LdapConn.parseattr(obj.office_name()))])) if obj.email: mails = [] mails.append(str(obj.email)) if obj.alternative_email: mails.append(str(obj.alternative_email)) new_user.append(('mail',mails)) Person.create_ldap_user( ldap_user_name, new_user ) # Update ldap groups cn_group = Group.cn_group_by_gid(obj.group_id) cn_groups = ['%s' % str(cn_group)] if settings.LDAP_DEFAULT_GROUPS: cn_groups += settings.LDAP_DEFAULT_GROUPS Group.add_member_in_groups( ldap_user_name, cn_groups ) obj.save() except ValidationError as e: messages.set_level(request, messages.ERROR) messages.error(request,"%s" % e[0])
def test_autor_getter(self): au = Person(nombre='Pedro', apellidos='Juan Gutiérrez') b = Book(autor=au) self.assertTrue(b.autor.nombre == 'Pedro') self.assertTrue(b.autor.apellidos == 'Juan Gutiérrez')
def addperson(request): person = Person() person.p_name = 'Atom-' + str(random.randrange(1, 100)) person.save() return render(request, 'addperson.html')
def set_up_test_db(): person1 = Person(index=0, name='Test', age=30, has_died=False, eye_color='black', phone='000', address='test') person2 = Person(index=1, name='Test', age=30, has_died=False, eye_color='black', phone='000', address='test') friend = Person(index=2, name='Friend', age=30, has_died=False, eye_color='brown', phone='000', address='test') company = Company(index=0, name='Test') no_employees_company = Company(index=1, name='No Employees') vegetable = Food(index=0, name='Lettuce', type=Food.VEGETABLE) fruit = Food(index=1, name='Banana', type=Food.FRUIT) person1.company = company person1.foods.append(vegetable) person1.foods.append(fruit) person2.company = company person1.befriend(friend) person2.befriend(friend) db.session.add(person1) db.session.add(no_employees_company)
def scrape(face_book_cookie, people_search_session_cookie, csrf_token): # Uncomment for quick testing """ directory = yaledirectory.API(people_search_session_cookie, csrf_token) people = [] directory_entries = read_directory(directory, 'aa') for entry in directory_entries: print('Parsing directory entry with NetID ' + entry.netid) person = add_directory_to_person({}, entry) people.append(person) """ html = get_html(face_book_cookie) tree = get_tree(html) containers = get_containers(tree) if len(containers) == 0: print( 'No people were found on this page. There may be something wrong with authentication, aborting.' ) return directory = yaledirectory.API(people_search_session_cookie, csrf_token) watermark_mask = Image.open('app/res/watermark_mask.png') image_uploader = ImageUploader() print('Already hosting {} images.'.format(len(image_uploader.files))) emails = {} people = [] for container in containers: person = { 'school': 'Yale College', 'school_code': 'YC', } person['last_name'], person['first_name'] = clean_name( container.find('h5', { 'class': 'yalehead' }).text) person['year'] = clean_year( container.find('div', { 'class': 'student_year' }).text) pronoun = container.find('div', {'class': 'student_info_pronoun'}).text person['pronoun'] = pronoun if pronoun else None info = container.find_all('div', {'class': 'student_info'}) person['college'] = info[0].text.replace(' College', '') try: person['email'] = info[1].find('a').text except AttributeError: pass #person.email = guess_email(person) trivia = info[1].find_all(text=True, recursive=False) try: room = trivia.pop(0) if RE_ROOM.match(trivia[0]) else None if room: person['residence'] = room result = RE_ROOM.search(room) person['building_code'], person['entryway'], person[ 'floor'], person['suite'], person['room'] = result.groups( ) person['birthday'] = trivia.pop() if RE_BIRTHDAY.match( trivia[-1]) else None person['major'] = trivia.pop() if trivia[-1] in MAJORS else None if person['major'] and person['major'] in MAJOR_FULL_NAMES: person['major'] = MAJOR_FULL_NAMES[person['major']] except IndexError: pass new_trivia = [] for r in range(len(trivia)): row = trivia[r].strip() if row.endswith(' /'): row = row.rstrip(' /') if RE_ACCESS_CODE.match(row): person['access_code'] = row if RE_PHONE.match(row): person['phone'] = clean_phone(row) if len(new_trivia) == 1 and not person.get('residence'): person['residence'] = new_trivia.pop(0) else: new_trivia.append(row) trivia = new_trivia # Handle first row of address being duplicated for residence if len(trivia) >= 2 and trivia[0] == trivia[1] and not person.get( 'residence'): person['residence'] = trivia.pop(0) person['address'] = '\n'.join(trivia) person['leave'] = False person['eli_whitney'] = False directory_entry = get_directory_entry(directory, person) if directory_entry is not None: person['netid'] = directory_entry.netid person['upi'] = directory_entry.upi if not person.get('email'): person['email'] = directory_entry.email if not person.get( 'year' ) and directory_entry.student_expected_graduation_year: person['year'] = int( directory_entry.student_expected_graduation_year) # This may not always be the case. But it's probably a safe bet. person['eli_whitney'] = True person = add_directory_to_person(person, directory_entry) else: print('Could not find directory entry.') image_id = clean_image_id(container.find('img')['src']) if image_id: image_filename = image_uploader.get_image_filename( image_id, person) if image_filename in image_uploader.files: person['image'] = image_uploader.get_file_url(image_filename) else: print('Image has not been processed yet.') image_r = requests.get( 'https://students.yale.edu/facebook/Photo?id=' + str(image_id), headers={ 'Cookie': face_book_cookie, }, stream=True) image_r.raw.decode_content = True try: im = Image.open(image_r.raw) # Paste mask over watermark im.paste(watermark_mask, (0, 0), watermark_mask) output = BytesIO() im.save(output, format='JPEG', mode='RGB') person['image'] = image_uploader.upload_image( output, image_filename) except OSError: # "Cannot identify image" error print('PIL could not identify image.') if person.get('email'): emails[person['email']] = len(people) people.append(person) # Check leaves people = compare_years('pre2020', people, emails) people = compare_years('fall2020', people, emails) # Fetch non-undergrad users by iterating netids # Get set of netids for students we've already processed checked_netids = { person_dict.get('netid') for person_dict in people if 'netid' in person_dict } directory_entries = read_directory(directory) for entry in directory_entries: if entry.netid not in checked_netids: print('Parsing directory entry with NetID ' + entry.netid) checked_netids.add(entry.netid) person = add_directory_to_person({}, entry) people.append(person) emails.append(person['email']) # Add data from departmental scraper departmental = Departmental() department_people = departmental.scrape() for record in department_people: person_i = None if record.get('email'): person_i = emails.index(record['email']) if not person_i: for i, person in enumerate(people): if name_matches(person, record['name']): person_i = i break # Add in data if we found a match if person_i: person = add_departmental_to_person(person, record) # Store people into database Person.query.delete() for person_dict in people: db.session.add( Person( ** {k: v for k, v in person_dict.items() if v or type(v) == bool})) db.session.commit() print('Done.')