Repository: beetbox/beets

Issue:
work_date of parentwork with parentwork plugin
I currently have my beets library paths looking like this (I mostly have classical music, so I use the parentwork plugin): parent_composer_sort/work_date - parentwork/..., but if different subworks have different dates, this can store them in different directories.  Is there a way to work around this, perhaps in the path config?  Or would I need to write a plugin or edit the parentwork plugin?


Discussion:
Hello! I don't know too much about the details here—@dosoe, can you please weigh in?
Thanks for the quick response.  To expand on this, as a workaround I've edited the parentwork plugin to get a composition date for the parent work, but I'm not sure if it's ideal because not all works for which a subwork has a date have a date themselves.  Ideal might be something like being able to get the last work_date for a group of tracks with the same parentwork, but I'm not sure how possible that is, especially in the path config.
So far I considered the work date one would want would be the first to appear. In the plugin I take the work, check if it has a date, then I take its 'father', check if it has a date and so on. I keep the first date I find, as I would think it is the closest to the actual composition date and because as you said, a work having a date doesn't mean the parent work has one. 
If we decide to take the date of the parentwork (which would make sense as the work date would be coherent with the parentwork) would necessitate a change in the plugin, but a rather small one. 
Getting the last work date for several works with the same parentwork would be more complicated to implement. As for now, we just go up the work tree to get to the top, but this kind of search for the work date would necessitate a complete exploration of the tree, which is more complex and especially much longer (keep in mind there is a cap on one request per second on the MusicBrainz server) as for example for an opera the tree could contain a hundred works. 
You could then argue that for a given parentwork we would need to fetch the date just once and not each time we find a recording with this parentwork, but that would necessitate keeping in memory all past parentworks and their data to be able to check if we already have this parentwork somewhere. I'm not sure how to do that. 
Thanks for the response.  My main concern here is just getting a date for each parentwork (as long as it's the same for all the tracks in that work, and it doesn't matter too much to me if it's the first or last possible date) so I can sort them.  I'm not sure what's the best way to accomplish that, though.  What do you think?

When I mentioned getting the last (or first) work_date for a group of tracks, I didn't mean to check all the works in the tree.  I was more thinking just getting the last work_date for all the tracks in the database, not doing any musicbrainz queries.
> I was more thinking just getting the last work_date for all the tracks in the database, not doing any musicbrainz queries.

I'm a bit reluctant to use this: first it would require a lot of communication between different items (but that's doable) and second I'm afraid it could lead to the same works being attributed different years. One example is the different collections of Vivaldi concertos https://musicbrainz.org/work/7044d543-9f9d-4598-8ee7-0f9945d28595 that contains several concertos with different composition dates like https://musicbrainz.org/work/e0ba07d2-01b4-469b-a7ea-f46dc3ac71a8 and https://musicbrainz.org/work/251339db-5788-3b6f-a080-770873826cf1. A release of the "four seasons" would attribute the works the year 1723, but a release of the complete collection would use 1725 (or even later) as the composition date. You would end up with the same parentwork but different composition years, which doesn't really make sense to me (eventhough that's the case right now). 
Therefore I would tend to get the date directly from the parentwork, even if it means some tracks wouldn't have a work date. 

Resolving patch:
diff --git a/beetsplug/parentwork.py b/beetsplug/parentwork.py
--- a/beetsplug/parentwork.py
+++ b/beetsplug/parentwork.py
@@ -131,6 +131,8 @@ def get_info(self, item, work_info):
                 if artist['type'] == 'composer':
                     parent_composer.append(artist['artist']['name'])
                     parent_composer_sort.append(artist['artist']['sort-name'])
+                    if 'end' in artist.keys():
+                        parentwork_info["parentwork_date"] = artist['end']
 
             parentwork_info['parent_composer'] = u', '.join(parent_composer)
             parentwork_info['parent_composer_sort'] = u', '.join(
@@ -207,4 +209,4 @@ def find_work(self, item, force):
             item, fields=['parentwork', 'parentwork_disambig',
                           'mb_parentworkid', 'parent_composer',
                           'parent_composer_sort', 'work_date',
-                          'parentwork_workid_current'])
+                          'parentwork_workid_current', 'parentwork_date'])


Test patch: