Пример #1
0
 def post(self, request, *args, **kwargs):
     message = ""
     postData = json.loads(request.body)
     location = Location.objects.filter(id=int(postData.get('id', '-1')))
     location_id = None;
     obj = {}
     if location.exists():
         location_id = location.first().pk
         location = location.first()
         location.title=postData.get('title', '')
         location.description=postData.get('description', '')
     else:
         location = Location(
             title=postData.get('title', ''), 
             description=postData.get('description', ''), 
             latitude=postData.get('latitude', ''),
             longitude=postData.get('longitude', ''),
             created_user_id=postData.get('created_user_id', ''))
         base64img = postData.get('image', '')
         if base64img != '':
             image_data = b64decode(base64img)
             location.picture.save(location.title+'.jpg', ContentFile(image_data), save=False)
     location.save()
     location_id = location.pk
     message = self.json_dumps(self.get_location_dict(location))
     return http.HttpResponse(message)
Пример #2
0
    def _import(self, folder):
        json_folder = folder + JSONPATTERN

        print("Nuking locations:", end='')
        Location.objects.all().delete()
        print("Done")
        print("Importing: %s" % json_folder)

        file_counter = 0

        for filename in iglob(json_folder):
            with open(filename) as f:
                file_counter += 1
                record_id = self._file_to_record_number(folder, filename)
                print("[%04d] %s " % (file_counter, record_id), end="")
                details = json.load(f)[0]

                try:
                    location = Location.objects.get(
                        place_id=int(details["place_id"]))
                    print("loaded ", end="")

                except Location.DoesNotExist:
                    location = Location(
                        place_id=int(details["place_id"]),
                        display_name=details["display_name"],
                        lat=float(details["lat"]),
                        lon=float(details["lon"]),
                        osm_id=int(details["osm_id"]),
                        geom=details["geojson"]
                        if "geojson" in details else None,
                    )
                    location.save()
                    print("saved ", end="")

                # Synching with record
                try:
                    record = Record.objects.get(record_number=record_id)
                    location.records.add(record)
                    location.save()
                    print("linked ", end="")

                except Record.DoesNotExist:
                    print("record missing")
                    location.delete()

                print("")
Пример #3
0
def record_update_with_location(record_id, place_id):
    try:
        location = Location.objects.get(place_id=place_id)
    except Location.DoesNotExist:
        details = grab_location_data(place_id)
        location = Location(
            place_id=int(details["place_id"]),
            display_name=details['localname'],
            lat=details["centroid"]["coordinates"][0],
            lon=details["centroid"]["coordinates"][1],
            osm_id=int(details["osm_id"]),
            geom=details["geometry"] if "geometry" in details else None,
        )
        location.save()

    record = Record.objects.get(id=record_id)
    if record.location_set.count():
        record.location_set.all().delete()
        record.save()

    record.location_set.add(location)
    record.save()

    pass
