def add(request): print "Adding project" form = ImageUploadForm(request.POST, request.FILES) print form if form.is_valid(): print "Form valid" p = Project() p.owner = request.user p.name = form.cleaned_data['name'] p.architect = form.cleaned_data['architect'] try: p.address = form.cleaned_data['address'] p.latitude = form.cleaned_data['latitude'] p.longitude = form.cleaned_data['longitude'] except KeyError: pass p.pub_date = timezone.now() p.image_file = form.cleaned_data['image'] print form.cleaned_data p.save() image_file = request.FILES['image'] image_str = '' for c in image_file.chunks(): image_str += c image_file_strio = StringIO(image_str) image = PImage.open(image_file_strio) if image.mode != "RGB": image = image.convert('RGB') rotation_code = get_rotation_code(image) image = rotate_image(image, rotation_code) width, height = image.size wh_ratio = float(width) / float(height) if width <= 400 and height <= 300: cropped_image = image else: if wh_ratio > 4.0/3.0: if height > 300: ratio = 300.0 / float(height) image.thumbnail((int(ceil(width * ratio)), 300), PImage.ANTIALIAS) width, height = image.size else: if width > 400: ratio = 400.0 / float(width) image.thumbnail((400, int(ceil(height * ratio))), PImage.ANTIALIAS) width, height = image.size left = max(int(width/2)-200, 0) bottom = max(int(height/2)-150, 0) right = min(int(width/2)+200, width) top = min(int(height/2)+150, height) cropped_image = image.crop([max(int(width/2)-200, 0), max(int(height/2)-150, 0), min(int(width/2)+200, width), min(int(height/2)+150, height)]) filename, ext = os.path.splitext(p.image_file.name) thumb_filename = settings.MEDIA_ROOT + filename + "-thumb" + ext #cropped_image.save(thumb_filename, "JPEG") f = StringIO() try: cropped_image.save(f, format='jpeg') s = f.getvalue() print type(p.thumbnail_file) p.thumbnail_file.save(thumb_filename, ContentFile(s)) p.save() finally: f.close() print "Project added" return HttpResponse(p.id) return HttpResponse('')
p = Project() p.owner = User.objects.get(username = "******") p.name = resource.value(RDFS.label) p.longitude = resource.value(URIRef("http://www.w3.org/2003/01/geo/wgs84_pos#long")) p.latitude = resource.value(URIRef("http://www.w3.org/2003/01/geo/wgs84_pos#lat")) p.wikipedia_page_id = resource.value(URIRef("http://dbpedia.org/ontology/wikiPageID")) # Retrieve and concatenate all architects names architects = [architect.value(RDFS.label) for architect in resource[URIRef("http://dbpedia.org/ontology/architect")]] #.encode('ascii', 'ignore')) if architects != []: p.architect = ",".join(architects) else: architect = resource.value(URIRef("http://dbpedia.org/property/architect")) if architect: if type(architect) == Resource: p.architect = architect.identifier.encode('ascii', 'ignore') else: p.architect = architect.encode('ascii', 'ignore') else: p.architect = '' # Retrieve thumb and remove "?width=300" to get main image URL if resource.value(URIRef("http://dbpedia.org/ontology/thumbnail")): thumb_url = resource.value(URIRef("http://dbpedia.org/ontology/thumbnail")).identifier i = thumb_url.rindex('?') p.wikipedia_image_url = thumb_url[:i]
if nProjects%500 == 0 and nProjects > 0: print nProjects wikipedia_page_id = result['wiki_page_id']['value'] if not Project.objects.filter(wikipedia_page_id=wikipedia_page_id).exists(): p = Project() p.owner = User.objects.get(username = "******") p.name = result[ 'stripped_structure_name']['value'] p.longitude = result['long']['value'] p.latitude = result['lat']['value'] p.wikipedia_page_id = wikipedia_page_id p.architect = '' if result.has_key('architect_prop'): p.architect = result['architect_prop']['value'] if result.has_key('stripped_architect_name'): if p.architect != '': p.architect += ', ' + result['stripped_architect_name']['value'] else: p.architect = result['stripped_architect_name']['value'] if result.has_key('thumbnail'): thumb_url = result['thumbnail']['value'] i = thumb_url.rindex('?') p.wikipedia_image_url = thumb_url[:i] ten_months = datetime.timedelta(days=300) p.pub_date = timezone.make_aware(datetime.datetime.now() - ten_months, timezone.get_current_timezone())