def flag(self, request, pk): try: media = Media.objects.get(pk=int(pk)) if media.status == Media.VERIFIED: return Response({"message": "Media has already been verified"}) else: if 'status_reason' in request.data.keys(): Media.flag(int(pk), request.data["status_reason"]) #Notifying Administrators try: inform_media_to_be_verified(int(pk)) except Exception as e: pass #Notifying the creator try: inform_media_rejected_or_flagged(int(pk), request.data["status_reason"], Media.FLAGGED) except Exception as e: pass return Response({"message": "Flagged!"}) else: return Response({"message": "Reason must be provided"}) except Media.DoesNotExist: return Response({"message": "No Media with the given id was found"})
def verify(self, request, pk): if request and hasattr(request, "user"): if request.user.is_authenticated: try: Media.verify(int(pk)) return Response({"message": "Verified!"}) except Media.DoesNotExist: return Response({"message": "No Media with the given id was found"}) return Response({"message", "Only Administrators can verify contributions"})
def flag(self, request, pk): try: media = Media.objects.get(pk=int(pk)) if media.status == Media.VERIFIED: return Response({"message": "Media has already been verified"}) else: if 'status_reason' in request.data.keys(): Media.flag(int(pk), request.data["status_reason"]) return Response({"message": "Flagged!"}) else: return Response({"message": "Reason must be provided"}) except Media.DoesNotExist: return Response( {"message": "No Media with the given id was found"})
def reject(self, request, pk): if request and hasattr(request, "user"): if request.user.is_authenticated: try: if 'status_reason' in request.data.keys(): Media.reject(int(pk), request.data["status_reason"]) return Response({"message": "Rejected!"}) else: return Response({"message": "Reason must be provided"}) except Media.DoesNotExist: return Response( {"message": "No Media with the given id was found"}) return Response( {"message", "Only Administrators can reject contributions"})
def reject(self, request, pk): if request and hasattr(request, "user"): if request.user.is_authenticated: try: if 'status_reason' in request.data.keys(): Media.reject(int(pk), request.data["status_reason"]) # Notifying the creator try: inform_media_rejected_or_flagged( int(pk), request.data["status_reason"], Media.REJECTED) except Exception as e: pass return Response({ "success": True, "message": "Rejected." }) else: return Response({ "success": False, "message": "Reason must be provided." }) except Media.DoesNotExist: return Response({ "success": False, "message": "No Media with the given id was found." }) return Response({ "success": False, "message": "Only Administrators can reject contributions." })
def load_arts(self): # SETUP FOR SAVING MEDIA # Files url - source from which to download media files files_url = "https://www.fp-artsmap.ca/sites/default/files/" # Delete art artists art_artists = PublicArtArtist.objects.all() art_artists.delete() # Set artsmap path - directory for media files downloaded from fp-artsmap.ca artsmap_path = "{}{}{}".format(settings.BASE_DIR, settings.MEDIA_URL, "fp-artsmap") # # COMMENT IF MEDIA IS ALREADY DOWNLOADED # # Delete fp-artsmap directory contents if it exists # if os.path.exists(artsmap_path): # files = glob.glob("{}/*".format(artsmap_path)) # for f in files: # if os.path.isfile(f): # os.remove(f) # elif os.path.isdir(f): # shutil.rmtree(f) # # Create fp-artsmap directory # if not os.path.exists(artsmap_path): # os.mkdir(artsmap_path) # # END COMMENT # SETUP FOR SAVING PLACENAMES node_placenames_geojson = self.nodes_to_geojson() node_types = [ "public_art", "artist", "organization", "event", "resource", "grant" ] # Removing every Node PlaceName object from the database. node_placenames = PlaceName.objects.filter(kind__in=node_types) node_placenames.delete() # Remove all related_data existing_related_data = RelatedData.objects.all() existing_related_data.delete() error_log = [] print('----------CREATING PLACENAMES FROM NODES----------') # Loop through each record in geojson for rec in node_placenames_geojson["features"]: with transaction.atomic(): node_placename = self.create_placename(rec) # If the record is a Public Art PlaceName, we should create the Art's Artist (also a placename) # then associate it with the Art PlaceName using the ArtArtist model # Conditions are kept relatively light to trap errors (e.g. non-existing Artist) if rec["properties"]["type"] == 'public_art' and rec.get( "artists"): for artist_id in rec.get("artists"): if artist_id: # Get the record for the associated artist artist_list = [ placename for placename in node_placenames_geojson["features"] if placename["properties"]["node_id"] == artist_id ] # Create the PlaceName for the artist - Only get first item (records may duplicate) artist_placename = self.create_placename( artist_list[0]) # Create relationship (ignore if it already exists) PublicArtArtist.objects.get_or_create( public_art=node_placename, artist=artist_placename) for data in rec["related_data"]: for value in data.get("value"): RelatedData.objects.get_or_create( data_type=data.get("key"), label=data.get("label"), is_private=data.get("private"), value=value, placename=node_placename) # Add location as related_data if rec["location_id"]: for row in self.query(""" SELECT street, city, province, postal_code, location_country.name FROM location left join location_country on country = code WHERE lid = %s; """ % rec["location_id"]): value = "" # Append street to value if it exists if row.get("street"): value += row.get("street") # Conditional data based on postal code and city if row.get("postal_code") and row.get("city"): value += "\n{} {}".format(row.get("postal_code"), row.get("city")) elif row.get("postal_code"): value += "\n{}".format(row.get("postal_code")) elif row.get("city"): value += "\n{}".format(row.get("city")) # Append province if it exists if row.get("province"): value += "\n{}".format(row.get("province")) # Append Country if it exists if row.get("name"): value += "\n{}".format(row.get("name")) # Sanitize location data if value.startswith("\n"): value = value.strip() if value.startswith(", "): value.replace(", ", "", 1) RelatedData.objects.get_or_create(data_type="location", label="Location", is_private=False, value=value, placename=node_placename) for fid in rec["files"]: for index, row in enumerate(self.query(""" SELECT file_managed.*, field_shared_image_gallery_title FROM file_managed LEFT JOIN field_data_field_shared_image_gallery ON fid = field_shared_image_gallery_fid WHERE fid = %s; """ % fid), start=1): # Extract data from query row filename = row.get("field_shared_image_gallery_title", '') uri = row["uri"] mime_type = row["filemime"] file_type = row["type"] media_path = None media_url = None if not filename: if node_placename.kind in NODES_WITH_ARTWORK: filename = "{} - {} {}".format( node_placename.name, 'Artwork', index) else: filename = "{} - {}".format( node_placename.name, index) try: existing_media = Media.objects.get( name=filename, placename=node_placename) except Media.DoesNotExist: existing_media = None if not existing_media: print('--Processing File: {}'.format(filename)) # If the video is from youtube/vimeo, only store their url # Else, download the file from fp-artsmap.ca and set the media_file from_youtube = uri.startswith("youtube://v/") from_vimeo = uri.startswith("vimeo://v/") if from_youtube or from_vimeo: if from_youtube: media_url = uri.replace( "youtube://v/", "https://youtube.com/watch?v=") elif from_vimeo: media_url = uri.replace( "vimeo://v/", "https://vimeo.com/") else: # Set up paths download_url = uri.replace("public://", files_url) storage_path = "{}/{}".format( artsmap_path, uri.replace("public://", "")) media_path = "{}/{}".format( "fp-artsmap", uri.replace("public://", "")) # # COMMENT IF MEDIA IS ALREADY DOWNLOADED # if not os.path.exists(os.path.dirname(storage_path)): # print('Creating ' + os.path.dirname(storage_path)) # os.makedirs(os.path.dirname(storage_path), exist_ok=True) # response = requests.get(download_url, allow_redirects=True) # open(storage_path, 'wb').write(response.content) # # END COMMENT # If the media is a display picture, save it in the PlaceName if fid == rec["properties"]["display_picture"]: node_placename.image = media_path node_placename.save() else: # Instantiate Media object and fill data current_media = Media() current_media.name = filename current_media.mime_type = mime_type current_media.file_type = file_type if node_placename.kind in NODES_WITH_ARTWORK: current_media.is_artwork = True else: current_media.is_artwork = False current_media.status = "VE" current_media.placename = node_placename if media_path: current_media.media_file = media_path elif media_url: current_media.url = media_url # Save Media current_media.save() print('--Done\n') print("Arts imported!")