Ejemplo n.º 1
0
def main():
    my_house = House(
        address=[
            AddressData(
                address_data=[
                    AddressParts(
                        house_number=1023,
                        street_name="Main Street",
                        city_name="Anytown"
                    )
                ]
            )
        ]
    )

    # also called automatically when `.save()` is called
    my_house.full_clean()

    out = pickle.loads(pickle.dumps(my_house))
#    out.full_clean()
    out.validate()

    # clean also automatically wraps dicts in appropriate classes so this is
    # also acceptable:
    my_house = House(address=[{'address_data': [{
                'house_number': 1023,
                'street_name': "Main Street",
                'city_name': "Anytown"
            }]}])
    my_house.full_clean()
Ejemplo n.º 2
0
def seed_data():
    ron_key = Student(student_id=423491377, first_name ="Ron", last_name = "Weasley").put()
    harry_key = Student (student_id=423491782, first_name ="Harry", last_name = "Potter").put()
    hermione_key = Student(student_id=423491249, first_name="Hermione", last_name="Granger").put()
    malfoy_key = Student(student_id=42391043, first_name="Draco", last_name="Malfoy").put()
    crabbe_key = Student(student_id=42391122, first_name="Vincent", last_name="Crabbe").put()
    goyle_key = Student(student_id=42391063, first_name="Gregory", last_name="Goyle").put()


    ron_wand=Wand(length = 14.0, material = "willow", core="unicorn", owner=ron_key)
    harry_wand=Wand(length = 11.0, material = "holly", core="phoenix feather", owner=harry_key)
    hermione_wand = Wand(length=10.75, material="vinewood", core="dragon heartstring", owner=hermione_key)

    ron_wand.put()
    harry_wand.put()
    hermione_wand.put()

    gryff = House(name="Gryffindor", mascot="Lion", students=[ron_key, harry_key, hermione_key])
    snakes = House(name="Slytherin", mascot="Snake", students=[malfoy_key, crabbe_key, goyle_key])
    hufflepuff = House(name="Hufflepuff", mascot="Badger", students=[])
    ravenclaw = House(name="Ravenclaw",  mascot="Eagle", students=[])

    gryff.put()
    snakes.put()
    hufflepuff.put()
    ravenclaw.put()

    potions = Course(name="Potions", location="dungeon")
    dada = Course(name="Defense Against the Dark Arts", location="3C")
    transfiguration = Course(name="Transfiguration", location="Class 34")
    potions_key = potions.put()
    dada_key = dada.put()
    transfiguration_key = transfiguration.put()

    #dada
    Enrollment(student=ron_key, course=dada_key).put()
    Enrollment(student=harry_key, course=dada_key).put()
    Enrollment(student=hermione_key, course=dada_key).put()

    #potions
    Enrollment(student=hermione_key, course=potions_key).put()
    Enrollment(student=ron_key, course=potions_key).put()
    Enrollment(student=malfoy_key, course=potions_key).put()
    Enrollment(student=crabbe_key, course=potions_key).put()
    Enrollment(student=goyle_key, course=potions_key).put()

    #tranfirguration
    Enrollment(student=harry_key, course=transfiguration_key).put()
    Enrollment(student=hermione_key, course=transfiguration_key).put()
    Enrollment(student=crabbe_key, course=transfiguration_key).put()
    Enrollment(student=goyle_key, course=transfiguration_key).put()

    snape = Teacher(name="Severus Snape", years_experience=5, classes_taught=[potions_key])
    snape.put()
    moody = Teacher(name="Alastor Moody", years_experience=1, classes_taught=[dada_key])
    moody.put()
    mcgonagall = Teacher(name="Minerva McGonagall", years_experience=12, classes_taught=[transfiguration_key])
    mcgonagall.put()
Ejemplo n.º 3
0
def create_house(**kwargs):
    session = Session()
    house = House()
    for k, v in kwargs.iteritems():
        setattr(house, k, v)
    session.add(house)
    session.commit()
