def test_cross_streets_outside_nyc(self): from fixcity.bmabr.views import cross_streets_for_rack rack = Rack(address='i have no idea where this is', title='far away', date=datetime.utcfromtimestamp(0), email='*****@*****.**', location=Point(20.0, 20.0, srid=SRID), ) self.assertEqual(cross_streets_for_rack(rack), (None, None))
def test_cross_streets(self): from fixcity.bmabr.views import cross_streets_for_rack rack = Rack(address='67 s 3rd st, brooklyn, ny 11211', title='williamsburg somewhere', date=datetime.utcfromtimestamp(0), email='*****@*****.**', location=Point(-73.964858020364, 40.713349294636, srid=SRID), ) self.assertEqual(cross_streets_for_rack(rack), (u"WYTHE AV", u"BERRY ST"))
def make_csv(bo, outfile): import csv csv_writer = csv.writer(outfile) # Initial heading row. Does DOT want this? csv_writer.writerow( ['date received', 'generated by', 'establishment', 'street address', 'from', 'to', 'borough', 'cb', 'neighborhood', # ... skip some we lack 'email', # ... skip some more ... 'status', # ... skip more ... 'image', 'doc', ]) from fixcity.bmabr.views import cross_streets_for_rack, neighborhood_for_rack from fixcity.bmabr.models import CommunityBoard from fixcity.bmabr.models import StatementOfSupport for rack in bo.racks: from_st, to_st = cross_streets_for_rack(rack) neighborhood = neighborhood_for_rack(rack) cb = CommunityBoard.objects.get(the_geom__intersects=rack.location) try: site_domain = Site.objects.get_current().domain photo_url = 'http://%s%s' % (site_domain, rack.photo.url) except (ValueError, AttributeError): photo_url = '' row = [ rack.date.strftime('%m/%d/%Y'), # 'date' rack.source or 'web', # 'generated by' rack.title, # 'establishment' rack.address, # 'street address' ... actually the whole address. from_st, # 'from' to_st, # 'to' cb.borough.boroname, # 'borough' ... actually should be a 2-letter code but we don't have those. cb.borocd, # 'cb' in DOT's extended format, eg. 301 for bk 1 neighborhood, # 'neighborhood' rack.email, # 'email' 'request', # 'status' photo_url, # 'image' ] statement_query = StatementOfSupport.objects.filter(s_rack=rack.id) for s in statement_query: row.append(s.file.url) csv_writer.writerow(row)
def make_rack_page(rack): # I suppose we 'should' use a PageTemplate and Frames and so # forth, but frankly I don't have enough time to learn Platypus # well enough to understand them. flowables = [] flowables.append(Paragraph('%s. %s' % (rack.id, rack.title), stylesheet['h1'])) flowables.append(Paragraph(rack.address, stylesheet['h1'])) from fixcity.bmabr.views import cross_streets_for_rack prev_street, next_street = cross_streets_for_rack(rack) prev_street = str(prev_street).title() next_street = str(next_street).title() flowables.append(Paragraph( 'Cross Streets: %s and %s' % (prev_street, next_street), stylesheet['h2'])) # Make a map image. # Derive the bounding box from rack center longitude/latitude. # The offset was arrived at empirically by guess-and-adjust. ratio = 0.65 x_offset = 0.002 y_offset = x_offset * ratio bounds = (rack.location.x - x_offset, rack.location.y - y_offset, rack.location.x + x_offset, rack.location.y + y_offset) image_pixels_x = 640 # That's the most google will give us. image_pixels_y = image_pixels_x * ratio image_inches_x = 3.5 * inch image_inches_y = image_inches_x * ratio tries = 3 for i in range(tries): try: the_map = get_map(bounds, size=(image_pixels_x, image_pixels_y), format='jpg') break except RuntimeError: if i == tries -1: raise else: continue map_row = [Image(the_map, width=image_inches_x, height=image_inches_y)] # Photo, if provided. if rack.photo.name: photo = rack.photo.extra_thumbnails['large'] map_row.append(Image(photo.dest, width=image_inches_x, )) #height=image_inches_y)) else: map_row.append(Paragraph('<i>no photo provided</i>', normalStyle)) map_table = Table([map_row], colWidths=[image_inches_x + 0.25, image_inches_x + 0.25], style=TableStyle([ ('ALIGN', (0, 0), (-1, -1), 'CENTER'), ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), ])) flowables.append(map_table) flowables.append(Spacer(0, 0.25 * inch)) flowables.append(Paragraph('Description', stylesheet['h1'])) flowables.append(Paragraph(rack.description or 'none', normalStyle)) votes = Vote.objects.get_score(rack) flowables.append(Spacer(0, 0.25 * inch)) flowables.append(Paragraph('<b>%d likes</b>' % votes['score'], normalStyle)) comment_count = Comment.objects.filter( content_type=ContentType.objects.get_for_model(rack), object_pk=smart_unicode(rack.pk), site__pk=settings.SITE_ID).count() flowables.append(Paragraph('<b>%d comments</b>' % comment_count, normalStyle)) # TODO: append comment text! return flowables
def make_csv(bo, outfile): logger.info('Start make_csv') import csv fieldnames = ("ID", "TrackNum", "DateIn", "Action_", "GenBy", "Requestor", "Address", "Address2", "ReqBoro", "ZipCode", "Phone", "Email", "Title", "RComment", "LocName", "LocAdd", "LocAddNum", "Street", "From_", "To_", "Width", "x", "y", "Boro", "LocZip", "CB", "Comments", "Area", "SubStation", "SubLine", "Status", "Sited", "SitedBy", "InspDate", "InspBy", "Side", "Sm", "Lg", "RackType", "MeterNum", "DOTRespDat", "CBNoteDate", "CommNote", "WorkOrder", "InstDate", "Invoice", "Image_", "txtImgName") # In python 2.7 you can do: csv_writer.writeheader() outfile.write(', '.join(fieldnames)) outfile.write('\r\n') csv_writer = csv.DictWriter(outfile, fieldnames) from fixcity.bmabr.views import cross_streets_for_rack, neighborhood_for_rack from fixcity.bmabr.models import CommunityBoard from fixcity.bmabr.models import StatementOfSupport for rack in bo.racks: from_st, to_st = cross_streets_for_rack(rack) neighborhood = neighborhood_for_rack(rack) cb = CommunityBoard.objects.get(the_geom__intersects=rack.location) try: site_domain = Site.objects.get_current().domain photo_url = 'http://%s%s' % (site_domain, rack.photo.url) except (ValueError, AttributeError): photo_url = '' row = { "ID": '', 'TrackNum': '', 'DateIn': rack.date.strftime('%m/%d/%Y'), 'Action_': 'REQUEST', 'GenBy': 'INTERNET', #rack.source or 'web', 'Requestor': rack.user or rack.email, 'Address': '', 'Address2': '', # user's address? 'ReqBoro': cb.borough.boroname, # user's or rack's?? actually should be a 2-letter code but we just have names. 'ZipCode': '', 'Phone': '', 'Email': rack.email, 'Title': '', # eg. Dr, Mr, Mrs ... 'RComment': rack.description, 'LocName': rack.title, 'LocAdd': '', # Number part of rack address? 'LocAddNum': '', # Number part of rack address again? 'Street': rack.address, # Should be just the street part? 'From_': from_st, 'To_': to_st, 'Width': '', # what's this? 'x': '', # maybe a longitude? but in some other units? 'y': '', # maybe a latitude? but what are the units?? 'Boro': cb.borough.borocode, # or what? should be '1' for manhattan?? 'LocZip': '', # rack's zip? don't have it. 'CB': cb.borocd, 'Comments': '', # for DOT's internal use? 'Area': neighborhood, 'SubStation': '', # subway station 'SubLine': '', # subway line 'Status': 'P', # always submit as 'Pending'. # DOT internal statuses: "sited" (approved for install), # "rejected" (with a comment), "installed" (DOT has # audited it post-install) 'Sited': '', 'SitedBy': '', # only if installed. 'InspDate': '', 'InspBy': '', 'Side': '', 'Sm': '', 'Lg': '', 'RackType': '', 'MeterNum': '', 'DOTRespDat': '', 'CBNoteDate': '', 'CommNote': '', 'WorkOrder': '', 'InstDate': '', 'Invoice': '', 'Image_': '', 'txtImgName': photo_url, # URL ok here? } statement_query = StatementOfSupport.objects.filter(s_rack=rack.id) for s in statement_query: # What to do with these? DOT doesn't really have a place for them. #row.append(s.file.url) pass # DictWriter doesn't handle encoding automatically. for k, v in row.items(): if v is None: v = u'' if isinstance(v, basestring): row[k] = v.encode('utf8') csv_writer.writerow(row) logger.info('Start make_csv')