1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2014 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 from mediagoblin
.db
.models
import Activity
, Generator
, User
19 def create_generator(request
):
21 This creates a Generator object based on the Client associated with the
22 OAuth credentials used. If the request has invalid OAuth credentials or
23 no OAuth credentials None is returned.
25 if not hasattr(request
, "access_token"):
28 client
= request
.access_token
.get_requesttoken
.get_client
30 # Check if there is a generator already
31 generator
= Generator
.query
.filter_by(
32 name
=client
.application_name
,
36 generator
= Generator(
37 name
=client
.application_name
,
46 def create_activity(verb
, obj
, actor
, target
=None, generator
=None):
48 This will create an Activity object which for the obj if possible
49 and save it. The verb should be one of the following:
50 add, author, create, delete, dislike, favorite, follow
51 like, post, share, unfollow, unfavorite, unlike, unshare,
54 If none of those fit you might not want/need to create an activity for
55 the object. The list is in mediagoblin.db.models.Activity.VALID_VERBS
57 # exception when we try and generate an activity with an unknow verb
58 # could change later to allow arbitrary verbs but at the moment we'll play
61 if verb
not in Activity
.VALID_VERBS
:
62 raise ValueError("A invalid verb type has been supplied.")
65 # This should exist as we're creating it by the migration for Generator
66 generator
= Generator
.query
.filter_by(name
="GNU MediaGoblin").first()
68 generator
= Generator(
69 name
="GNU MediaGoblin",
74 # Ensure the object has an ID which is needed by the activity.
75 obj
.save(commit
=False)
78 activity
= Activity(verb
=verb
)
81 if target
is not None:
82 activity
.target
= target
84 # If they've set it override the actor from the obj.
85 activity
.actor
= actor
.id if isinstance(actor
, User
) else actor
86 activity
.generator
= generator
.id
89 # Sigh want to do this prior to save but I can't figure a way to get
90 # around relationship() not looking up object when model isn't saved.
91 if activity
.generate_content():