Ejemplo n.º 4
0
def newhouse_save():
    #接收数据
    params = request.form.to_dict()
    facility_ids = request.form.getlist('facility')
    #验证数据的有效性

    #创建对象并保存
    house = House()
    house.user_id = session['user_id']
    house.area_id = params.get('area_id')
    house.title = params.get('title')
    house.price = params.get('price')
    house.address = params.get('address')
    house.room_count = params.get('room_count')
    house.acreage = params.get('acreage')
    house.beds = params.get('beds')
    house.unit = params.get('unit')
    house.capacity = params.get('capacity')
    house.deposit = params.get('deposit')
    house.min_days = params.get('min_days')
    house.max_days = params.get('max_days')
    #根据设施的编号查询设施对象
    if facility_ids:
        facility_list = Facility.query.filter(
            Facility.id.in_(facility_ids)).all()
        house.facilities = facility_list
    house.add_update()
    #返回结果
    return jsonify(code=RET.OK, house_id=house.id)
Ejemplo n.º 5
0
 def __crawl_house_info(self, house):
     attrs = {
         'name': self.__get_location(house),
         'price': self.__get_price(house),
         'subway_distance': self.__get_subway_distance(house),
         'area': self.__get_area(house),
         'url': self.__get_url(house)
     }
     subway_name = self.__get_subway(house)
     sw = session.query(Subway).filter(Subway.name == subway_name).first()
     attrs['subway'] = sw
     attrs.update(self.__get_detail(attrs.get('url')))
     h = session.query(House).filter(
         House.number == attrs.get('number')).first()
     if h is None:
         h = House(**attrs)
         Logger.info('获取 {} 价格 {}'.format(h.name, h.price))
         self._add.append(h)
     else:
         if attrs.get('subway').name != h.subway.name:
             tmp_subway = attrs['subway']
         del attrs['subway']
         session.query(House).filter(House.name == h.name).update(attrs)
         if 'subway_2' in locals():
             h.subway = tmp_subway
         if h in session.dirty:
             Logger.info('{} 数据更新'.format(h.name))
             self._changes.append(h)
         else:
             Logger.info('{} 没有变化'.format(h.name))
     session.add(h)
     session.commit()
Ejemplo n.º 6
0
def art_add():
    form = ArtForm()
    if form.validate_on_submit():
        data = form.data
        print(data['category'],)
        if data['category'] == 1:
            category = '租房'
        if data['category'] == 2:
            category = '二手房'
        if data['category'] == 3:
            category = '商业地产'
        # 保存数据
        art = House(
            name= data['name'],
            category= category,
            number= data['number'],
            company= data['company'],
            address= data['address'],
            shop_address= data['shop_address'],
            created_time=datetime.datetime.now()
        )
        field.session.add(art)
        field.session.commit()
        flash(u"添加成功", 'ok')
    return render_template('art_add.html', title=u'添加', form=form)
Ejemplo n.º 7
0
def create_house(pigeon_id, name, cover_url, category):
    house = House(cover_url=cover_url,
                  name=name,
                  category=category,
                  view=1,
                  num_of_subed=0)
    house.key = ndb.Key(House, name, parent=ndb.Key(Pigeon, pigeon_id))
    house.put()
Ejemplo n.º 8
0
    def process_item(self, item, spider):
        # 写入小区房价
        if isinstance(item, FangtianxiaRentItem):
            db_house = self.session.query(House).filter(
                House.house_name == item['name']).filter(
                    House.city_name == item['city_name']).first()

            if db_house:
                if db_house.property_corp == u'暂无数据':
                    db_house.property_corp = item['property_corp']

                if db_house.developers == u'暂无数据':
                    db_house.developers = item['developers']

                if db_house.building_date == u'暂无数据':
                    db_house.building_date = item['building_date']

                if db_house.building_date == u'暂无数据':
                    db_house.building_date = item['building_date']

                # 需要添加判断, 以防同一个价格数据插入多次

                p = Price(price=item['price'],
                          origin=item['origin'],
                          months=item['months'],
                          crawl_time=item['crawl_date'],
                          uid=db_house.id)
                db_house.price.append(p)

            else:
                house = House(house_name=item['name'],
                              city_name=item['city_name'],
                              url=item['url'],
                              latitude=item['latitude'],
                              longitude=item['longitude'],
                              address=item['location'],
                              building_date=item['building_date'],
                              building_type=item['building_type'],
                              property_corp=item['property_corp'],
                              developers=item['developers'],
                              district=item['district'])
                price = Price(price=item['price'],
                              origin=item['origin'],
                              months=item['months'],
                              crawl_time=item['crawl_date'])

                house.price.append(price)
                self.session.add(house)

            try:
                self.session.commit()
            except Exception, e:
                print e
                self.session.rollback()
            return item
