Last active
April 8, 2018 19:43
-
-
Save fangeugene/122278a305b617db884b209485217a6b to your computer and use it in GitHub Desktop.
Script to accept Team Media suggestions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from consts.account_permissions import AccountPermissions | |
from consts.media_type import MediaType | |
from controllers.suggestions.suggestions_review_base_controller import SuggestionsReviewBaseController | |
from google.appengine.ext import ndb | |
import json | |
from models.media import Media | |
from models.suggestion import Suggestion | |
from helpers.suggestions.media_creator import MediaCreator | |
from helpers.media_manipulator import MediaManipulator | |
from helpers.user_bundle import UserBundle | |
class AcceptSuggestionController(SuggestionsReviewBaseController): | |
def __init__(self, *args, **kw): | |
self.user_bundle = UserBundle() | |
self.preferred_keys = [] | |
self.REQUIRED_PERMISSIONS.append(AccountPermissions.REVIEW_MEDIA) | |
#super(AcceptSuggestionController, self).__init__(*args, **kw) | |
def create_target_model(self, suggestion): | |
# Setup | |
to_replace = None | |
to_replace_id = None | |
# Remove preferred reference from another Media if specified | |
team_reference = Media.create_reference( | |
suggestion.contents['reference_type'], | |
suggestion.contents['reference_key']) | |
if to_replace_id: | |
to_replace = Media.get_by_id(to_replace_id) | |
if team_reference not in to_replace.preferred_references: | |
# Preferred reference must have been edited earlier. Skip this Suggestion for now. | |
return | |
to_replace.preferred_references.remove(team_reference) | |
# Add preferred reference to current Media (images only) if explicitly listed in preferred_keys or if to_replace_id exists | |
media_type_enum = suggestion.contents['media_type_enum'] | |
preferred_references = [] | |
if media_type_enum in MediaType.image_types and ('preferred::{}'.format(suggestion.key.id()) in self.preferred_keys or to_replace_id): | |
preferred_references = [team_reference] | |
media = MediaCreator.create_media_model(suggestion, team_reference, preferred_references) | |
# Do all DB writes | |
if to_replace: | |
MediaManipulator.createOrUpdate(to_replace, auto_union=False) | |
return MediaManipulator.createOrUpdate(media) | |
def run(self): | |
suggestions = Suggestion.query().filter( | |
Suggestion.review_state == Suggestion.REVIEW_PENDING).filter( | |
Suggestion.target_model == "media").filter( | |
Suggestion.author == ndb.Key('Account', 'TODO REPLACE WITH ACCOUNT ID')).fetch(limit=1000) | |
print len(suggestions) | |
accept_keys = [] | |
for suggestion in suggestions: | |
accept_key = suggestion.key.id() | |
accept_keys.append(accept_key) | |
self.preferred_keys.append('preferred::{}'.format(accept_key)) | |
print accept_keys | |
# Process accepts | |
for accept_key in accept_keys: | |
self._process_accepted(accept_key) | |
print "DONE!" | |
asc = AcceptSuggestionController() | |
asc.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment