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 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 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 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 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 test_media_deletes_broken_attachment(test_app): user_a = fixture_add_user(u"chris_a") media = fixture_media_entry(uploader=user_a.id, save=False, expunge=False) media.attachment_files.append(dict( name=u"some name", filepath=[u"does", u"not", u"exist"], )) Session.add(media) Session.flush() MediaEntry.query.get(media.id).delete() User.query.get(user_a.id).delete()
def test_media_deletes_broken_attachment(test_app): user_a = fixture_add_user(u"chris_a") media = fixture_media_entry(uploader=user_a.id, save=False) media.attachment_files.append(dict( name=u"some name", filepath=[u"does", u"not", u"exist"], )) Session.add(media) Session.flush() MediaEntry.query.get(media.id).delete() User.query.get(user_a.id).delete()
def test_add_subtitle_entry(test_app): user_a = fixture_add_user(u"test_user") media = fixture_media_entry(uploader=user_a.id, save=False, expunge=False) media.subtitle_files.append( dict( name=u"some name", filepath=[u"does", u"not", u"exist"], )) Session.add(media) Session.flush() MediaEntry.query.get(media.id).delete() User.query.get(user_a.id).delete()
def test_customize_subtitle(test_app): user_a = fixture_add_user(u"test_user") media = fixture_media_entry(uploader=user_a.id, save=False, expunge=False) media.subtitle_files.append( dict( name=u"some name", filepath=[u"does", u"not", u"exist"], )) Session.add(media) Session.flush() for subtitle in media.subtitle_files: assert '' == open_subtitle(subtitle['filepath'])[0]
def test_user_deletes_other_comments(test_app): user_a = fixture_add_user(u"chris_a") user_b = fixture_add_user(u"chris_b") media_a = fixture_media_entry(uploader=user_a.id, save=False, expunge=False, fake_upload=False) media_b = fixture_media_entry(uploader=user_b.id, save=False, expunge=False, fake_upload=False) Session.add(media_a) Session.add(media_b) Session.flush() # Create all 4 possible comments: for u in (user_a, user_b): for m in (media_a, media_b): cmt = TextComment() cmt.actor = u.id cmt.content = u"Some Comment" Session.add(cmt) # think i need this to get the command ID Session.flush() link = Comment() link.target = m link.comment = cmt Session.add(link) Session.flush() usr_cnt1 = User.query.count() med_cnt1 = MediaEntry.query.count() cmt_cnt1 = Comment.query.count() User.query.get(user_a.id).delete(commit=False) usr_cnt2 = User.query.count() med_cnt2 = MediaEntry.query.count() cmt_cnt2 = Comment.query.count() # One user deleted assert usr_cnt2 == usr_cnt1 - 1 # One media gone assert med_cnt2 == med_cnt1 - 1 # Three of four comments gone. assert cmt_cnt2 == cmt_cnt1 - 3 User.query.get(user_b.id).delete() usr_cnt2 = User.query.count() med_cnt2 = MediaEntry.query.count() cmt_cnt2 = Comment.query.count() # All users gone assert usr_cnt2 == usr_cnt1 - 2 # All media gone assert med_cnt2 == med_cnt1 - 2 # All comments gone assert cmt_cnt2 == cmt_cnt1 - 4
def media_data_init(self, **kwargs): """ Initialize or update the contents of a media entry's media_data row """ session = Session() media_data = session.query( self.media_data_table).filter_by(media_entry=self.id).first() # No media data, so actually add a new one if media_data is None: media_data = self.media_data_table(media_entry=self.id, **kwargs) session.add(media_data) # Update old media data else: for field, value in kwargs.iteritems(): setattr(media_data, field, value)
def media_data_init(self, **kwargs): """ Initialize or update the contents of a media entry's media_data row """ session = Session() media_data = session.query(self.media_data_table).filter_by( media_entry=self.id).first() # No media data, so actually add a new one if media_data is None: media_data = self.media_data_table( media_entry=self.id, **kwargs) session.add(media_data) # Update old media data else: for field, value in kwargs.iteritems(): setattr(media_data, field, value)
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 test_user_deletes_other_comments(test_app): user_a = fixture_add_user(u"chris_a") user_b = fixture_add_user(u"chris_b") media_a = fixture_media_entry(uploader=user_a.id, save=False, expunge=False, fake_upload=False) media_b = fixture_media_entry(uploader=user_b.id, save=False, expunge=False, fake_upload=False) Session.add(media_a) Session.add(media_b) Session.flush() # Create all 4 possible comments: for u_id in (user_a.id, user_b.id): for m_id in (media_a.id, media_b.id): cmt = MediaComment() cmt.media_entry = m_id cmt.author = u_id cmt.content = u"Some Comment" Session.add(cmt) Session.flush() usr_cnt1 = User.query.count() med_cnt1 = MediaEntry.query.count() cmt_cnt1 = MediaComment.query.count() User.query.get(user_a.id).delete(commit=False) usr_cnt2 = User.query.count() med_cnt2 = MediaEntry.query.count() cmt_cnt2 = MediaComment.query.count() # One user deleted assert usr_cnt2 == usr_cnt1 - 1 # One media gone assert med_cnt2 == med_cnt1 - 1 # Three of four comments gone. assert cmt_cnt2 == cmt_cnt1 - 3 User.query.get(user_b.id).delete() usr_cnt2 = User.query.count() med_cnt2 = MediaEntry.query.count() cmt_cnt2 = MediaComment.query.count() # All users gone assert usr_cnt2 == usr_cnt1 - 2 # All media gone assert med_cnt2 == med_cnt1 - 2 # All comments gone assert cmt_cnt2 == cmt_cnt1 - 4
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 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)
def save(self, obj): Session.add(obj) Session.flush()
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