def post_spot_images(data): """ Takes in a dict of {spot_id: [list_of_urls,]} and POSTs each in turn to the server. Returns a tuple of (spot_id, spot_image_id, original_url). """ output = [] for (spot_id, urls) in data.items(): for url in urls: img = Image(spot_id) response = img.post(url, 'dummy_description', 'cstimmel') output.append((spot_id, response['id'], url)) return output
def post_spot_images(data): """ Takes in a dict of {spot_id: [list_of_urls,]} and POSTs each in turn to the server. Returns a tuple of (spot_id, spot_image_id, original_url). """ output = [] for (spot_id, urls) in data.items(): for url in urls: img = Image(spot_id) response = img.post(url, 'dummy_description', 'cstimmel') output.append((spot_id, response['id'], url)) return output
def PUT(self, args, **kwargs): try: schema = SpotSchema().get() space_id = kwargs['space_id'] space = Space.objects.get(id=space_id) if space.is_deleted: self.error404_response() if space.spot_id: spot = Spot().get(space.spot_id) else: spot = self._spacemap.pending_spot(space, schema) Permitted().can_edit(self._request.user, space, spot) data = json.loads(self._request.read()) for field in data: if field and field.startswith("extended_info.has_"): data[field] = data[field].lower() fields, missing_fields = self._validate(spot, data) pending = json.loads(space.pending) if space.pending else {} for field in fields: if field == 'editors': try: for editor in SpaceEditor.objects.filter(space=space): editor.delete() except SpaceEditor.DoesNotExist: pass for username in fields[field].split(','): editor = username.strip() if len(editor): space_editor = SpaceEditor(editor=username.strip(),space=space) space_editor.save() else: pending[field] = fields[field] pending[field] = fields[field] if len(missing_fields) > 0: space.is_complete = None space.is_pending_publication = None pending['_missing_fields'] = missing_fields else: space.is_complete = True # SPOT-1303 if 'manager' in data: space.manager = data['manager'] if 'is_published' in data: if data.get('is_published') == True: space_images = SpaceImage.objects.filter(space=space.id) image_links = SpotImageLink.objects.filter(space=space.id, is_deleted__isnull=False) if space.is_complete and (space.pending and len(space.pending) != 0 or len(space_images) or len(image_links)): # create/update modified spot spot = self._spacemap.apply_pending(spot, space) if space.spot_id: Spot().put(spot, self._request.user) else: spot = Spot().post(spot, self._request.user) space.spot_id = spot.get('id') # fix up images, adding new, updating spot images for img in image_links: if img.is_deleted: Image(space.spot_id).delete(img.image_id) img.delete() else: Image(space.spot_id).put(img.image_id, { 'display_index' : img.display_index }, self._request.user) for img in space_images: spotimage = Image(space.spot_id).post(img.image.path, img.description, self._request.user) link = SpotImageLink(space=space, spot_id=space.spot_id, image_id=spotimage.get('id'), display_index=img.display_index) link.save() img.delete() pending = {} space.is_complete = None space.is_pending_publication = None else: # unpublish # pull spot data into space.pending # remove spot from spot server # spot().delete(spot.id) # images? pass elif 'is_pending_publication' in data: if data.get('is_pending_publication') == True: if space.is_pending_publication != True: space.is_pending_publication = True if hasattr(settings, 'SS_PUBLISHER_EMAIL'): send_mail('Space Publishing Request', 'A request has been made to publish space\n\t http%s://%s%sspace/%s/' \ % ('s' if self._request.is_secure() else '', self._request.get_host(), settings.APP_URL_ROOT, space.id), settings.SS_PUBLISHER_FROM, settings.SS_PUBLISHER_EMAIL, fail_silently=False) else: space.is_pending_publication = False space.modified_by = self._request.user.username space.pending = json.dumps(pending) if len(pending) > 0 else None space.save() return self.json_response('{"id": "%s"}' % space.id) except PermittedException: return self.error_response(401, "Unauthorized") except Space.DoesNotExist: if e.args[0]['status_code'] == 404: self.error404_response() # no return except (SpaceMapException, SpotException, SpotSchemaException) as e: return self.error_response(e.args[0]['status_code'], e.args[0]['status_text']) except Exception as ex: return self.error_response(400, "Unknown error: %s" % ex)
def PUT(self, args, **kwargs): try: schema = SpotSchema().get() space_id = kwargs['space_id'] space = Space.objects.get(id=space_id) if space.is_deleted: self.error404_response() if space.spot_id: spot = Spot().get(space.spot_id) else: spot = self._spacemap.pending_spot(space, schema) Permitted().can_edit(self._request.user, space, spot) data = json.loads(self._request.read()) for field in data: if field and field.startswith("extended_info.has_"): data[field] = data[field].lower() fields, missing_fields = self._validate(spot, data) pending = json.loads(space.pending) if space.pending else {} for field in fields: if field == 'editors': try: for editor in SpaceEditor.objects.filter(space=space): editor.delete() except SpaceEditor.DoesNotExist: pass for username in fields[field].split(','): editor = username.strip() if len(editor): space_editor = SpaceEditor(editor=username.strip(), space=space) space_editor.save() else: pending[field] = fields[field] pending[field] = fields[field] if len(missing_fields) > 0: space.is_complete = None space.is_pending_publication = None pending['_missing_fields'] = missing_fields else: space.is_complete = True # SPOT-1303 if 'manager' in data: space.manager = data['manager'] if 'is_published' in data: if data.get('is_published') == True: space_images = SpaceImage.objects.filter(space=space.id) image_links = SpotImageLink.objects.filter( space=space.id, is_deleted__isnull=False) if space.is_complete and ( space.pending and len(space.pending) != 0 or len(space_images) or len(image_links)): # create/update modified spot spot = self._spacemap.apply_pending(spot, space) if space.spot_id: Spot().put(spot, self._request.user) else: spot = Spot().post(spot, self._request.user) space.spot_id = spot.get('id') # fix up images, adding new, updating spot images for img in image_links: if img.is_deleted: Image(space.spot_id).delete(img.image_id) img.delete() else: Image(space.spot_id).put( img.image_id, {'display_index': img.display_index}, self._request.user) for img in space_images: spotimage = Image(space.spot_id).post( img.image.path, img.description, self._request.user) link = SpotImageLink( space=space, spot_id=space.spot_id, image_id=spotimage.get('id'), display_index=img.display_index) link.save() img.delete() pending = {} space.is_complete = None space.is_pending_publication = None else: # unpublish # pull spot data into space.pending # remove spot from spot server # spot().delete(spot.id) # images? pass elif 'is_pending_publication' in data: if data.get('is_pending_publication') == True: if space.is_pending_publication != True: space.is_pending_publication = True if hasattr(settings, 'SS_PUBLISHER_EMAIL'): send_mail('Space Publishing Request', 'A request has been made to publish space\n\t http%s://%s%sspace/%s/' \ % ('s' if self._request.is_secure() else '', self._request.get_host(), settings.APP_URL_ROOT, space.id), settings.SS_PUBLISHER_FROM, settings.SS_PUBLISHER_EMAIL, fail_silently=False) else: space.is_pending_publication = False space.modified_by = self._request.user.username space.pending = json.dumps(pending) if len(pending) > 0 else None space.save() return self.json_response('{"id": "%s"}' % space.id) except PermittedException: return self.error_response(401, "Unauthorized") except Space.DoesNotExist: if e.args[0]['status_code'] == 404: self.error404_response() # no return except (SpaceMapException, SpotException, SpotSchemaException) as e: return self.error_response(e.args[0]['status_code'], e.args[0]['status_text']) except Exception as ex: return self.error_response(400, "Unknown error: %s" % ex)