Пример #4
0
def add(request, level):
    """ Respond to the "/geo/editor/location/add/X/" URL.

        We let the user add a location with the given level ID.
    """
    if not request.user.is_authenticated:
        return HttpResponseRedirect("/geo/editor/")

    try:
        level = Level.objects.get(level=level)
    except Level.DoesNotExist:
        return HttpResponseRedirect("/geo/editor/location/select/" +
                                    str(level.level) + "/")

    if request.method == "GET":
        form   = LocationDetailsForm()
        errMsg = None
    elif request.method == "POST":
        if request.POST.get("cancel") == "Cancel":
            return HttpResponseRedirect("/geo/editor/location/select/" +
                                        str(level.level) + "/")

        form = LocationDetailsForm(request.POST)
        errMsg = None
        if form.is_valid():
            code         = request.POST['code']
            name         = request.POST['name']
            display_name = request.POST['display_name']
            abbreviation = request.POST['abbreviation']
            display_lat  = request.POST['display_point_lat']
            display_long = request.POST['display_point_long']

            if code == "":
                errMsg = "You must enter a code for this location."
            elif name == "":
                errMsg = "You must enter a name for this location."
            elif Location.objects.filter(code=code).count() > 0:
                    errMsg = "There is already a location with that code."
            elif Location.objects.filter(level=level, name=name).count() > 0:
                    errMsg = "There is already a " + level.name.lower() \
                           + " with that name."

            if errMsg == None:
                location = Location()
                location.level         = level
                location.code          = code
                location.name          = name
                location.display_name  = display_name
                location.abbreviation  = abbreviation
                location.min_zoom_lat  = decimal.Decimal("0.00")
                location.min_zoom_long = decimal.Decimal("0.00")
                location.max_zoom_lat  = decimal.Decimal("0.00")
                location.max_zoom_long = decimal.Decimal("0.00")

                if display_lat != None:
                    location.display_lat = decimal.Decimal(display_lat)

                if display_long != None:
                    location.display_long = decimal.Decimal(display_long)

                location.population    = 0
                location.area          = 0
                location.averageIncome = 0
                location.save()

                # Send a signal telling the rest of the system that the
                # location record was added.

                location_changed.send(sender=None)

                # Finally, tell the user's web browser to reload the page.

                return HttpResponseRedirect("/geo/editor/location/details/" +
                                            code + "/")

    return render_to_response("editor/templates/editForm.html",
                              {'title'   : "Geodatabase Editor",
                               'heading' : "Add " + level.name,
                               'errMsg'  : errMsg,
                               'form'    : form},
                              context_instance=RequestContext(request))
Пример #5
0
    start = datetime.datetime(2014,1,1,1,1,1)
    stop = datetime.datetime.now()
    return start + datetime.timedelta(seconds=random.randint(0,int((stop-start).total_seconds())))



# counter to be used for node numbers
c = count()

# create nodes
for query in ["center","house"]:
    data = json.loads("".join(urllib2.urlopen("http://whereis.mit.edu/search?type=query&q="+query+"&output=json").readlines()))
    
    for d in data:
        # create locations
        loc= Location(name=d['name'], latitude=d['lat_wgs84'], longitude=d['long_wgs84'])
        loc.save()
        # create node
        node_num = c.next()
        # create node's IP address
        ip = IP(node_number=node_num, address="18.10.13."+str(node_num))
        ip.save()
        # save node
        node = Node(location=loc, number=node_num, ip=ip)
        node.save()

# create random datapoints for each node

nodes = Node.objects.all()

for node in nodes:
Пример #6
0
# Data to be entered into the database, so we don't have to manually enter it
# every time we run sync_db

from data.models import User, Category, Item, Reservation, Claim, Location
import time

# Make location
baker = Location.create_location('Baker Hall', '42.35666', '-71.09582')
bexely = Location.create_location('Bexley Hall', '42.35852604285305', '-71.09368236574556')
burton_conner = Location.create_location('Burton-Conner House', '42.35607', '-71.09811')
east_campus = Location.create_location('East Campus', '42.36026', '-71.08880')
macgregor = Location.create_location('MacGregeor House', '42.35543', '-71.09981')
maseeh = Location.create_location('Maseeh Hall', '42.35764801061463', '-71.09338732275393')
mccormick = Location.create_location('McCormick Hall', '42.35731', '-71.09454')
new_house = Location.create_location('New House', '42.35543', '-71.10023')
next_house = Location.create_location('Next House', '42.354714540813504', '-71.10203476461794')
random_hall = Location.create_location('Random Hall', '42.36191', '-71.09821')
senior = Location.create_location('Senior House', '42.36007', '-71.08689')
simmons = Location.create_location('Simmons Hall', '42.35733', '-71.10105')

# Add in categories
girs = Category.create_category('GIRs')
furniture = Category.create_category('Furniture')
clothing = Category.create_category('Clothing')
santorum = Category.create_category('Rainbow-colored Santorum Posters')

kerry = User.create_user('kxing', 'Kerry', 'Xing', '*****@*****.**', '(123)456-7890', next_house)
paul = User.create_user('pwh', 'Paul', 'Hemberger', '*****@*****.**', '(234)567-8901', maseeh)
sean = User.create_user('scockey', 'Sean', 'Cockey', '*****@*****.**', '(345)678-9012', maseeh)
sarine = User.create_user('sarine', 'Sarine', 'Shahmirian', '*****@*****.**', '(456)789-0123', bexely)
Пример #7
0
def main():
    """ Our main program.
    """
    # Start by deleting our existing location data.

    print "Deleting existing locations..."

    Location.objects.all().delete()

    # Load the Level objects into memory, for speed.

    levels = {} # Maps level number to Level object.
    for level in range(1, 9):
        levels[level] = Level.objects.get(level=level)

    # Now import the location records.

    print "Importing locations"

    f = open("../Location Data/locations.txt", "r")
    for i,line in enumerate(f.readlines()[1:]):
        if i % 1000 == 0:
            print "  %d locations imported" % i

        parts = line.split("\t")
        if len(parts) == 1: continue
        code  = parts[0]
        level = int(parts[1][0])
        name  = parts[2]

        loc = Location()
        loc.level = levels.get(level)
        loc.code  = code
        loc.name  = name
        loc.save()

    f.close()

    print
    print "Imported %d locations" % Location.objects.count()
    print

    # Load all the Location objects into memory, for speed of access.

    print "Loading locations into memory..."

    locations = {} # Maps location code to Location object.
    for location in Location.objects.all():
        locations[location.code] = location

    print
    print "Loaded %d locations" % len(locations)
    print

    # Load the list of parents into memory.

    parents = {} # Maps location code to list of parent location codes.

    print "Loading parents into memory..."

    f = open("Location Data/all-parents.txt", "r")
    for line in f.readlines():
        parts = line.rstrip().split("\t")

        locationCode = parts[0]
        if len(parts) > 1:
            parentCodes = parts[1:]
        else:
            parentCodes = []

        parents[locationCode] = parentCodes

    f.close()

    # Load the list of children into memory.

    children = {} # Maps location code to list of child location codes.

    print "Loading children into memory..."

    f = open("Location Data/all-children.txt", "r")
    for line in f.readlines():
        parts = line.rstrip().split("\t")

        locationCode = parts[0]
        if len(parts) > 1:
            childCodes = parts[1:]
        else:
            childCodes = []

        children[locationCode] = childCodes

    f.close()

    # Load the list of neighbours into memory.

    neighbours = {} # Maps location code to list of neighbour location codes.

    print "Loading neighbours into memory..."

    f = open("Location Data/neighbours.txt", "r")
    for line in f.readlines():
        parts = line.rstrip().split("\t")

        locationCode = parts[0]
        if len(parts) > 1:
            neighbourCodes = parts[1:]
        else:
            neighbourCodes = []

        neighbours[locationCode] = neighbourCodes

    f.close()

    # Finally, update each location with its list of parents, children and
    # neighbours.

    print "Collating location data..."

    for i,location in enumerate(Location.objects.all()):
        if i % 1000 == 0:
            print "  %d locations collated" % i

        changed = False

        if location.code in children:
            childLocations = []
            for childCode in children[location.code]:
                childLocation = locations.get(childCode)
                if childLocation != None:
                    childLocations.append(childLocation)
                else:
                    print "Missing location: " + repr(childCode)
            location.children = childLocations
            changed = True

        if location.code in parents:
            parentLocations = []
            for parentCode in parents[location.code]:
                parentLocation = locations.get(parentCode)
                if parentLocation != None:
                    parentLocations.append(parentLocation)
                else:
                    print "Missing location: " + repr(parentCode)
            location.parents = parentLocations
            changed = True

        if location.code in neighbours:
            neighbourLocations = []
            for neighbourCode in neighbours[location.code]:
                neighbourLocation = locations.get(neighbourCode)
                if neighbourLocation != None:
                    neighbourLocations.append(neighbourLocation)
                else:
                    print "Missing location: " + repr(neighbourCode)
            location.neighbors = neighbourLocations
            changed = True

        if changed:
            location.save()

    # That's all, folks!

    print "All done!"
 def create_location(self, chat, user, photo, longitude, latitude):
     location = Location(chat, user, photo, longitude, latitude)
     self._context.get_context().add(location)
     self._context.get_context().flush()
     self._context.get_context().refresh(location)
     return location
