def featured_media_panel(request): """ This is a new administrator panel to manage featured media. This is an entirely optional panel, as there are other ways to manage it, but this way gives the admin more control. """ form = archivalook_forms.FeaturedMediaList(request.form) if request.method == 'POST' and form.validate(): featured_media = split_featured_media_list(form.box_content.data) previous_features = FeaturedMedia.query.all() for index, (media_entry, display_type) in enumerate(featured_media): target = FeaturedMedia.query.filter( FeaturedMedia.media_entry == media_entry).first() # If this media was already featured, we don't have to create a new # feature, we just have to edit the old one's values if target is not None: target.order = index target.display_type = display_type previous_features.remove(target) Session.add(target) else: new_feature = FeaturedMedia( media_entry=media_entry, display_type=display_type, order=index) Session.add(new_feature) [Session.delete(feature) for feature in previous_features] Session.commit() form.box_content.data = create_featured_media_textbox() return render_to_response( request, 'archivalook/feature.html', {'form' : form})
def import_file(self, media): try: media_type, media_manager = ( #get_media_type_and_manager(media.filename)) type_match_handler(media,media.filename)) except FileTypeNotSupported: print u"File type not supported: {0}".format(media.filename) return entry = self.db.MediaEntry() entry.media_type = unicode(media_type) entry.title = unicode( os.path.basename(os.path.splitext(media.filename)[0])) entry.uploader = 1 # Process the user's folksonomy "tags" entry.tags = convert_to_tag_list_of_dicts("") # Generate a slug from the title entry.generate_slug() task_id = unicode(uuid.uuid4()) entry.queued_media_file = media.filename.split("/") entry.queued_task_id = task_id try: entry.save() entry_id = entry.id run_process_media(entry) Session.commit() return entry_id except Exception: Session.rollback() raise
def test_media_entry_change_and_delete(test_app): """ Test that media entry additions/modification/deletes automatically show up in the index. """ media_a = fixture_media_entry(title='mediaA', save=False, expunge=False, fake_upload=False, state='processed') media_b = fixture_media_entry(title='mediaB', save=False, expunge=False, fake_upload=False, state='processed') media_a.description = 'DescriptionA' media_b.description = 'DescriptionB' Session.add(media_a) Session.add(media_b) Session.commit() # Check that the media entries are in the index engine = get_engine() assert engine.search('mediaA') == [media_a.id] assert engine.search('mediaB') == [media_b.id] # Modify one, and delete the other media_a.title = 'new' media_b.delete() # Check that the changes are present in the index assert engine.search('new') == [media_a.id] assert engine.search('mediaA') == [] assert engine.search('mediaB') == []
def featured_media_panel(request): """ This is a new administrator panel to manage featured media. This is an entirely optional panel, as there are other ways to manage it, but this way gives the admin more control. """ form = archivalook_forms.FeaturedMediaList(request.form) if request.method == 'POST' and form.validate(): featured_media = split_featured_media_list(form.box_content.data) previous_features = FeaturedMedia.query.all() for index, (media_entry, display_type) in enumerate(featured_media): target = FeaturedMedia.query.filter( FeaturedMedia.media_entry == media_entry).first() # If this media was already featured, we don't have to create a new # feature, we just have to edit the old one's values if target is not None: target.order = index target.display_type = display_type previous_features.remove(target) Session.add(target) else: new_feature = FeaturedMedia(media_entry=media_entry, display_type=display_type, order=index) Session.add(new_feature) [Session.delete(feature) for feature in previous_features] Session.commit() form.box_content.data = create_featured_media_textbox() return render_to_response(request, 'archivalook/feature.html', {'form': form})
def take_punitive_actions(request, form, report, user): message_body = "" # The bulk of this action is running through all of the different # punitive actions that a moderator could take. if u"takeaway" in form.action_to_resolve.data: for privilege_name in form.take_away_privileges.data: take_away_privileges(user.username, privilege_name) form.resolution_content.data += _(u"\n{mod} took away {user}'s {privilege} privileges.").format( mod=request.user.username, user=user.username, privilege=privilege_name ) # If the moderator elects to ban the user, a new instance of user_ban # will be created. if u"userban" in form.action_to_resolve.data: user_ban = ban_user( form.targeted_user.data, expiration_date=form.user_banned_until.data, reason=form.why_user_was_banned.data ) Session.add(user_ban) form.resolution_content.data += _(u"\n{mod} banned user {user} {expiration_date}.").format( mod=request.user.username, user=user.username, expiration_date=( _("until {date}").format(date=form.user_banned_until.data) if form.user_banned_until.data else _("indefinitely") ), ) # If the moderator elects to send a warning message. An email will be # sent to the email address given at sign up if u"sendmessage" in form.action_to_resolve.data: message_body = form.message_to_user.data form.resolution_content.data += _(u"\n{mod} sent a warning email to the {user}.").format( mod=request.user.username, user=user.username ) if u"delete" in form.action_to_resolve.data and report.is_comment_report(): deleted_comment = report.obj() Session.delete(deleted_comment) form.resolution_content.data += _(u"\n{mod} deleted the comment.").format(mod=request.user.username) elif u"delete" in form.action_to_resolve.data and report.is_media_entry_report(): deleted_media = report.obj() deleted_media.delete() form.resolution_content.data += _(u"\n{mod} deleted the media entry.").format(mod=request.user.username) report.archive(resolver_id=request.user.id, resolved=datetime.now(), result=form.resolution_content.data) Session.add(report) Session.commit() if message_body: send_email( mg_globals.app_config["email_sender_address"], [user.email], _("Warning from") + "- {moderator} ".format(moderator=request.user.username), message_body, ) return redirect(request, "mediagoblin.moderation.users_detail", user=user.username)
def clean_orphan_tags(commit=True): """Search for unused MediaTags and delete them""" q1 = Session.query(Tag).outerjoin(MediaTag).filter(MediaTag.id==None) for t in q1: Session.delete(t) # The "let the db do all the work" version: # q1 = Session.query(Tag.id).outerjoin(MediaTag).filter(MediaTag.id==None) # q2 = Session.query(Tag).filter(Tag.id.in_(q1)) # q2.delete(synchronize_session = False) if commit: Session.commit()
def remove_collection_item(collection_item, commit=True): collection = collection_item.in_collection collection.items = collection.items - 1 Session.delete(collection_item) Session.add(collection) hook_runall('collection_remove_media', collection_item=collection_item) if commit: Session.commit()
def clean_orphan_tags(commit=True): """Search for unused MediaTags and delete them""" q1 = Session.query(Tag).outerjoin(MediaTag).filter(MediaTag.id == None) for t in q1: Session.delete(t) # The "let the db do all the work" version: # q1 = Session.query(Tag.id).outerjoin(MediaTag).filter(MediaTag.id==None) # q2 = Session.query(Tag).filter(Tag.id.in_(q1)) # q2.delete(synchronize_session = False) if commit: Session.commit()
def add_media_to_collection(collection, media, note=None, commit=True): collection_item = CollectionItem() collection_item.collection = collection.id collection_item.media_entry = media.id if note: collection_item.note = note Session.add(collection_item) collection.items = collection.items + 1 Session.add(collection) Session.add(media) if commit: Session.commit()
def add_media_to_collection(collection, media, note=None, commit=True): collection_item = CollectionItem() collection_item.collection = collection.id collection_item.media_entry = media.id if note: collection_item.note = note Session.add(collection_item) collection.items = collection.items + 1 Session.add(collection) Session.add(media) if commit: Session.commit()
def add_media_to_collection(collection, media, note=None, commit=True): collection_item = CollectionItem() collection_item.collection = collection.id collection_item.get_object = media if note: collection_item.note = note Session.add(collection_item) collection.num_items = collection.num_items + 1 Session.add(collection) Session.add(media) hook_runall('collection_add_media', collection_item=collection_item) if commit: Session.commit()
def add_media_to_collection(collection, media, note=None, commit=True): collection_item = CollectionItem() collection_item.collection = collection.id collection_item.get_object = media if note: collection_item.note = note Session.add(collection_item) collection.num_items = collection.num_items + 1 Session.add(collection) Session.add(media) hook_runall('collection_add_media', collection_item=collection_item) if commit: Session.commit()
def test_unprocess_media_entry(test_app): """ Test that media entries that aren't marked as processed are not added to the index. """ dirname = pluginapi.get_config('indexedsearch').get('INDEX_DIR') media_a = fixture_media_entry(title='mediaA', save=False, expunge=False, fake_upload=False, state='unprocessed') media_a.description = 'DescriptionA' Session.add(media_a) Session.commit() # Check that the media entry is not in the index ix = whoosh.index.open_dir(dirname, indexname=INDEX_NAME) with ix.searcher() as searcher: qp = whoosh.qparser.QueryParser('title', schema=ix.schema) query = qp.parse('mediaA') assert len(searcher.search(query)) == 0
def add_to_collection(self, collection_title, entries): if not entries: return collection = (self.db.Collection.query .filter_by(creator=1, title=collection_title) .first()) if not collection: collection = self.db.Collection() collection.title = collection_title collection.creator = 1 collection.generate_slug() collection.save() Session.commit() for entry in entries: add_media_to_collection(collection, entry, commit=False) try: Session.commit() except Exception as exc: print (u"Could add media to collection {}: {}" "".format(collection_title, exc)) Session.rollback()
def test_update_index(test_app): """ Test that the update_index method: - updates any media entries whose time in the index is prior to the updated attribute of the media entry itself - ignores any media_entry whose time in the index matches the updated attribute of the media entry itself - deletes entries from the index that don't exist in the database. - adds entries that are missing from the index """ dirname = pluginapi.get_config('indexedsearch').get('INDEX_DIR') fake_time = datetime.datetime.utcnow() media_a = fixture_media_entry(title='mediaA', save=False, expunge=False, fake_upload=False, state='processed') media_b = fixture_media_entry(title='mediaB', save=False, expunge=False, fake_upload=False, state='processed') media_c = fixture_media_entry(title='mediaC', save=False, expunge=False, fake_upload=False, state='processed') media_a.description = 'DescriptionA' media_b.description = 'DescriptionB' media_c.description = 'DescriptionC' Session.add(media_a) Session.add(media_b) Session.add(media_c) Session.commit() ix = whoosh.index.open_dir(dirname, indexname=INDEX_NAME) with whoosh.writing.AsyncWriter(ix) as writer: # Mess up the index by: # - changing the time of media_a to a fake time before it was created # and changing the description # - changing the description of media_b # - adding a fake entry # - deleting an entry writer.update_document(title='{0}'.format(media_a.title), description='fake_description_a', media_id=media_a.id, time=fake_time) writer.update_document(title='{0}'.format(media_b.title), description='fake_description_b', media_id=media_b.id, time=media_b.updated) writer.update_document(title='fake document', description='fake_description_d', media_id=29, time=fake_time) writer.delete_by_term('media_id', media_c.id) engine = get_engine() engine.update_index() with engine.index.searcher() as searcher: # We changed the time in the index for media_a, so it should have # been audited. qp = whoosh.qparser.QueryParser('description', schema=engine.index.schema) query = qp.parse('fake_description_a') assert len(searcher.search(query)) == 0 query = qp.parse('DescriptionA') fields = searcher.search(query)[0] assert fields['media_id'] == media_a.id # media_b shouldn't have been audited, because we didn't change the # time, so should still have a fake description. query = qp.parse('fake_description_b') fields = searcher.search(query)[0] assert fields['media_id'] == media_b.id # media_c should have been re-added to the index query = qp.parse('DescriptionC') fields = searcher.search(query)[0] assert fields['media_id'] == media_c.id # The fake entry, media_d, should have been deleted query = qp.parse('fake_description_d') assert len(searcher.search(query)) == 0
def atomic_update(table, query_dict, update_values): table.query.filter_by(**query_dict).update(update_values, synchronize_session=False) Session.commit()
def commit(self): Session.commit()
def commit(self): Session.commit()
def atomic_update(table, query_dict, update_values): table.find(query_dict).update(update_values, synchronize_session=False) Session.commit()
def take_punitive_actions(request, form, report, user): message_body = '' # The bulk of this action is running through all of the different # punitive actions that a moderator could take. if u'takeaway' in form.action_to_resolve.data: for privilege_name in form.take_away_privileges.data: take_away_privileges(user.username, privilege_name) form.resolution_content.data += \ u"\n{mod} took away {user}\'s {privilege} privileges.".format( mod=request.user.username, user=user.username, privilege=privilege_name) # If the moderator elects to ban the user, a new instance of user_ban # will be created. if u'userban' in form.action_to_resolve.data: user_ban = ban_user(form.targeted_user.data, expiration_date=form.user_banned_until.data, reason=form.why_user_was_banned.data) Session.add(user_ban) form.resolution_content.data += \ u"\n{mod} banned user {user} {expiration_date}.".format( mod=request.user.username, user=user.username, expiration_date = ( "until {date}".format(date=form.user_banned_until.data) if form.user_banned_until.data else "indefinitely" ) ) # If the moderator elects to send a warning message. An email will be # sent to the email address given at sign up if u'sendmessage' in form.action_to_resolve.data: message_body = form.message_to_user.data form.resolution_content.data += \ u"\n{mod} sent a warning email to the {user}.".format( mod=request.user.username, user=user.username) if u'delete' in form.action_to_resolve.data and \ report.is_comment_report(): deleted_comment = report.comment Session.delete(deleted_comment) form.resolution_content.data += \ u"\n{mod} deleted the comment.".format( mod=request.user.username) elif u'delete' in form.action_to_resolve.data and \ report.is_media_entry_report(): deleted_media = report.media_entry deleted_media.delete() form.resolution_content.data += \ u"\n{mod} deleted the media entry.".format( mod=request.user.username) report.archive(resolver_id=request.user.id, resolved=datetime.now(), result=form.resolution_content.data) Session.add(report) Session.commit() if message_body: send_email( mg_globals.app_config['email_sender_address'], [user.email], _('Warning from') + '- {moderator} '.format(moderator=request.user.username), message_body) return redirect(request, 'mediagoblin.moderation.users_detail', user=user.username)