Ejemplo n.º 9
0
def newhome():
    if request.method == 'POST':
        #May not need to format types of input
        LandlordFName = request.form['landlordFName'].encode('ascii', 'ignore')
        LandlordLName = request.form['landlordLName'].encode('ascii', 'ignore')
        LandlordEmail = request.form['landlordEmail']
        Address1 = request.form['address1'].encode('ascii', 'ignore')
        Address2 = request.form['address2'].encode('ascii', 'ignore')
        City = request.form['city'].encode('ascii', 'ignore')
        State = request.form['state'].encode('ascii', 'ignore')
        Zipcode = request.form['zip']  # parseFloat deprecated
        Rooms = int(request.form['bedrooms'])
        ParkingSpots = int(request.form['parking'])
        MonthlyRent = int(request.form['rent'])
        UtilitiesIncluded = True if request.form[
            'utilities'] == 'true' else False
        Laundry = True if request.form['laundry'] == 'true' else False
        Pets = True if request.form['pets'] == 'true' else False
        Latitude = request.form['latitude']
        Longitude = request.form['longitude']
        DistFromCC = request.form['disttocc']
        #Finding corresponding landlord based on email
        someLandlord = Landlord.query.filter_by(Email=LandlordEmail).first()
        #If no landlord exists by that email
        if someLandlord == None:
            return jsonify([{
                'status':
                400,
                'message':
                'Landlord does not match any on file, please check the email.'
            }])
        house = House(someLandlord.Id, Address1, Address2, City, State,
                      Zipcode, Rooms, ParkingSpots, MonthlyRent,
                      UtilitiesIncluded, Laundry, Pets, Latitude, Longitude,
                      DistFromCC)
        db.session.add(house)
        #Handling SQLalchemy errors when a house cannot be inputted/already has the address
        #Will need to readjust once unique key is handled
        try:
            db.session.commit()
        except exc.IntegrityError:
            db.session.rollback()
            return jsonify([{
                'status':
                400,
                'message':
                'This house has already been listed as active'
            }])
        return jsonify([{'status': 200}])
    else:
        if 'username' in session:
            return render_template('newhome.html')
        else:
            return redirect(url_for('index'))
Ejemplo n.º 10
0
def newhome():
    #TOODshould there be a sessions check here?? Probably...
    if request.method == 'POST':
        #May not need to format types of input
        LandlordFName = request.form['landlordFName'].encode('ascii', 'ignore')
        LandlordLName = request.form['landlordLName'].encode('ascii', 'ignore')
        LandlordEmail = request.form['landlordEmail']
        Address1 = request.form['address1'].encode('ascii', 'ignore')
        Address2 = request.form['address2'].encode('ascii', 'ignore')
        City = request.form['city'].encode('ascii', 'ignore')
        State = request.form['state'].encode('ascii', 'ignore')
        Zipcode = request.form['zip']   # parseFloat deprecated 
        Rooms = int(request.form['bedrooms'])
        ParkingSpots = int(request.form['parking'])
        MonthlyRent = int(request.form['rent'])
        UtilitiesIncluded = True if request.form['utilities'] == 'true' else False
        Laundry = True if request.form['laundry'] == 'true' else False
        Pets = True if request.form['pets'] == 'true' else False
        Latitude = request.form['latitude']
        Longitude = request.form['longitude']
        DistFromCC = request.form['disttocc']
        DayAvailable = int(request.form['availableday'])
        MonthAvailable = int(request.form['availablemonth'])
        YearAvailable = int(request.form['availableyear'])
        DateAvailable = datetime(YearAvailable, MonthAvailable, DayAvailable, 0, 0)
        LeaseTerm = int(request.form['leaseterm'])
        #Finding corresponding landlord based on email
        someLandlord = Landlord.query.filter_by(Email=LandlordEmail).first()
        #If no landlord exists by that email
        if someLandlord == None:
            return jsonify([{'status':400, 'message':'Landlord does not match any on file, please check the email.'}]) 
        house = House(someLandlord.Id, Address1, Address2, City, State, Zipcode, \
                      Rooms, ParkingSpots, MonthlyRent, UtilitiesIncluded, Laundry, \
                      Pets, Latitude, Longitude, DistFromCC, DateAvailable, LeaseTerm,\
                      datetime.now(), datetime.now(), True)
        db.session.add(house)
        #Handling SQLalchemy errors when a house cannot be inputted/already has the address
        #Will need to read just once unique key is handled 
        try:
            db.session.commit() 
            mc.delete("Houses") # flush cache, it's now stale
            mc.delete("AllIds") # flush cache, it's now stale
        except exc.IntegrityError:
            db.session.rollback()
            return jsonify([{'status':400, 'message':'This house has already been listed as active'}])
        return jsonify([{'status':201, "houseID":house.Id}])
    else:   
        if 'username' in session:
            usertype = {"type": session['usertype']}
            return render_template('newhome.html', usertype=usertype)
        else:
            return redirect(url_for('auth_page.index'))
Ejemplo n.º 11
0
def add_number_of_schools():
    try:
        request_content = PostRequestInput(request.data)
        house = House(request_content.get_house_location())
        request_content.add_to_house_details(
            key="Schools", value=house.get_amenity_frequency("school"))
    except json.decoder.JSONDecodeError:
        return fail(400, "Could convert body to JSON")
    except TypeError:
        return fail(400, "Something is wrong with the input's data stractures")
    except KeyError:
        return fail(404, "Input should have lat and lon")
    except OverpassServiceError:
        return fail(503, "Please try again later")
    return jsonify(request_content.build_response())
def register_account():
    # get name data
    name = request.form['name']
    # get note data
    email = request.form['email']
    # get note data
    password = request.form['password']
    # create user
    newUser = User(name, email, password, 0)
    db.session.add(newUser)
    db.session.commit()

    house = House(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, email, 0)
    db.session.add(house)
    db.session.commit()
    return redirect(url_for('log_in'))
Ejemplo n.º 13
0
def getHouse(url, refer):
    global num
    headers = {
        "Referer":
        refer,
        "User-Agent":
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36",
    }

    req = request.Request(url, headers=headers)

    response = request.urlopen(req)

    content = response.read().decode()

    html = etree.HTML(content)

    house_list = html.xpath(
        '//ul[@id="house-lst"]/li/div[@class="info-panel"]/h2/a')
    for house in house_list:
        attrib = house.attrib
        housename = attrib["title"]
        houseurl = attrib["href"]
        print("%s : %s" % (housename, houseurl))
        print("%s 开始爬取" % housename)
        name, pf, Num = getUser(houseurl, url)
        print("%s 爬取结束" % housename)
        sleep(1)
        num += 1
        h = House()
        h.houseName = housename
        h.houseUrl = houseurl
        h.houseUser = name
        h.userPF = pf
        h.lookNum = Num
        h.save()
Ejemplo n.º 14
0
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Base, House, Student

