def pin_recording_for_user(): """ Pin a recording for user. A user token (found on https://listenbrainz.org/profile/) must be provided in the Authorization header! Each request should contain only one pinned recording item in the payload. The format of the JSON to be POSTed to this endpoint should look like the following: .. code-block:: json { "recording_msid": "40ef0ae1-5626-43eb-838f-1b34187519bf", "recording_mbid": "40ef0ae1-5626-43eb-838f-1b34187519bf", // Optional "blurb_content": "Wow..", "pinned_until": 1824001816 } :reqheader Authorization: Token <user token> :statuscode 200: feedback accepted. :statuscode 400: invalid JSON sent, see error message for details. :statuscode 401: invalid authorization. See error message for details. :resheader Content-Type: *application/json* """ user = validate_auth_header() data = request.json if "recording_msid" not in data: log_raise_400("JSON document must contain recording_msid: ", data) try: recording_to_pin = WritablePinnedRecording( user_id=user["id"], recording_msid=data["recording_msid"], recording_mbid=data["recording_mbid"] if "recording_mbid" in data else None, blurb_content=data["blurb_content"] if "blurb_content" in data else None, pinned_until=data["pinned_until"] if "pinned_until" in data else None, ) except ValidationError as e: log_raise_400( "Invalid JSON document submitted: %s" % str(e).replace("\n ", ":").replace("\n", " "), data) try: db_pinned_rec.pin(recording_to_pin) except Exception as e: current_app.logger.error( "Error while inserting pinned track record: {}".format(e)) raise APIInternalServerError("Something went wrong. Please try again.") return jsonify({"status": "ok"})
def pin_single_sample(self, user_id: int, index: int = 0) -> WritablePinnedRecording: """Inserts one recording from pinned_rec_samples into the database. Args: user_id: the row ID of the user in the DB index: the index of the element in pinned_rec_samples to insert Returns: The PinnedRecording object that was pinned """ recording_to_pin = WritablePinnedRecording( user_id=user_id, recording_msid=self.pinned_rec_samples[index]["recording_msid"], recording_mbid=self.pinned_rec_samples[index]["recording_mbid"], blurb_content=self.pinned_rec_samples[index]["blurb_content"], ) db_pinned_rec.pin(recording_to_pin) return recording_to_pin
def insert_test_data(self, user_id: int, limit: int = 4): """Inserts test data into the database. Args: user_id: the row ID of the user in the DB limit: the amount of recordings in pinned_rec_samples to insert (default = all 4) Returns: The amount of samples inserted. """ for data in self.pinned_rec_samples[:limit]: db_pinned_rec.pin( WritablePinnedRecording( user_id=user_id, recording_msid=data["recording_msid"], recording_mbid=data["recording_mbid"], blurb_content=data["blurb_content"], )) return min(limit, len(self.pinned_rec_samples))
def test_pinned_recording_with_metadata(self): recordings = [{ "title": "Strangers", "artist": "Portishead", "release": "Dummy" }, { "title": "Wicked Game", "artist": "Tom Ellis", "release": "Lucifer" }] submitted_data = msb_db.insert_all_in_transaction(recordings) msids = [x["ids"]["recording_msid"] for x in submitted_data] with ts.engine.connect() as connection: query = """ INSERT INTO mbid_mapping_metadata (recording_mbid, release_mbid, release_name, artist_credit_id, artist_mbids, artist_credit_name, recording_name) VALUES ('076255b4-1575-11ec-ac84-135bf6a670e3', '1fd178b4-1575-11ec-b98a-d72392cd8c97', 'Dummy', 65, '{6a221fda-2200-11ec-ac7d-dfa16a57158f}'::UUID[], 'Portishead', 'Strangers') """ connection.execute(sqlalchemy.text(query)) query = """INSERT INTO mbid_mapping (recording_msid, recording_mbid, match_type, last_updated) VALUES (:msid, '076255b4-1575-11ec-ac84-135bf6a670e3', 'exact_match', now())""" connection.execute(sqlalchemy.text(query), msid=msids[0]) pinned_recs = [{ "recording_msid": msids[0], "recording_mbid": "076255b4-1575-11ec-ac84-135bf6a670e3", "blurb_content": "Awesome recordings with mapped data" }, { "recording_msid": msids[1], "recording_mbid": None, "blurb_content": "Great recording but unmapped" }] for data in pinned_recs: db_pinned_rec.pin( WritablePinnedRecording( user_id=self.user["id"], recording_msid=data["recording_msid"], recording_mbid=data["recording_mbid"], blurb_content=data["blurb_content"], )) pins = db_pinned_rec.get_pin_history_for_user(self.user["id"], 5, 0) pins_with_metadata = fetch_track_metadata_for_items(pins) received = [x.dict() for x in pins_with_metadata] # pinned recs returned in reverse order of submitted because order newest to oldest self.assertEqual(received[0]["recording_msid"], pinned_recs[1]["recording_msid"]) self.assertEqual(received[0]["recording_mbid"], pinned_recs[1]["recording_mbid"]) self.assertEqual(received[0]["blurb_content"], pinned_recs[1]["blurb_content"]) self.assertEqual( received[0]["track_metadata"], { "track_name": "Wicked Game", "artist_name": "Tom Ellis", "additional_info": { "recording_msid": msids[1] } }) self.assertEqual(received[1]["recording_msid"], pinned_recs[0]["recording_msid"]) self.assertEqual(received[1]["recording_mbid"], pinned_recs[0]["recording_mbid"]) self.assertEqual(received[1]["blurb_content"], pinned_recs[0]["blurb_content"]) self.assertEqual( received[1]["track_metadata"], { "track_name": "Strangers", "artist_name": "Portishead", "release_name": "Dummy", "additional_info": { "recording_mbid": "076255b4-1575-11ec-ac84-135bf6a670e3", "release_mbid": "1fd178b4-1575-11ec-b98a-d72392cd8c97", "artist_mbids": ["6a221fda-2200-11ec-ac7d-dfa16a57158f"], "recording_msid": msids[0] } })