dimagi/commcare-hq — pull request #5167 by NoahCarnahan (2014-12-29 10:34:27+00:00)
Title: Only publish specified apps to the exchange.

There was a bug where all apps in a domain would be published to the exchange, even when their "publish" checkbox was unchecked. This bug was introduced by a fix (https://github.com/dimagi/commcare-hq/pull/5159) for a related bug that was causing no apps to be published. This original bug was introduced in #4640.

The root cause of both of these issues was this:
The app docs that were being published were being filtered by a list of doc ids (that happens [here](https://github.com/dimagi/commcare-hq/blob/31ee4beb59bcca875393f0c2ff03a5e667a81cf5/corehq/apps/domain/models.py#L677-L680)). This list of app docs was coming from the `domain/related_to_domain` couch view, and only contained the original versions of each app, not copies. However, the list of doc ids (called `copy_by_id`) contained the doc ids of the latest saved copies of the original apps, not the original app documents themselves (that happens [here](https://github.com/dimagi/commcare-hq/blob/31ee4beb59bcca875393f0c2ff03a5e667a81cf5/corehq/apps/domain/views.py#L1512), among other places).

This resulted in the apps specified by the user (that is, the latest saved versions) never matching the apps retrieved by the view (the "original" docs). This is what caused the original bug where no apps were being copied. The attempted fix ignored the filtering altogether, which resulted in all the apps being published.

It's possible that a cleaner fix to this problem would be to just use the original apps instead of their latest saved versions in the first place, but I haven't couldn't quite figured out why it was implemented this way in the first place.

FYI @emord @esoergel