Esempio n. 1
0
 def save(self):
     '''Save entry in db'''
     data = self.cleaned_data
     name = data['name']
     for pe_name in data['production_environment']:
         prod_env = ProductionEnvironment.objects.filter(name=pe_name)
         area = Area(name=name, productionEnvironment=prod_env[0])
         area.save()
Esempio n. 2
0
def load_data_to_db(row):
    province_id = int(row['CH_ID'])
    province = Area.objects(aid=province_id).first()
    if not province:
        try:
            province = Area(level='ch',
                            th_name=row['CHANGWAT_T'].split()[-1],
                            en_name=row['CHANGWAT_E'],
                            aid=province_id)
            province.save()
        except ValidationError as e:
            print(row['CHANGWAT_T'], row['CHANGWAT_E'])

    amphoe_id = int(row['AM_ID'])
    amphoe = Area.objects(aid=amphoe_id).first()
    if not amphoe:
        try:
            amphoe = Area(level='am',
                          th_name=row['AMPHOE_T'].split()[1],
                          en_name=row['AMPHOE_E'],
                          aid=amphoe_id,
                          parent=province)
            amphoe.save()
        except ValidationError as e:
            print(row['AMPHOE_E'], row['AMPHOE_T'])

    tambon_id = int(row['TA_ID'])
    tambon = Area.objects(aid=tambon_id).first()
    if not tambon:
        try:
            tambon = Area(
                level='ta',
                th_name=row['TAMBON_T'].split()[1],
                aid=tambon_id,
                parent=amphoe,
                latlng=[float(row['LONG']),
                        float(row['LAT'])],
            )
            tambon.save()
        except ValidationError as e:
            print(row['TAMBON_E'], row['TAMBON_T'])
            sys.exit(1)
    else:
        # the tambon exists, do nothing.
        '''
        print('{}, {}, {} already in the database'.format(
            tambon.th_name.encode('utf8'),
            amphoe.th_name.encode('utf8'),
            province.th_name.encode('utf8')
        ))
        '''
        return

    amphoe.childs.append(tambon)
    amphoe.save()

    province.childs.append(amphoe)
    province.save()
Esempio n. 3
0
    def setUp(self):

       u = get_user_model()()
       u.id = 1
       u.username = '******'
       u.first_name = 'test'
       u.last_name = 'test'
       u.email = 'test'
       u.set_password('test') 
       u.save()

       p = Provider()
       p.id = 1
       p.user = u
       p.name = "test"  
       p.email = "*****@*****.**" 
       p.phone = 12312312
       p.currency = "USD"
       p.language = "DE"
       p.save()

       a = Area()
       a.id = 1
       a.name = "test"
       a.price = 10.20
       a.provider = p
       a.poly = '{ "type": "Polygon", "coordinates": [ [ [0, 0], [10, 50], [10, 0], [0, 0] ] ] }'
       a.save()

       u = get_user_model()()
       u.id = 2
       u.username = '******'
       u.first_name = 'test2'
       u.last_name = 'test2'
       u.email = '*****@*****.**'
       u.set_password('test') 
       u.save()

       p = Provider()
       p.id = 2
       p.user = u
       p.name = "test2"  
       p.email = "*****@*****.**" 
       p.phone = 123123212
       p.currency = "USD"
       p.language = "EN"
       p.save()

       a = Area()
       a.id = 2
       a.name = "test2"
       a.price = 10.20
       a.provider = p
       a.poly = '{ "type": "Polygon", "coordinates": [ [ [0, 0], [10, 50], [10, 0], [0, 0] ] ] }'
       a.save()

       self.url = reverse("area:area-detail",kwargs={"pk":1})