Пример #9
0
def import_data(f, log):
    """ Import the data in 'f', recording errors etc into 'log'.

        'f' is a temporary file holding the data to import, and 'log' is a list
        of strings.

        We import the data from 'f', updating the Location records, etc, as we
        go along.  If we encounter any errors, or want to notify the user of an
        action which has occurred, we append the error or notification to the
        'log' list.
    """
    locs_with_names = set() # Set of location codes which we had names for.
    cur_loc         = None  # Location object we are currently importing.

    for line in f:
        line = line.rstrip()
        if len(line) == 0: continue # Ignore blanks.
        parts = line.split("\t")
        if len(parts) < 2:
            log.append("File contains invalid data.  Are you sure it's " \
                       + "a tab-delimited text file?")
            break

        # Process this line.

        loc_code = parts[0]
        field    = parts[1].lower()

        if len(parts) >= 3:
            value = parts[2]
        else:
            value = ""

        if cur_loc == None or loc_code != cur_loc.code:
            # We're starting a new location.
            if cur_loc != None:
                # Save the previous location to disk.
                cur_loc.save()
                if is_new:
                    log.append("Added location " + cur_loc.code)
                else:
                    log.append("Updated location " + cur_loc.code)

            # Load the new location into memory.

            try:
                cur_loc = Location.objects.get(code=loc_code)
                is_new  = False
            except Location.DoesNotExist:
                cur_loc = Location() # Create a new location.
                cur_loc.code = loc_code
                is_new  = True

        # Import the field specified by this line in the import field.

        if field == "level":

            try:
                level = Level.objects.get(level=int(value))
            except:
                level = None

            if level != None:
                cur_loc.level = level

        elif field == "full_name":

            cur_loc.name = value

        elif field == "display_name":

            cur_loc.display_name = value

        elif field == "min_zoom_lat":

            try:
                cur_loc.min_zoom_lat = decimal.Decimal(value)
            except:
                log.append("Invalid min_zoom_lat value '" + value +
                           "' for location " + loc_code)

        elif field == "min_zoom_long":

            try:
                cur_loc.min_zoom_long = decimal.Decimal(value)
            except:
                log.append("Invalid min_zoom_long value '" + value +
                           "' for location " + loc_code)

        elif field == "max_zoom_lat":

            try:
                cur_loc.max_zoom_lat = decimal.Decimal(value)
            except:
                log.append("Invalid max_zoom_lat value '" + value +
                           "' for location " + loc_code)

        elif field == "max_zoom_long":

            try:
                cur_loc.max_zoom_long = decimal.Decimal(value)
            except:
                log.append("Invalid max_zoom_long value '" + value +
                           "' for location " + loc_code)

        elif field == "population":

            try:
                cur_loc.population = int(value)
            except:
                log.append("Invalid population value '" + value +
                           "' for location " + loc_code)

        elif field == "area":

            try:
                cur_loc.area = int(value)
            except:
                log.append("Invalid area value '" + value +
                           "' for location " + loc_code)

        elif field == "income":

            try:
                cur_loc.averageIncome = decimal.Decimal(value)
            except:
                log.append("Invalid income value '" + value +
                           "' for location " + loc_code)

        elif field == "parents":

            parents = []
            ok      = True # initially.
            for parent_code in parts[2:]:
                try:
                    parent = Location.objects.get(code=parent_code)
                except Location.DoesNotExist:
                    log.append("Invalid parent location code: " + parent_code)
                    ok = False
                    break
                parents.append(parent)

            if ok:
                cur_loc.parents.clear()
                cur_loc.parents.add(*parents)

        elif field == "children":

            children = []
            ok       = True # initially.
            for child_code in parts[2:]:
                try:
                    child = Location.objects.get(code=child_code)
                except Location.DoesNotExist:
                    log.append("Invalid child location code: " + child_code)
                    ok = False
                    break
                children.append(child)

            if ok:
                cur_loc.children.clear()
                cur_loc.children.add(*children)

        elif field == "neighbors":

            neighbours = []
            ok         = True # initially.
            for neighbour_code in parts[2:]:
                try:
                    neighbour = Location.objects.get(code=neighbour_code)
                except Location.DoesNotExist:
                    log.append("Invalid neighbour location code: " +
                               neighbour_code)
                    ok = False
                    break
                neighbours.append(neighbour)

            if ok:
                cur_loc.neighbors.clear()
                cur_loc.neighbors.add(*neighbours)

        elif field == "outline":

            wkt = "".join(parts[2:])

            try:
                outline = Outline.objects.get(location=cur_loc)
            except Outline.DoesNotExist:
                if is_new:
                    # We have to save the current location before our outline
                    # can refer to it.
                    cur_loc.save()
                outline = Outline()
                outline.location = cur_loc

            outline.outline = wkt
            outline.save()

        elif field == "name" or field == "addname":

            filter = {}
            ok     = True # initially.
            for filter_src in parts[3:]:
                if "=" not in filter_src:
                    log.append("Invalid name filter: '" + filter_src + "'")
                    ok = False
                    break

                key,value = filter_src.split("=", 1)
                key = key.strip().lower()
                value = value.strip()
                if key not in ["source", "country", "state",
                               "metro", "region", "county", "city"]:
                    log.append("Invalid name filter: '" + filter_src + "'")
                    ok = False
                    break

                try:
                    filter_loc = Location.objects.get(code=value)
                except Location.DoesNotExist:
                    log.append("The filter '" + filter_src + " refers to " + \
                               "a non-existent location.")
                    ok = False
                    break

                filter[key] = filter_loc

            if ok:
                # If this is the first time we've received a name for this
                # location, and we aren't adding to the list of names, erase
                # the existing names (if any).
                if field == "name":
                    if loc_code not in locs_with_names and not is_new:
                        for loc_name in LocationName.objects.filter(
                                                            location=cur_loc):
                            name = loc_name.name
                            loc_name.delete()

                            if name.locationname_set.count() == 0:
                                # We've removed the last occurrence of this
                                # name -> delete the name as well.
                                name.delete()

                    # Remember that we've got a name for this location.

                    locs_with_names.add(loc_code)

                # Create a new LocationName record for this name.

                if is_new:
                    # We have to save the current location before our
                    # LocationName object can refer to it.
                    cur_loc.save()

                try:
                    name = Name.objects.get(level=cur_loc.level,
                                            name=parts[2])
                except Name.DoesNotExist:
                    name = Name()
                    name.level = cur_loc.level
                    name.name  = parts[2]
                    name.save()

                loc_name = LocationName()
                loc_name.name     = name
                loc_name.location = cur_loc

                for field,filter_loc in filter.items():
                    if field == "source":
                        loc_name.sourceFilter = filter_loc
                    elif field == "country":
                        loc_name.countryFilter = filter_loc
                    elif field == "state":
                        loc_name.stateFilter = filter_loc
                    elif field == "metro":
                        loc_name.metroFilter = filter_loc
                    elif field == "region":
                        loc_name.regionFilter = filter_loc
                    elif field == "county":
                        loc_name.countyFilter = filter_loc
                    elif field == "city":
                        loc_name.cityFilter = filter_loc

                loc_name.save()

        elif field == "delete":

            log.append("Deleting locations is not supported yet.")

        else:

            log.append("Invalid field for location " + loc_code +
                       ": '" + field + "'")

    f.close()

    if cur_loc != None:
        # Save the last location to disk.
        cur_loc.save()
        if is_new:
            log.append("Added location " + cur_loc.code)
        else:
            log.append("Updated location " + cur_loc.code)