1 # The contents of this file are subject to the Common Public Attribution
2 # License Version 1.0. (the "License"); you may not use this file except in
3 # compliance with the License. You may obtain a copy of the License at
4 # http://code.reddit.com/LICENSE. The License is based on the Mozilla Public
5 # License Version 1.1, but Sections 14 and 15 have been added to cover use of
6 # software over a computer network and provide for limited attribution for the
7 # Original Developer. In addition, Exhibit A has been modified to be consistent
10 # Software distributed under the License is distributed on an "AS IS" basis,
11 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
12 # the specific language governing rights and limitations under the License.
14 # The Original Code is reddit.
16 # The Original Developer is the Initial Developer. The Initial Developer of
17 # the Original Code is reddit Inc.
19 # All portions of the code written by reddit are Copyright (c) 2006-2015 reddit
20 # Inc. All Rights Reserved.
21 ###############################################################################
23 from r2.lib.db.thing import NotFound
24 from r2.models import Target, Collection, CollectionStorage, Subreddit
27 def run(collection, sr):
29 sr = Subreddit._by_name(sr)
31 raise ValueError("specified subreddit does not exist")
33 collection = Collection.by_name(collection)
35 raise ValueError("specified collection does not exist")
37 srs = Target(collection).subreddits_slow
42 raise ValueError("specified subreddit is already in that collection")
44 srs.sort(key=lambda sr: sr._downs, reverse=True)
45 sr_names = [sr.name for sr in srs]
46 sr_names_column = {'sr_names':
47 CollectionStorage.SR_NAMES_DELIM.join(sr_names)}
49 CollectionStorage._set_values(collection.name, sr_names_column)
50 print "%s has been added to %s" % (sr.name, collection.name)