def add_entry():
    connect(host='mongodb://*****:*****@ds255539.mlab.com:55539/9321test')
    parser = reqparse.RequestParser()
    parser.add_argument('name', type=str, help='LGA name input error')
    parser.add_argument('postcode', type=int, help='Postcode input error')
    args = parser.parse_args()
    postcode = args.get("postcode")#check postcode to find the LGA name
    ############################# postcode ######################################
    if postcode:
        name_list = []
        for k, v in postdic.items():
            for s in v:
                if s == postcode:
                    name_list.append(k) #regions founded

        if name_list == []: #check whether the input postcode in the postdic
            return jsonify(Input_postcode=False),404

        feed = AtomFeed(title='Multi-collections POST complete', feed_url=request.url)
        for a in Area.objects:
            for n in name_list:
                if a.name.lower().replace(' ', '') == n.lower().replace(' ', ''):
                    name_list.remove(n)
        for n in name_list:
            name = n
            url = 'http://127.0.0.1:5000' + url_for('show_entry', name=name)

            # download from the internet -- check 400 - if 2 not in 1 -- not up-to-date
            dld_url = 'http://www.bocsar.nsw.gov.au/Documents/RCS-Annual/'+name+'lga.xlsx'
            r = requests.get(dld_url)
            with open(name + 'lga.xlsx','wb') as f:
                f.write(r.content)

            excel_url = name + 'lga.xlsx'
            data = xlrd.open_workbook(excel_url).sheets()[0]
            id = 0  # initialisation id for embeded document
            p = []  # used to collect Offense()
            for row in range(7, 69):  # for further update
                id = id + 1
                offence_group = str(data.cell(row, 0).value)
                if offence_group != '':
                    backup = offence_group
                if offence_group == '':
                    offence_group = backup
                offence_type = str(data.cell(row, 1).value)
                incidents_2012 = str(data.cell(row, 2).value)
                rate_2012 = str(data.cell(row, 3).value)
                incidents_2013 = str(data.cell(row, 4).value)
                rate_2013 = str(data.cell(row, 5).value)
                incidents_2014 = str(data.cell(row, 6).value)
                rate_2014 = str(data.cell(row, 7).value)
                incidents_2015 = str(data.cell(row, 8).value)
                rate_2015 = str(data.cell(row, 9).value)
                incidents_2016 = str(data.cell(row, 10).value)
                rate_2016 = str(data.cell(row, 11).value)
                trend_24m = str(data.cell(row, 12).value)
                trend_60m = str(data.cell(row, 13).value)
                lga_rank = str(data.cell(row, 14).value)
                p.append(Offense(id, offence_group, offence_type, incidents_2012, rate_2012, incidents_2013, rate_2013, \
                                 incidents_2014, rate_2014, incidents_2015, rate_2015, incidents_2016, \
                                 rate_2016, trend_24m, trend_60m, lga_rank))
            t = Area(name, p)
            t.save()
            entry = FeedEntry(title=name, url=url, updated=datetime.datetime.utcnow(), author={'name': 'admin'})
            feed.add(entry)

        response = make_response(feed.to_string())
        response.mimetype = "application/atom+xml"

    ############################# name ######################################
    else: #if postcode not given, check the name field
        name = args.get("name").lower().replace(' ', '')
        if not name:
            return jsonify(Input=False), 404
        url = 'http://127.0.0.1:5000' + url_for('show_entry', name=name)

        #if LGA or postcode that already has been imported before
        for a in Area.objects:
            if a.name.lower().replace(' ', '') == name.lower().replace(' ', ''):
                feed = AtomFeed(title='Already existed', feed_url=url)
                entry = FeedEntry(title=name, url=url, updated=datetime.datetime.utcnow(),
                                author={'name': 'admin'})
                feed.add(entry)
                response = make_response(feed.to_string())
                response.mimetype = "application/atom+xml"
                return response, 200

        #download from the internet -- check 400 - if 2 not in 1 -- not up-to-date
        dld_url = 'http://www.bocsar.nsw.gov.au/Documents/RCS-Annual/'+name+'lga.xlsx'
        r = requests.get(dld_url)
        with open(name + 'lga.xlsx','wb') as f:
           f.write(r.content)

        excel_url = name + 'lga.xlsx'
        data = xlrd.open_workbook(excel_url).sheets()[0]
        id = 0 #initialisation id for embeded document
        p = [] #used to collect Offense()
        for row in range(7, 69): #for further update
            id = id+1
            offence_group = str(data.cell(row, 0).value)
            if offence_group != '':
                backup = offence_group
            if offence_group == '':
                offence_group = backup
            offence_type = str(data.cell(row, 1).value)
            incidents_2012 = str(data.cell(row, 2).value)
            rate_2012 = str(data.cell(row, 3).value)
            incidents_2013 = str(data.cell(row, 4).value)
            rate_2013 = str(data.cell(row, 5).value)
            incidents_2014 = str(data.cell(row, 6).value)
            rate_2014 = str(data.cell(row, 7).value)
            incidents_2015 = str(data.cell(row, 8).value)
            rate_2015 = str(data.cell(row, 9).value)
            incidents_2016 = str(data.cell(row, 10).value)
            rate_2016 = str(data.cell(row, 11).value)
            trend_24m = str(data.cell(row, 12).value)
            trend_60m = str(data.cell(row, 13).value)
            lga_rank = str(data.cell(row, 14).value)
            p.append(Offense(id, offence_group, offence_type, incidents_2012, rate_2012, incidents_2013, rate_2013, \
                            incidents_2014, rate_2014, incidents_2015, rate_2015, incidents_2016, \
                            rate_2016, trend_24m, trend_60m, lga_rank))
        t = Area(name,p)
        t.save()

        feed = AtomFeed(title='Sucess POST Activity', feed_url=url)
        entry = FeedEntry(title=name, url=url, updated=datetime.datetime.utcnow(), author={'name': 'admin'})
        feed.add(entry)
        response = make_response(feed.to_string())
        response.mimetype = "application/atom+xml"

    return response, 201