engine = create_engine('sqlite:///hogwarts.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()

gryffindor = House(name="Gryffindor", image_url="http://images1.wikia.nocookie.net/__cb20120922143611/harrypotter/images/7/70/G_final.jpg")
hufflepuff = House(name="Hufflepuff", image_url="http://images3.wikia.nocookie.net/__cb20111027164827/harrypotter/images/3/3f/H_final.jpg")
ravenclaw = House(name="Ravenclaw", image_url="http://images2.wikia.nocookie.net/__cb20111021043857/harrypotter/images/d/da/R_final.jpg")
slytherin = House(name="Slytherin", image_url="http://images3.wikia.nocookie.net/__cb20111027165214/harrypotter/images/d/da/S_final.jpg")
session.add(gryffindor)
session.add(hufflepuff)
session.add(ravenclaw)
session.add(slytherin)
session.commit()

harry = Student(name="Harry Potter", image_url="http://images1.wikia.nocookie.net/__cb20111110202524/harrypotter/images/thumb/d/d4/Dhharryroomhighreso.jpg/250px-Dhharryroomhighreso.jpg", house=gryffindor)
ron = Student(name="Ron Weasley", image_url="http://images4.wikia.nocookie.net/__cb20090712085217/harrypotter/images/9/99/Ron_DH.PNG", house=gryffindor)
hermione = Student(name="Hermione Granger", image_url="http://images2.wikia.nocookie.net/__cb20120512155742/harrypotter/images/thumb/9/95/DeathlyPromo_Hermione.PNG/250px-DeathlyPromo_Hermione.PNG", house=gryffindor)
luna = Student(name="Luna Lovegood", image_url="http://images1.wikia.nocookie.net/__cb20101113121321/harrypotter/images/thumb/4/49/Luna_profile.jpg/250px-Luna_profile.jpg", house=ravenclaw)
cho = Student(name="Cho Chang", image_url="http://images3.wikia.nocookie.net/__cb20081103140303/harrypotter/images/thumb/c/c1/Cho_Chang_Profile.JPG/250px-Cho_Chang_Profile.JPG", house=ravenclaw)
cedric = Student(name="Cedric Diggory", image_url="http://images1.wikia.nocookie.net/__cb20111015180906/harrypotter/images/thumb/c/c5/Cedric.jpg/250px-Cedric.jpg", house=hufflepuff)
draco = Student(name="Draco Malfoy", image_url="http://images1.wikia.nocookie.net/__cb20100925140918/harrypotter/images/thumb/2/21/Dracodh.jpg/250px-Dracodh.jpg", house=slytherin)
pansy = Student(name="Pansy Parkinson", image_url="http://images1.wikia.nocookie.net/__cb20110801070343/harrypotter/images/thumb/6/62/Pansyinfobox.jpg/250px-Pansyinfobox.jpg", house=slytherin)

session.add(harry)
session.add(ron)
session.add(hermione)
Ejemplo n.º 15
0
    def get(self):

        # CLEAR All
        query = Department.query()
        entries = query.fetch()
        entry_keys = [entry.key for entry in entries]
        ndb.delete_multi(entry_keys)

        query = House.query()
        entries = query.fetch()
        entry_keys = [entry.key for entry in entries]
        ndb.delete_multi(entry_keys)

        query = Class.query()
        entries = query.fetch()
        entry_keys = [entry.key for entry in entries]
        ndb.delete_multi(entry_keys)

        query = Student.query()
        entries = query.fetch()
        entry_keys = [entry.key for entry in entries]
        ndb.delete_multi(entry_keys)

        user = users.get_current_user()
        if user and users.is_current_user_admin():
            # Create houses
            houses = ['Gryffindor', 'Hufflepuff', 'Ravenclaw', 'Slytherin']
            for house in houses:
                House(house_name=house).put()

            # Create departments
            deps = [
                {
                    'code': 'CS',
                    'name': 'Computer Science'
                },
                {
                    'code': 'BA',
                    'name': 'Business Administration'
                },
                {
                    'code': 'PHY',
                    'name': 'Physics'
                },
                {
                    'code': 'MAT',
                    'name': 'Mathematics'
                },
                {
                    'code': 'DRA',
                    'name': 'Drama'
                },
                {
                    'code': 'ECE',
                    'name': 'Electrical Engineering'
                },
            ]
            for dep in deps:
                Department(id=dep['code'], dept_name=dep['name']).put()

            # Create 40 classes for department, update depts
            classes_count = 40
            for dept in deps:
                department_key = ndb.Key('Department', dept['code'])
                department_obj = department_key.get()
                for i in range(classes_count):
                    class_full_name = "%s - Class %d" % (dept['name'], i)
                    department = department_key
                    class_key = Class(id='%s-%d' % (dept['code'], i),
                                      class_full_name=class_full_name).put()
                    department_obj.classes.append(class_key)
                department_obj.put()

            # Create students
            students_count = 1000
            random.seed()
            all_classes = Class.query().fetch()
            all_houses = House.query().fetch()
            for i in range(students_count):
                house = random.choice(all_houses)
                house_key = house.key
                student_name = 'Student %d' % i
                student_year = random.randint(1, 4)
                current_classes = random.sample(all_classes, 5)
                current_classes_keys = [el.key for el in current_classes]
                all_classes_keys = [el.key for el in all_classes]
                leftovers = set(all_classes_keys) - set(current_classes_keys)
                completed_classes_keys = random.sample(leftovers,
                                                       6 * (student_year - 1))
                student_key = Student(student_name=student_name,
                                      student_year=student_year,
                                      current_classes=current_classes_keys,
                                      completed_classes=completed_classes_keys,
                                      house=house_key).put()
                house.students.append(student_key)
                for el in current_classes:
                    el.students_enrolled.append(student_key)
            # save new status of houses and current classes
            for house in all_houses:
                house.put()
            for current_class in all_classes:
                current_class.put()

        elif user and not users.is_current_user_admin():
            self.redirect(users.create_logout_url('/create_entries'))
        else:
            self.redirect(users.create_login_url('/create_entries'))
Ejemplo n.º 16
0
def occupy_houses():
    houses = get_houses()
    for house in houses:
        new_house = House(name=house)
        db.session.add(new_house)
    db.session.commit()