Repository: pymedusa/Medusa

Issue:
Metadata settings not getting saved
**Describe the bug**
After changing the metadata setting on Post Processing the metadata remains default on config.ini resulting on reverting to default on restart

**To Reproduce**
Steps to reproduce the behavior:
1. Go to Post Processing -> Metadata
2. Change settings
3. Save changes
4. Check config.ini for changes on metadata_*

**Expected behavior**
Metadata settings persist after restart

**Screenshots**
![screenshot_2018-10-05 organizr v2](https://user-images.githubusercontent.com/44146/46532743-15bc9780-c89a-11e8-8ee5-6c6f1e9637d7.png)

config.ini

```
metadata_kodi = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
metadata_kodi_12plus = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
metadata_mediabrowser = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
metadata_ps3 = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
metadata_wdtv = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
metadata_tivo = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
metadata_mede8er = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
```


**Medusa:**
 - OS: Raspbian 9.4 arm
 - Python: 2.7.13
 - Branch: master
 - Commit:  4614efc77

**Additional context**
Medusa have permissions to r/w config.ini, other settings gets saved on config.ini. 
No errors found on logs



Discussion:
Im pretty sure i tested this when vueified. 
@p0psicles I reproduced it on latest develop. After a restart the selections are gone.
Also tested with [docker image](https://hub.docker.com/r/linuxserver/medusa/) with same result, the "Saved Post-Processing config" is shown but no changes on config.ini
https://github.com/pymedusa/Medusa/blob/develop/medusa/server/api/v2/config.py#L233 this should do it? 
@p0psicles What do you mean?
I mean i dont understand how this is not working?
can confirm I am seeing this exact same issue.

Resolving patch:
diff --git a/medusa/server/api/v2/base.py b/medusa/server/api/v2/base.py
--- a/medusa/server/api/v2/base.py
+++ b/medusa/server/api/v2/base.py
@@ -10,6 +10,7 @@
 import operator
 import traceback
 from builtins import object
+from collections import OrderedDict
 from concurrent.futures import ThreadPoolExecutor
 from datetime import date, datetime
 
@@ -20,7 +21,7 @@
 from medusa import app
 from medusa.logger.adapters.style import BraceAdapter
 
-from six import itervalues, string_types, text_type, viewitems
+from six import iteritems, string_types, text_type, viewitems
 
 from tornado.gen import coroutine
 from tornado.httpclient import HTTPError
@@ -583,20 +584,44 @@ def __init__(self, target, attr):
 
     def patch(self, target, value):
         """Patch the field with the specified value."""
-        map_values = {
-            'showMetadata': 'show_metadata',
-            'episodeMetadata': 'episode_metadata',
-            'episodeThumbnails': 'episode_thumbnails',
-            'seasonPosters': 'season_posters',
-            'seasonBanners': 'season_banners',
-            'seasonAllPoster': 'season_all_poster',
-            'seasonAllBanner': 'season_all_banner',
+        patches = {
+            'kodi': ListField(app, 'METADATA_KODI'),
+            'kodi_12plus': ListField(app, 'METADATA_KODI_12PLUS'),
+            'mede8er': ListField(app, 'METADATA_MEDE8ER'),
+            'mediabrowser': ListField(app, 'METADATA_MEDIABROWSER'),
+            'sony_ps3': ListField(app, 'METADATA_PS3'),
+            'tivo': ListField(app, 'METADATA_TIVO'),
+            'wdtv': ListField(app, 'METADATA_WDTV'),
         }
 
+        map_values = OrderedDict([
+            ('showMetadata', 'show_metadata'),
+            ('episodeMetadata', 'episode_metadata'),
+            ('fanart', 'fanart'),
+            ('poster', 'poster'),
+            ('banner', 'banner'),
+            ('episodeThumbnails', 'episode_thumbnails'),
+            ('seasonPosters', 'season_posters'),
+            ('seasonBanners', 'season_banners'),
+            ('seasonAllPoster', 'season_all_poster'),
+            ('seasonAllBanner', 'season_all_banner'),
+        ])
+
         try:
-            for new_provider_config in itervalues(value):
+            for name, new_provider_config in viewitems(value):
+                new_values = {}
                 for k, v in viewitems(new_provider_config):
-                    setattr(target.metadata_provider_dict[new_provider_config['name']], map_values.get(k, k), v)
+                    key = map_values.get(k)
+                    if key:
+                        # The order comes from map_values
+                        index = list(map_values).index(k)
+                        new_values[index] = int(v)
+                    setattr(target.metadata_provider_dict[new_provider_config['name']], key or k, v)
+
+                patch_field = patches.get(name)
+                if patch_field:
+                    sorted_values = [v for k, v in sorted(iteritems(new_values))]
+                    patch_field.patch(app, sorted_values)
         except Exception as error:
             log.warning('Error trying to change attribute app.metadata_provider_dict: {0!r}', error)
             return False


Test patch: