text:
#include "OgreStableHeaders.h"
#include "OgreInstanceManager.h"
#include "OgreInstanceBatchHW.h"
#include "OgreInstanceBatchHW_VTF.h"
#include "OgreInstanceBatchShader.h"
#include "OgreInstanceBatchVTF.h"
#include "OgreMesh.h"
#include "OgreSubMesh.h"
#include "OgreMeshManager.h"
#include "OgreMaterialManager.h"
#include "OgreSceneManager.h"
#include "OgreHardwareBufferManager.h"
#include "OgreSceneNode.h"
#include "OgreIteratorWrappers.h"

namespace Ogre
{
namespace v1
{
    InstanceManager::InstanceManager( IdString customName, SceneManager *sceneManager,
                                        const String &meshName, const String &groupName,
                                        InstancingTechnique instancingTechnique, uint16 instancingFlags,
                                        size_t instancesPerBatch, unsigned short subMeshIdx,
                                        bool useBoneMatrixLookup ) :
                mName( customName ),
#if OGRE_DEBUG_MODE
                mIdCount( 0 ),
#endif
                mInstancesPerBatch( instancesPerBatch ),
                mInstancingTechnique( instancingTechnique ),
                mInstancingFlags( instancingFlags ),
                mSubMeshIdx( subMeshIdx ),
                mSceneManager( sceneManager ),
                mMaxLookupTableInstances(16),
                mNumCustomParams( 0 )
    {
        mMeshReference = MeshManager::getSingleton().load( meshName, groupName );

        if(mMeshReference->sharedVertexData[VpNormal])
            MeshManager::unshareVertices(mMeshReference.get());

        if( mMeshReference->hasSkeleton() && !mMeshReference->getSkeleton().isNull() )
            mMeshReference->getSubMesh(mSubMeshIdx)->_compileBoneAssignments();
    }

    InstanceManager::~InstanceManager()
    {
        //Remove all batches from all materials we created
        InstanceBatchMap::const_iterator itor = mInstanceBatches.begin();
        InstanceBatchMap::const_iterator end  = mInstanceBatches.end();

        while( itor != end )
        {
            InstanceBatchVec::const_iterator it = itor->second.begin();
            InstanceBatchVec::const_iterator en = itor->second.end();

            while( it != en )
                OGRE_DELETE *it++;

            ++itor;
        }
    }
    //----------------------------------------------------------------------
    void InstanceManager::setInstancesPerBatch( size_t instancesPerBatch )
    {
        if( !mInstanceBatches.empty() )
        {
            OGRE_EXCEPT(Exception::ERR_INVALID_STATE, "Instances per batch can only be changed before"
                        " building the batch.", "InstanceManager::setInstancesPerBatch");
        }

        mInstancesPerBatch = instancesPerBatch;
    }

    //----------------------------------------------------------------------
    void InstanceManager::setMaxLookupTableInstances( size_t maxLookupTableInstances )
    {
        if( !mInstanceBatches.empty() )
        {
            OGRE_EXCEPT(Exception::ERR_INVALID_STATE, "Instances per batch can only be changed before"
                " building the batch.", "InstanceManager::setMaxLookupTableInstances");
        }

        mMaxLookupTableInstances = maxLookupTableInstances;
    }

    //----------------------------------------------------------------------
    void InstanceManager::setNumCustomParams( unsigned char numCustomParams )
    {
        if( !mInstanceBatches.empty() )
        {
            OGRE_EXCEPT(Exception::ERR_INVALID_STATE, "setNumCustomParams can only be changed before"
                " building the batch.", "InstanceManager::setNumCustomParams");
        }

        mNumCustomParams = numCustomParams;
    }
    //----------------------------------------------------------------------
    size_t InstanceManager::getMaxOrBestNumInstancesPerBatch( const String &materialName,
                                                                size_t suggestedSize, uint16 flags )
    {
        //Get the material
        MaterialPtr mat = MaterialManager::getSingleton().getByName( materialName,
                                                                    mMeshReference->getGroup() );
        InstanceBatch *batch = 0;

        //Base material couldn't be found
        if( mat.isNull() )
            return 0;

        switch( mInstancingTechnique )
        {
        case ShaderBased:
            batch = OGRE_NEW InstanceBatchShader( -1, &mSceneManager->_getEntityMemoryManager( SCENE_DYNAMIC ),
                                                    this, mMeshReference, mat, suggestedSize, 0 );
            break;
        case TextureVTF:
            batch = OGRE_NEW InstanceBatchVTF( -1, &mSceneManager->_getEntityMemoryManager( SCENE_DYNAMIC ), this,
                                                mMeshReference, mat, suggestedSize, 0 );
            static_cast<InstanceBatchVTF*>(batch)->setBoneDualQuaternions((mInstancingFlags & IM_USEBONEDUALQUATERNIONS) != 0);
            static_cast<InstanceBatchVTF*>(batch)->setUseOneWeight((mInstancingFlags & IM_USEONEWEIGHT) != 0);
            static_cast<InstanceBatchVTF*>(batch)->setForceOneWeight((mInstancingFlags & IM_FORCEONEWEIGHT) != 0);
            break;
        case HWInstancingBasic:
            batch = OGRE_NEW InstanceBatchHW( -1, &mSceneManager->_getEntityMemoryManager( SCENE_DYNAMIC ), this,
                                                mMeshReference, mat, suggestedSize, 0 );
            break;
        case HWInstancingVTF:
            batch = OGRE_NEW InstanceBatchHW_VTF( -1, &mSceneManager->_getEntityMemoryManager( SCENE_DYNAMIC ), this,
                                                    mMeshReference, mat, suggestedSize, 0 );
            static_cast<InstanceBatchHW_VTF*>(batch)->setBoneMatrixLookup((mInstancingFlags & IM_VTFBONEMATRIXLOOKUP) != 0, mMaxLookupTableInstances);
            static_cast<InstanceBatchHW_VTF*>(batch)->setBoneDualQuaternions((mInstancingFlags & IM_USEBONEDUALQUATERNIONS) != 0);
            static_cast<InstanceBatchHW_VTF*>(batch)->setUseOneWeight((mInstancingFlags & IM_USEONEWEIGHT) != 0);
            static_cast<InstanceBatchHW_VTF*>(batch)->setForceOneWeight((mInstancingFlags & IM_FORCEONEWEIGHT) != 0);
            break;
        default:
            OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED,
                    "Unimplemented instancing technique: " +
                    StringConverter::toString(mInstancingTechnique),
                    "InstanceBatch::getMaxOrBestNumInstancesPerBatches()");
        }

        const size_t retVal = batch->calculateMaxNumInstances( mMeshReference->getSubMesh(mSubMeshIdx),
                                                                flags );

        OGRE_DELETE batch;

        return retVal;
    }
    //----------------------------------------------------------------------
    InstancedEntity* InstanceManager::createInstancedEntity( const String &materialName,
                                                             SceneMemoryMgrTypes sceneType )
    {
        InstanceBatch *instanceBatch;

        if( mInstanceBatches.empty() )
            instanceBatch = buildNewBatch( materialName, sceneType, true );
        else
            instanceBatch = getFreeBatch( materialName, sceneType );

        return instanceBatch->createInstancedEntity();
    }
    //-----------------------------------------------------------------------
    inline InstanceBatch* InstanceManager::getFreeBatch( const String &materialName,
                                                         SceneMemoryMgrTypes sceneType )
    {
        IdString materialHash( materialName + StringConverter::toString( sceneType ) );
        InstanceBatchVec &batchVec = mInstanceBatches[materialHash];

        InstanceBatchVec::const_reverse_iterator itor = batchVec.rbegin();
        InstanceBatchVec::const_reverse_iterator end  = batchVec.rend();

        while( itor != end )
        {
            if( !(*itor)->isBatchFull() )
                return *itor;
            ++itor;
        }

        //None found, or they're all full
        return buildNewBatch( materialName, sceneType, false );
    }
    //-----------------------------------------------------------------------
    InstanceBatch* InstanceManager::buildNewBatch( const String &materialName,
                                                    SceneMemoryMgrTypes sceneType, bool firstTime )
    {
        IdString materialHashGeneric( materialName );
        IdString materialHash( materialName + StringConverter::toString( sceneType ) );

        //Get the bone to index map for the batches
        Mesh::IndexMap &idxMap = mMeshReference->getSubMesh(mSubMeshIdx)->blendIndexToBoneIndexMap;
        idxMap = idxMap.empty() ? mMeshReference->sharedBlendIndexToBoneIndexMap : idxMap;

        //Get the material
        MaterialPtr mat = MaterialManager::getSingleton().getByName( materialName,
                                                                    mMeshReference->getGroup() );

        //Get the array of batches grouped by this material
        InstanceBatchVec &materialInstanceBatch = mInstanceBatches[materialHash];

        InstanceBatch *batch = 0;

        switch( mInstancingTechnique )
        {
        case ShaderBased:
            batch = OGRE_NEW InstanceBatchShader( Id::generateNewId<InstanceBatch>(),
                                                    &mSceneManager->_getEntityMemoryManager(sceneType),
                                                    this, mMeshReference, mat, mInstancesPerBatch,
                                                    &idxMap );
            break;
        case TextureVTF:
            batch = OGRE_NEW InstanceBatchVTF( Id::generateNewId<InstanceBatch>(),
                                                    &mSceneManager->_getEntityMemoryManager(sceneType),
                                                    this, mMeshReference, mat, mInstancesPerBatch,
                                                    &idxMap );
            static_cast<InstanceBatchVTF*>(batch)->setBoneDualQuaternions((mInstancingFlags & IM_USEBONEDUALQUATERNIONS) != 0);
            static_cast<InstanceBatchVTF*>(batch)->setUseOneWeight((mInstancingFlags & IM_USEONEWEIGHT) != 0);
            static_cast<InstanceBatchVTF*>(batch)->setForceOneWeight((mInstancingFlags & IM_FORCEONEWEIGHT) != 0);
            break;
        case HWInstancingBasic:
            batch = OGRE_NEW InstanceBatchHW( Id::generateNewId<InstanceBatch>(),
                                                    &mSceneManager->_getEntityMemoryManager(sceneType),
                                                    this, mMeshReference, mat, mInstancesPerBatch,
                                                    &idxMap );
            break;
        case HWInstancingVTF:
            batch = OGRE_NEW InstanceBatchHW_VTF( Id::generateNewId<InstanceBatch>(),
                                                    &mSceneManager->_getEntityMemoryManager(sceneType),
                                                    this, mMeshReference, mat, mInstancesPerBatch,
                                                    &idxMap );
            static_cast<InstanceBatchHW_VTF*>(batch)->setBoneMatrixLookup((mInstancingFlags & IM_VTFBONEMATRIXLOOKUP) != 0, mMaxLookupTableInstances);
            static_cast<InstanceBatchHW_VTF*>(batch)->setBoneDualQuaternions((mInstancingFlags & IM_USEBONEDUALQUATERNIONS) != 0);
            static_cast<InstanceBatchHW_VTF*>(batch)->setUseOneWeight((mInstancingFlags & IM_USEONEWEIGHT) != 0);
            static_cast<InstanceBatchHW_VTF*>(batch)->setForceOneWeight((mInstancingFlags & IM_FORCEONEWEIGHT) != 0);
            break;
        default:
            OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED,
                    "Unimplemented instancing technique: " +
                    StringConverter::toString(mInstancingTechnique),
                    "InstanceBatch::buildNewBatch()");
        }

#if OGRE_DEBUG_MODE
        batch->setName( mName.getFriendlyText() + "/InstanceBatch_" +
                            StringConverter::toString(mIdCount++) );
#endif

        batch->_notifyManager( mSceneManager );

        if( !firstTime )
        {
            //TODO: Check different materials have the same mInstancesPerBatch upper limit
            //otherwise we can't share
            batch->buildFrom( mMeshReference->getSubMesh(mSubMeshIdx), mSharedRenderOperation );
        }
        else
        {
            //Ensure we don't request more than we can
            const size_t maxInstPerBatch = batch->calculateMaxNumInstances( mMeshReference->
                                                        getSubMesh(mSubMeshIdx), mInstancingFlags );
            mInstancesPerBatch = std::min( maxInstPerBatch, mInstancesPerBatch );
            batch->_setInstancesPerBatch( mInstancesPerBatch );

            //TODO: Create a "merge" function that merges all submeshes into one big submesh
            //instead of just sending submesh #0

            //Get the RenderOperation to be shared with further instances.
            mSharedRenderOperation = batch->build( mMeshReference->getSubMesh(mSubMeshIdx) );
        }

        const BatchSettings &batchSettings = mBatchSettings[materialHashGeneric];
        batch->setCastShadows( batchSettings.setting[CAST_SHADOWS] );

        batch->setStatic( sceneType == SCENE_STATIC );

        materialInstanceBatch.push_back( batch );

        return batch;
    }
    //-----------------------------------------------------------------------
    void InstanceManager::cleanupEmptyBatches(void)
    {
        //Do this now to avoid any dangling pointer inside mDirtyBatches
        _updateDirtyBatches();

        InstanceBatchMap::iterator itor = mInstanceBatches.begin();
        InstanceBatchMap::iterator end  = mInstanceBatches.end();

        while( itor != end )
        {
            InstanceBatchVec::iterator it = itor->second.begin();
            InstanceBatchVec::iterator en = itor->second.end();

            while( it != en )
            {
                if( (*it)->isBatchUnused() )
                {
                    OGRE_DELETE *it;
                    //Remove it from the list swapping with the last element and popping back
                    size_t idx = it - itor->second.begin();
                    *it = itor->second.back();
                    itor->second.pop_back();

                    //Restore invalidated iterators
                    it = itor->second.begin() + idx;
                    en = itor->second.end();
                }
                else
                    ++it;
            }

            ++itor;
        }

        //By this point it may happen that all mInstanceBatches' objects are also empty
        //however if we call mInstanceBatches.clear(), next time we'll create an InstancedObject
        //we'll end up calling buildFirstTime() instead of buildNewBatch(), which is not the idea
        //(takes more time and will leak the shared render operation)
    }
    //-----------------------------------------------------------------------
    void InstanceManager::defragmentBatches( bool optimizeCull,
                                                InstanceBatch::InstancedEntityVec &usedEntities,
                                                InstanceBatch::CustomParamsVec &usedParams,
                                                InstanceBatchVec &fragmentedBatches )
    {
        InstanceBatchVec::iterator itor = fragmentedBatches.begin();
        InstanceBatchVec::iterator end  = fragmentedBatches.end();

        while( itor != end && !usedEntities.empty() )
        {
            if( !(*itor)->isStatic() )
                (*itor)->_defragmentBatch( optimizeCull, usedEntities, usedParams );
            ++itor;
        }

        InstanceBatchVec::iterator lastImportantBatch = itor;

        while( itor != end )
        {
            if( !(*itor)->isStatic() )
            {
                //If we get here, this means we hit remaining batches which will be unused.
                //Destroy them
                //Call this to avoid freeing InstancedEntities that were just reparented
                (*itor)->_defragmentBatchDiscard();
                OGRE_DELETE *itor;
            }
            else
            {
                //This isn't a meaningless batch, move it forward so it doesn't get wipe
                //when we resize the container (faster than removing element by element)
                *lastImportantBatch++ = *itor;
            }

            ++itor;
        }

        //Remove remaining batches all at once from the vector
        const size_t remainingBatches = end - lastImportantBatch;
        fragmentedBatches.resize( fragmentedBatches.size() - remainingBatches );
    }
    //-----------------------------------------------------------------------
    void InstanceManager::defragmentBatches( bool optimizeCulling )
    {
        //Do this now to avoid any dangling pointer inside mDirtyBatches
        _updateDirtyBatches();

        //Do this for every material
        InstanceBatchMap::iterator itor = mInstanceBatches.begin();
        InstanceBatchMap::iterator end  = mInstanceBatches.end();

        while( itor != end )
        {
            InstanceBatch::InstancedEntityVec   usedEntities;
            InstanceBatch::CustomParamsVec      usedParams;
            usedEntities.reserve( itor->second.size() * mInstancesPerBatch );

            //Collect all Instanced Entities being used by _all_ batches from this material
            InstanceBatchVec::iterator it = itor->second.begin();
            InstanceBatchVec::iterator en = itor->second.end();

            while( it != en )
            {
                //Don't collect instances from static batches, we assume they're correctly set
                //Plus, we don't want to put InstancedEntities from non-static into static batches
                if( !(*it)->isStatic() )
                    (*it)->getInstancedEntitiesInUse( usedEntities, usedParams );
                ++it;
            }

            defragmentBatches( optimizeCulling, usedEntities, usedParams, itor->second );

            ++itor;
        }
    }
    //-----------------------------------------------------------------------
    void InstanceManager::setSetting( BatchSettingId id, bool value, IdString materialName )
    {
        assert( id < NUM_SETTINGS );

        if( materialName == IdString() )
        {
            //Setup all existing materials
            InstanceBatchMap::iterator itor = mInstanceBatches.begin();
            InstanceBatchMap::iterator end  = mInstanceBatches.end();

            while( itor != end )
            {
                mBatchSettings[itor->first].setting[id] = value;
                applySettingToBatches( id, value, itor->second );

                ++itor;
            }
        }
        else
        {
            //Setup a given material
            mBatchSettings[materialName].setting[id] = value;

            InstanceBatchMap::const_iterator itor = mInstanceBatches.find( materialName );
            //Don't crash or throw if the batch with that material hasn't been created yet
            if( itor != mInstanceBatches.end() )
                applySettingToBatches( id, value, itor->second );
        }
    }
    //-----------------------------------------------------------------------
    bool InstanceManager::getSetting( BatchSettingId id, IdString materialName ) const
    {
        assert( id < NUM_SETTINGS );

        BatchSettingsMap::const_iterator itor = mBatchSettings.find( materialName );
        if( itor != mBatchSettings.end() )
            return itor->second.setting[id]; //Return current setting

        //Return default
        return BatchSettings().setting[id];
    }
    //-----------------------------------------------------------------------
    void InstanceManager::applySettingToBatches( BatchSettingId id, bool value,
                                                 const InstanceBatchVec &container )
    {
        InstanceBatchVec::const_iterator itor = container.begin();
        InstanceBatchVec::const_iterator end  = container.end();

        while( itor != end )
        {
            switch( id )
            {
            case CAST_SHADOWS:
                (*itor)->setCastShadows( value );
                break;
            case SHOW_BOUNDINGBOX:
                //(*itor)->getParentSceneNode()->showBoundingBox( value );
                break;
            default:
                break;
            }
            ++itor;
        }
    }
    //-----------------------------------------------------------------------
    void InstanceManager::setBatchesAsStatic( bool bStatic )
    {
        InstanceBatchMap::iterator itor = mInstanceBatches.begin();
        InstanceBatchMap::iterator end  = mInstanceBatches.end();

        while( itor != end )
        {
            InstanceBatchVec::iterator it = itor->second.begin();
            InstanceBatchVec::iterator en = itor->second.end();

            while( it != en )
            {
                (*it)->setStatic( bStatic );
                ++it;
            }

            ++itor;
        }
    }
    //-----------------------------------------------------------------------
    void InstanceManager::_addToDynamicBatchList( InstanceBatch *dynamicBatch )
    {
        InstanceBatchVec::iterator itor = std::find( mDynamicBatches.begin(), mDynamicBatches.end(),
                                                     dynamicBatch );
        if( itor == mDynamicBatches.end() )
            mDynamicBatches.push_back( dynamicBatch );
    }
    //-----------------------------------------------------------------------
    void InstanceManager::_removeFromDynamicBatchList( InstanceBatch *batch )
    {
        InstanceBatchVec::iterator itor = std::find( mDynamicBatches.begin(), mDynamicBatches.end(),
                                                     batch );
        if( itor != mDynamicBatches.end() )
            efficientVectorRemove( mDynamicBatches, itor );
    }
    //-----------------------------------------------------------------------
    void InstanceManager::_addDirtyStaticBatch( InstanceBatch *dirtyBatch )
    {
        //If he needs to this very often, they're probably not static...
        //Note: Calling this more often will only affect performance for the next frame.
        //It won't crash and can be ignored
        assert( std::find( mDirtyStaticBatches.begin(), mDirtyStaticBatches.end(), dirtyBatch )
                == mDirtyStaticBatches.end() && "Only flag as dirty static batches once!" );

        mDirtyStaticBatches.push_back( dirtyBatch );
    }
    //-----------------------------------------------------------------------
#ifdef OGRE_LEGACY_ANIMATIONS
    void InstanceManager::_updateAnimations(void)
    {
        InstanceBatchVec::const_iterator itor = mDynamicBatches.begin();
        InstanceBatchVec::const_iterator end  = mDynamicBatches.end();

        while( itor != end )
        {
            (*itor)->_updateAnimations();
            ++itor;
        }

        itor = mDirtyStaticBatches.begin();
        end  = mDirtyStaticBatches.end();

        while( itor != end )
        {
            (*itor)->_updateAnimations();
            ++itor;
        }

        //_updateDirtyBatches will be called after us, and will do that job.
        //mDirtyStaticBatches.clear();
    }
#endif
    //-----------------------------------------------------------------------
    void InstanceManager::_updateDirtyBatchesThread( size_t threadIdx )
    {
        InstanceBatchVec::const_iterator itor = mDynamicBatches.begin();
        InstanceBatchVec::const_iterator end  = mDynamicBatches.end();

        while( itor != end )
        {
            (*itor)->_updateEntitiesBoundsThread( threadIdx );
            ++itor;
        }

        itor = mDirtyStaticBatches.begin();
        end  = mDirtyStaticBatches.end();

        while( itor != end )
        {
            (*itor)->_updateEntitiesBoundsThread( threadIdx );
            ++itor;
        }
    }
    //-----------------------------------------------------------------------
    void InstanceManager::_updateDirtyBatches(void)
    {
        InstanceBatchVec::const_iterator itor = mDynamicBatches.begin();
        InstanceBatchVec::const_iterator end  = mDynamicBatches.end();

        while( itor != end )
        {
            (*itor)->_updateBounds();
            ++itor;
        }

        itor = mDirtyStaticBatches.begin();
        end  = mDirtyStaticBatches.end();

        while( itor != end )
        {
            (*itor)->_updateBounds();
            ++itor;
        }

        mDirtyStaticBatches.clear();
    }
    //-----------------------------------------------------------------------
    InstanceManager::InstanceBatchIterator InstanceManager::getInstanceBatchIterator(
                                        const String &materialName, SceneMemoryMgrTypes sceneType ) const
    {
        IdString materialHash( materialName + StringConverter::toString( sceneType ) );
        InstanceBatchMap::const_iterator it = mInstanceBatches.find( materialHash );
        if( it == mInstanceBatches.end() )
        {
            OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
                        "Can't find instance batches with material '" + materialName + "'",
                        "InstanceManager::getInstanceBatchIterator");
        }
        return InstanceBatchIterator( it->second.begin(), it->second.end() );
    }
}
}


text:
import 'babel-polyfill';
import Vue from 'vue';
import VueRouter from 'vue-router';
import VueHead from 'vue-head';
import Vuikit from 'vuikit';
import UIkit from 'uikit';
import Icons from 'uikit/dist/js/uikit-icons';
import VueHighlightJS from 'vue-highlight.js';

Vue.use(VueHead, {
  separator: '-',
  complement: 'Huehuetenango',
});
Vue.use(VueRouter);
Vue.use(Vuikit);
UIkit.use(Icons);
Vue.use(VueHighlightJS);

redpajama_set_name: RedPajamaGithub


text:
/*
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
/*
 * This code was generated by https://github.com/googleapis/google-api-java-client-services/
 * Modify at your own risk.
 */

package com.google.api.services.displayvideo.v1.model;

/**
 * The partner or advertisers with access to the inventory source.
 *
 *
 This is the Java data model class that specifies how to parse/serialize into the JSON that is
 * transmitted over HTTP when working with the Display & Video 360 API. For a detailed explanation
 * see:
 * https://developers.google.com/api-client-library/java/google-http-java-client/json
 *

 *
 * @author Google, Inc.
 */
@SuppressWarnings("javadoc")
public final class InventorySourceAccessors extends com.google.api.client.json.GenericJson {

  /**
   * The advertisers with access to the inventory source. All advertisers must belong to the same
   * partner.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private InventorySourceAccessorsAdvertiserAccessors advertisers;

  /**
   * The partner with access to the inventory source.
   * The value may be {@code null}.
   */
  @com.google.api.client.util.Key
  private InventorySourceAccessorsPartnerAccessor partner;

  /**
   * The advertisers with access to the inventory source. All advertisers must belong to the same
   * partner.
   * @return value or {@code null} for none
   */
  public InventorySourceAccessorsAdvertiserAccessors getAdvertisers() {
    return advertisers;
  }

  /**
   * The advertisers with access to the inventory source. All advertisers must belong to the same
   * partner.
   * @param advertisers advertisers or {@code null} for none
   */
  public InventorySourceAccessors setAdvertisers(InventorySourceAccessorsAdvertiserAccessors advertisers) {
    this.advertisers = advertisers;
    return this;
  }

  /**
   * The partner with access to the inventory source.
   * @return value or {@code null} for none
   */
  public InventorySourceAccessorsPartnerAccessor getPartner() {
    return partner;
  }

  /**
   * The partner with access to the inventory source.
   * @param partner partner or {@code null} for none
   */
  public InventorySourceAccessors setPartner(InventorySourceAccessorsPartnerAccessor partner) {
    this.partner = partner;
    return this;
  }

  @Override
  public InventorySourceAccessors set(String fieldName, Object value) {
    return (InventorySourceAccessors) super.set(fieldName, value);
  }

  @Override
  public InventorySourceAccessors clone() {
    return (InventorySourceAccessors) super.clone();
  }

}


text:
<?php

namespace NilPortugues\Laravel5\JsonApi;

use Illuminate\Support\ServiceProvider;
use NilPortugues\Laravel5\JsonApi\Providers\Laravel51Provider;
use NilPortugues\Laravel5\JsonApi\Providers\Laravel52Provider;

class Laravel5JsonApiServiceProvider extends ServiceProvider
{
    const LARAVEL_APPLICATION = 'Illuminate\Foundation\Application';
    const PATH = '/../../../config/jsonapi.php';

    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = false;

    /**
     * Bootstrap the application events.
     */
    public function boot()
    {
        $this->publishes([__DIR__.self::PATH => config('jsonapi.php')]);
    }

    /**
     * Register the service provider.
     */
    public function register()
    {
        $this->mergeConfigFrom(__DIR__.self::PATH, 'jsonapi');

        $version = '5.0.0';
        if (class_exists(self::LARAVEL_APPLICATION, true)) {
            $class = self::LARAVEL_APPLICATION;
            $version = $class::VERSION;
        }

        switch ($version) {
            case false !== strpos($version, '5.0.'):
            case false !== strpos($version, '5.1.'):
                $provider = new Laravel51Provider();
                break;
            case false !== strpos($version, '5.2.'):
                $provider = new Laravel52Provider();
                break;
            default:
                throw new \RuntimeException(
                    sprintf('Laravel version %s is not supported. Please use the 5.1 for the time being', $version)
                );
                break;
        }

        $this->app->singleton(JsonApiSerializer::class, $provider->provider());

        $this->publishes([__DIR__.'/../../../config/jsonapi.php' => base_path('config')]);
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return ['jsonapi'];
    }
}


text:
from __future__ import unicode_literals

from moto.acm import acm_backends
from moto.apigateway import apigateway_backends
from moto.autoscaling import autoscaling_backends
from moto.awslambda import lambda_backends
from moto.cloudformation import cloudformation_backends
from moto.cloudwatch import cloudwatch_backends
from moto.cognitoidentity import cognitoidentity_backends
from moto.cognitoidp import cognitoidp_backends
from moto.core import moto_api_backends
from moto.datapipeline import datapipeline_backends
from moto.dynamodb import dynamodb_backends
from moto.dynamodb2 import dynamodb_backends2
from moto.ec2 import ec2_backends
from moto.ecr import ecr_backends
from moto.ecs import ecs_backends
from moto.elb import elb_backends
from moto.elbv2 import elbv2_backends
from moto.emr import emr_backends
from moto.events import events_backends
from moto.glacier import glacier_backends
from moto.iam import iam_backends
from moto.instance_metadata import instance_metadata_backends
from moto.kinesis import kinesis_backends
from moto.kms import kms_backends
from moto.logs import logs_backends
from moto.opsworks import opsworks_backends
from moto.polly import polly_backends
from moto.rds2 import rds2_backends
from moto.redshift import redshift_backends
from moto.route53 import route53_backends
from moto.s3 import s3_backends
from moto.ses import ses_backends
from moto.secretsmanager import secretsmanager_backends
from moto.sns import sns_backends
from moto.sqs import sqs_backends
from moto.ssm import ssm_backends
from moto.sts import sts_backends
from moto.swf import swf_backends
from moto.xray import xray_backends
from moto.iot import iot_backends
from moto.iotdata import iotdata_backends
from moto.batch import batch_backends
from moto.resourcegroupstaggingapi import resourcegroupstaggingapi_backends

BACKENDS = {
    'acm': acm_backends,
    'apigateway': apigateway_backends,
    'autoscaling': autoscaling_backends,
    'batch': batch_backends,
    'cloudformation': cloudformation_backends,
    'cloudwatch': cloudwatch_backends,
    'cognito-identity': cognitoidentity_backends,
    'cognito-idp': cognitoidp_backends,
    'datapipeline': datapipeline_backends,
    'dynamodb': dynamodb_backends,
    'dynamodb2': dynamodb_backends2,
    'ec2': ec2_backends,
    'ecr': ecr_backends,
    'ecs': ecs_backends,
    'elb': elb_backends,
    'elbv2': elbv2_backends,
    'events': events_backends,
    'emr': emr_backends,
    'glacier': glacier_backends,
    'iam': iam_backends,
    'moto_api': moto_api_backends,
    'instance_metadata': instance_metadata_backends,
    'logs': logs_backends,
    'kinesis': kinesis_backends,
    'kms': kms_backends,
    'opsworks': opsworks_backends,
    'polly': polly_backends,
    'redshift': redshift_backends,
    'rds': rds2_backends,
    's3': s3_backends,
    's3bucket_path': s3_backends,
    'ses': ses_backends,
    'secretsmanager': secretsmanager_backends,
    'sns': sns_backends,
    'sqs': sqs_backends,
    'ssm': ssm_backends,
    'sts': sts_backends,
    'swf': swf_backends,
    'route53': route53_backends,
    'lambda': lambda_backends,
    'xray': xray_backends,
    'resourcegroupstaggingapi': resourcegroupstaggingapi_backends,
    'iot': iot_backends,
    'iot-data': iotdata_backends,
}

def get_model(name, region_name):
    for backends in BACKENDS.values():
        for region, backend in backends.items():
            if region == region_name:
                models = getattr(backend.__class__, '__models__', {})
                if name in models:
                    return list(getattr(backend, models[name])())


text:
from libqtile.log_utils import logger
from . import base
import six

class Net(base.ThreadedPollText):

    """
        Displays interface down and up speed.
    """
    orientations = base.ORIENTATION_HORIZONTAL
    defaults = [
        ('interface', 'wlan0', 'The interface to monitor'),
        ('update_interval', 1, 'The update interval.'),
    ]

    def __init__(self, **config):
        base.ThreadedPollText.__init__(self, **config)
        self.add_defaults(Net.defaults)
        self.interfaces = self.get_stats()

    def convert_b(self, b):
        # Here we round to 1000 instead of 1024
        # because of round things
        letter = 'B'
        if b // 1000 > 0:
            b /= 1000.0
            letter = 'k'
        if b // 1000 > 0:
            b /= 1000.0
            letter = 'M'
        if b // 1000 > 0:
            b /= 1000.0
            letter = 'G'
        # I hope no one have more than 999 GB/s
        return b, letter

    def get_stats(self):
        lines = []  # type: List[str]
        with open('/proc/net/dev', 'r') as f:
            lines = f.readlines()[2:]
        interfaces = {}
        for s in lines:
            int_s = s.split()
            name = int_s[0][:-1]
            down = float(int_s[1])
            up = float(int_s[-8])
            interfaces[name] = {'down': down, 'up': up}
        return interfaces

    def _format(self, down, up):
        down = "%0.2f" % down
        up = "%0.2f" % up
        if len(down) > 5:
            down = down[:5]
        if len(up) > 5:
            up = up[:5]

        down = "  " * (5 - len(down)) + down
        up = "  " * (5 - len(up)) + up
        return down, up

    def poll(self):
        try:
            new_int = self.get_stats()
            down = new_int[self.interface]['down'] - \
                self.interfaces[self.interface]['down']
            up = new_int[self.interface]['up'] - \
                self.interfaces[self.interface]['up']

            down = down / self.update_interval
            up = up / self.update_interval
            down, down_letter = self.convert_b(down)
            up, up_letter = self.convert_b(up)

            down, up = self._format(down, up)

            str_base = six.u("%s%s \u2193\u2191 %s%s")

            self.interfaces = new_int
            return str_base % (down, down_letter, up, up_letter)
        except Exception as e:
            logger.error('%s: Probably your wlan device is switched off or otherwise not present in your system.',
                    self.__class__.__name__, str(e))


text:
copy LICENSE bin
..\NuGet.exe pack oauth.nuspec -b bin -o bin


text:
/*
	Use of this source code is governed by a MIT license that can be found in the LICENSE file.
	by Giorgis (habtom@giorgis.io)

	The process of changing the sampling rate of a discrete signal to obtain
	a new discrete representation of the underlying continuous signal.
	resampler provides a high-level interface to the libswresample library audio resampling utilities
*/
package swresample

/*
	#cgo pkg-config: libswresample
	#include <libswresample/swresample.h>
*/
import "C"
import (
	"unsafe"
)

type (
	SwrContext     C.struct_SwrContext
	AVFrame        C.struct_AVFrame
	AVClass        C.struct_AVClass
	AVSampleFormat C.enum_AVSampleFormat
)

//const AVClass * swr_get_class (void)
//Get the AVClass for SwrContext.
func Swr_get_class() *AVClass {
	return (*AVClass)(C.swr_get_class())
}

//SwrContext constructor functions
//struct SwrContext * swr_alloc (void)
//Allocate SwrContext.
func Swr_alloc() *SwrContext {
	return (*SwrContext)(C.swr_alloc())
}

//int swr_init (struct SwrContext *s)
//Initialize context after user parameters have been set.
func Swr_init(s *SwrContext) int {
	return int(C.swr_init((*C.struct_SwrContext)(s)))
}

//int swr_is_initialized (struct SwrContext *s)
//Check whether an swr context has been initialized or not.
func Swr_is_initialized(s *SwrContext) int {
	return int(C.swr_is_initialized((*C.struct_SwrContext)(s)))
}

//struct SwrContext * swr_alloc_set_opts (struct SwrContext *s, int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate, int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate, int log_offset, void *log_ctx)
//Allocate SwrContext if needed and set/reset common parameters.
func Swr_alloc_set_opts(s *SwrContext, ocl int64, osf AVSampleFormat, osr int, icl int64, isf AVSampleFormat, isr, lo, lc int) *SwrContext {
	return (*SwrContext)(C.swr_alloc_set_opts((*C.struct_SwrContext)(s), C.int64_t(ocl), (C.enum_AVSampleFormat)(osf), C.int(osr), C.int64_t(icl), (C.enum_AVSampleFormat)(isf), C.int(isr), C.int(lo), unsafe.Pointer(&lc)))
}

//SwrContext destructor functions
//void swr_free (struct SwrContext **s)
//Free the given SwrContext and set the pointer to NULL.
func Swr_free(s **SwrContext) {
	C.swr_free((**C.struct_SwrContext)(unsafe.Pointer(s)))
}

//void swr_close (struct SwrContext *s)
//Closes the context so that swr_is_initialized() returns 0.
func Swr_close(s *SwrContext) {
	C.swr_close((*C.struct_SwrContext)(s))
}

//Core conversion functions
//int swr_convert (struct SwrContext *s, uint8_t **out, int out_count, const uint8_t **in, int in_count)
//Convert audio.
func Swr_convert(s *SwrContext, out **uint8, oc int, in **uint8, ic int) int {
	return int(C.swr_convert((*C.struct_SwrContext)(s), (**C.uint8_t)(unsafe.Pointer(out)), C.int(oc), (**C.uint8_t)(unsafe.Pointer(in)), C.int(ic)))
}

//int64_t swr_next_pts (struct SwrContext *s, int64_t pts)
//Convert the next timestamp from input to output timestamps are in 1/(in_sample_rate * out_sample_rate) units.
func Swr_next_pts(s *SwrContext, pts int64) int64 {
	return int64(C.swr_next_pts((*C.struct_SwrContext)(s), C.int64_t(pts)))
}

//Low-level option setting functions
//These functons provide a means to set low-level options that is not possible with the AVOption API.
//int swr_set_compensation (struct SwrContext *s, int sample_delta, int compensation_distance)
//Activate resampling compensation ("soft" compensation).
func Swr_set_compensation(s *SwrContext, sd, cd int) int {
	return int(C.swr_set_compensation((*C.struct_SwrContext)(s), C.int(sd), C.int(cd)))
}

//int swr_set_channel_mapping (struct SwrContext *s, const int *channel_map)
//Set a customized input channel mapping.
func Swr_set_channel_mapping(s *SwrContext, cm *int) int {
	return int(C.swr_set_channel_mapping((*C.struct_SwrContext)(s), (*C.int)(unsafe.Pointer(cm))))
}

//int swr_set_matrix (struct SwrContext *s, const double *matrix, int stride)
//Set a customized remix matrix.
func Swr_set_matrix(s *SwrContext, m *int, t int) int {
	return int(C.swr_set_matrix((*C.struct_SwrContext)(s), (*C.double)(unsafe.Pointer(m)), C.int(t)))
}

//Sample handling functions
//int swr_drop_output (struct SwrContext *s, int count)
//Drops the specified number of output samples.
func Swr_drop_output(s *SwrContext, c int) int {
	return int(C.swr_drop_output((*C.struct_SwrContext)(s), C.int(c)))
}

//int swr_inject_silence (struct SwrContext *s, int count)
//Injects the specified number of silence samples.
func Swr_inject_silence(s *SwrContext, c int) int {
	return int(C.swr_inject_silence((*C.struct_SwrContext)(s), C.int(c)))
}

//int64_t swr_get_delay (struct SwrContext *s, int64_t base)
//Gets the delay the next input sample will experience relative to the next output sample.
func Swr_get_delay(s *SwrContext, b int64) int64 {
	return int64(C.swr_get_delay((*C.struct_SwrContext)(s), C.int64_t(b)))
}

//Configuration accessors
//unsigned swresample_version (void)
//Return the LIBSWRESAMPLE_VERSION_INT constant.
func Swresample_version() uint {
	return uint(C.swresample_version())
}

//const char * swresample_configuration (void)
//Return the swr build-time configuration.
func Swresample_configuration() string {
	return C.GoString(C.swresample_configuration())
}

//const char * swresample_license (void)
//Return the swr license.
func Swresample_license() string {
	return C.GoString(C.swresample_license())
}

//AVFrame based API
//int swr_convert_frame (SwrContext *swr, AVFrame *output, const AVFrame *input)
//Convert the samples in the input AVFrame and write them to the output AVFrame.
func Swr_convert_frame(s *SwrContext, o, i *AVFrame) int {
	return int(C.swr_convert_frame((*C.struct_SwrContext)(s), (*C.struct_AVFrame)(o), (*C.struct_AVFrame)(i)))
}

//int swr_config_frame (SwrContext *swr, const AVFrame *out, const AVFrame *in)
//Configure or reconfigure the SwrContext using the information provided by the AVFrames.
func Swr_config_frame(s *SwrContext, o, i *AVFrame) int {
	return int(C.swr_config_frame((*C.struct_SwrContext)(s), (*C.struct_AVFrame)(o), (*C.struct_AVFrame)(i)))
}


text:
from django.urls import reverse
from django.db.models import Count
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from django.views.generic.list import ListView
from django.http import StreamingHttpResponse, Http404, HttpResponseRedirect, JsonResponse, HttpResponse
from django.views.generic import TemplateView
from django.core.exceptions import PermissionDenied
from django.views.generic.base import RedirectView, View
from django.shortcuts import get_object_or_404, render
from django.apps import apps
from django.core.paginator import Paginator
from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin
from django.db.models import Q
from braces.views import LoginRequiredMixin
from allauth.socialaccount.models import SocialApp
from django_datatables_view.base_datatable_view import BaseDatatableView
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.utils.html import mark_safe
from django.conf import settings

from .notifications import get_free_space, get_queue_data, is_shared
from .forms import CollectionSetForm, ExportForm
from . import forms
from .models import CollectionSet, Collection, Seed, Credential, Harvest, Export, User, Warc
from .sched import next_run_time
from .utils import CollectionHistoryIter, clean_token, clean_blogname, diff_historical_object
from .monitoring import monitor_harvests, monitor_queues, monitor_exports
from .auth import CollectionSetOrSuperuserPermissionMixin, \
    check_collection_set_based_permission, UserOrSuperuserOrStaffPermissionMixin, UserOrSuperuserPermissionMixin, \
    has_collection_set_based_permission, CollectionSetOrCollectionVisibilityOrSuperuserOrStaffPermissionMixin
from .templatetags import ui_extras

import os
import logging
import csv

log = logging.getLogger(__name__)

class CollectionSetListView(LoginRequiredMixin, ListView):
    model = CollectionSet
    template_name = 'ui/collection_set_list.html'
    paginate_by = 20
    allow_empty = True
    paginate_orphans = 0

    def get_context_data(self, **kwargs):
        context = super(CollectionSetListView, self).get_context_data(**kwargs)
        # collection set identity, collection set type Name, collection set data
        collection_sets_lists = []
        active_collection_sets, inactive_collection_sets = split_collection_sets(
            CollectionSet.objects.filter(group__in=self.request.user.groups.all()).annotate(
                num_collections=Count('collections')).order_by('name'))
        collection_sets_lists.append(('acs', "Active", active_collection_sets))
        collection_sets_lists.append(('iacs', "Inactive", inactive_collection_sets))
        collection_sets_lists.append(('scs', "Shared", list(
            CollectionSet.objects.filter(collections__visibility=Collection.LOCAL_VISIBILITY).annotate(
                num_collections=Count('collections')).order_by('name'))))
        if self.request.user.is_superuser or self.request.user.is_staff:
            other_active_collection_sets, other_inactive_collection_sets = split_collection_sets(
                CollectionSet.objects.exclude(group__in=self.request.user.groups.all()).annotate(
                    num_collections=Count('collections')).order_by('name'))
            collection_sets_lists.append(('oacs', "Other Active", other_active_collection_sets))
            collection_sets_lists.append(('oics', "Other Inactive", other_inactive_collection_sets))
        context['collection_sets_lists'] = collection_sets_lists
        return context

def split_collection_sets(collection_sets):
    active_collection_sets = []
    inactive_collection_sets = []
    for collection_set in collection_sets:
        # Treating collection sets with no collections as active.
        if collection_set.is_active() or len(collection_set.collections.all()) == 0:
            active_collection_sets.append(collection_set)
        else:
            inactive_collection_sets.append(collection_set)
    return active_collection_sets, inactive_collection_sets

class CollectionSetDetailView(LoginRequiredMixin, CollectionSetOrCollectionVisibilityOrSuperuserOrStaffPermissionMixin,
                              DetailView):
    model = CollectionSet
    template_name = 'ui/collection_set_detail.html'
    context_object_name = 'collection_set'

    def get_context_data(self, **kwargs):
        context = super(CollectionSetDetailView, self).get_context_data(**kwargs)
        collections = []
        for collection in self.object.collections.all():
            if has_collection_set_based_permission(collection, self.request.user, allow_superuser=True,
                                                   allow_staff=True,
                                                   allow_collection_visibility=True):
                collections.append(collection)
        context['collection_list'] = collections
        context["diffs"], context["diffs_len"] = top_object_diffs(self.object)
        context["harvest_types"] = Collection.HARVEST_CHOICES
        context["harvest_description"] = Collection.HARVEST_DESCRIPTION
        context["item_id"] = self.object.id
        context["model_name"] = "collection_set"
        context["can_edit"] = has_collection_set_based_permission(self.object, self.request.user)
        return context

class CollectionSetCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
    model = CollectionSet
    form_class = CollectionSetForm
    template_name = 'ui/collection_set_create.html'
    success_message = "New collection set added. You can now add collections. A collection retrieves data from a " \
                      "particular social media platform."

    def get_form_kwargs(self):
        kwargs = super(CollectionSetCreateView, self).get_form_kwargs()
        kwargs['request'] = self.request
        return kwargs

    def get_success_url(self):
        return reverse('collection_set_detail', args=(self.object.pk,))

class CollectionSetUpdateView(LoginRequiredMixin, CollectionSetOrSuperuserPermissionMixin, UpdateView):
    model = CollectionSet
    form_class = CollectionSetForm
    template_name = 'ui/collection_set_update.html'
    initial = {'history_note': ''}
    context_object_name = 'collection_set'

    def get_context_data(self, **kwargs):
        context = super(CollectionSetUpdateView, self).get_context_data(**kwargs)
        context['collection_list'] = Collection.objects.filter(
            collection_set=self.object.pk).annotate(num_seeds=Count('seeds')).order_by('name')
        return context

    def get_form_kwargs(self):
        kwargs = super(CollectionSetUpdateView, self).get_form_kwargs()
        kwargs['request'] = self.request
        return kwargs

    def get_success_url(self):
        return reverse("collection_set_detail", args=(self.object.pk,))

class CollectionSetAddNoteView(LoginRequiredMixin, RedirectView):
    permanent = False
    pattern_name = "collection_set_detail"
    http_method_names = ['post', 'put']

    def get_redirect_url(self, *args, **kwargs):
        collection_set = get_object_or_404(CollectionSet, pk=kwargs['pk'])
        # Check permissions to add note
        check_collection_set_based_permission(collection_set, self.request.user)
        collection_set.history_note = self.request.POST.get("history_note", "")
        if collection_set.history_note:
            log.debug("Adding note %s to %s", collection_set.history_note, collection_set)
            collection_set.save()
            messages.info(self.request, "Note added.")
        return super(CollectionSetAddNoteView, self).get_redirect_url(*args, **kwargs)

class SeedsJSONAPIView(LoginRequiredMixin, BaseDatatableView):
    columns = ['link', 'token', 'uid', 'messages']
    # need to define the order columns, also need to match the Datatables setting in columnDefs
    order_columns = ['', 'token', 'uid', '']
    seed_infos = {}
    seed_warnings = {}
    seed_errors = {}
    last_harvest = None
    collection = None
    status = None

    # set max limit of records returned, this is used to protect our site if someone tries to attack our site
    # and make it return huge amount of data
    # max_display_length = 1200

    def get_initial_queryset(self):
        # return queryset used as base for futher sorting/filtering
        # these are simply objects displayed in datatable
        # You should not filter data returned here by any filter values entered by user. This is because
        # we need some base queryset to count total number of records.
        self.collection = get_object_or_404(Collection, pk=self.kwargs['pk'])
        # Check permissions to download
        check_collection_set_based_permission(self.collection, self.request.user, allow_staff=True,
                                              allow_collection_visibility=True)
        self.status = self.kwargs['status']
        self.last_harvest = self.collection.last_harvest()
        self.seed_infos = _get_seed_msg_map(self.last_harvest.infos) if self.last_harvest else {}
        seed_warnings = _get_seed_msg_map(self.last_harvest.warnings) if self.last_harvest else {}
        _add_duplicate_seed_warnings(self.collection, self.seed_warnings)
        self.seed_warnings = seed_warnings
        self.seed_errors = _get_seed_msg_map(self.last_harvest.errors) if self.last_harvest else {}

        # get rid of the exclude field
        self.columns = ['link', 'token', 'uid', 'messages']
        exclude_fields = []
        if not Collection.HARVEST_FIELDS[self.collection.harvest_type]['link']:
            exclude_fields.append('link')
        if not Collection.HARVEST_FIELDS[self.collection.harvest_type]['uid']:
            exclude_fields.append('uid')
        if not Collection.HARVEST_FIELDS[self.collection.harvest_type]['token']:
            exclude_fields.append('token')

        for exclusion in exclude_fields:
            if exclusion in self.columns:
                self.columns.remove(exclusion)
        # Need to update this property here, because it's used by the get_context_data() method of BaseDataTableView and is set PRIOR to the call to this method
        self._columns = self.columns

        return Seed.objects.filter(collection=self.kwargs['pk'], is_active=self.status == 'active').order_by('token',
                                                                                'uid')

    def filter_queryset(self, qs):
        # use request parameters to filter queryset
        search = self.request.GET.get(u'search[value]', None)
        if search:
            qs = qs.filter(Q(token__icontains=search) | Q(uid__icontains=search))
        return qs

    def render_column(self, row, column):
        # We want to render user as a custom column
        if column == 'link':
            platform_type = self.collection.harvest_type.split('_')[0]
            return mark_safe(u' '.format(
                row.social_url(), static('ui/img/{}_logo.png'.format(platform_type)), platform_type, row.token))
        elif column == 'messages':
            msg_seed = ""
            for msg in self.seed_infos.get(row.seed_id, []):
                msg_seed += u'

{}

'.format(msg)
            for msg in self.seed_warnings.get(row.seed_id, []):
                msg_seed += u'

{}

'.format(msg)
            for msg in self.seed_errors.get(row.seed_id, []):
                msg_seed += u'

{}

'.format(msg)
            return mark_safe(u'
{}
'.format(msg_seed)) if msg_seed else ""

        elif column == 'uid':
            return mark_safe(u'{}'.format(reverse('seed_detail', args=[row.pk]),
                                                           row.uid)) if row.uid else ""
        elif column == 'token':
            return mark_safe(u'{}'.format(reverse('seed_detail', args=[row.pk]),
                                                           ui_extras.json_list(row.token))) if row.token else ""

class CollectionDetailView(LoginRequiredMixin, CollectionSetOrCollectionVisibilityOrSuperuserOrStaffPermissionMixin,
                           DetailView):
    model = Collection
    template_name = 'ui/collection_detail.html'

    def get_context_data(self, **kwargs):
        context = super(CollectionDetailView, self).get_context_data(**kwargs)

        context["next_run_time"] = next_run_time(self.object.id)
        # Last 5 harvests
        context["harvests"] = self.object.harvests.all().order_by('-date_requested')[:5]
        context["harvest_count"] = self.object.harvests.all().count()
        last_harvest = self.object.last_harvest()
        context["last_harvest"] = last_harvest
        context["seed_infos"] = _get_seed_msg_map(last_harvest.infos) if last_harvest else {}
        seed_warnings = _get_seed_msg_map(last_harvest.warnings) if last_harvest else {}
        _add_duplicate_seed_warnings(self.object, seed_warnings)
        context["seed_warnings"] = seed_warnings
        context["seed_errors"] = _get_seed_msg_map(last_harvest.errors) if last_harvest else {}
        diffs = []
        collection_history_iter = CollectionHistoryIter(self.object.history.all(),
                                                        Seed.history.filter(collection=self.object).order_by(
                                                            "-history_date"))
        for historical_obj in collection_history_iter[0:3]:
            diffs.append(diff_historical_object(historical_obj))
        context["diffs"] = diffs
        context["diffs_len"] = len(collection_history_iter)
        context["has_seeds_list"] = self.object.required_seed_count() != 0
        has_perms = has_collection_set_based_permission(self.object, self.request.user)
        # Edit button should be enabled.
        can_edit = not self.object.is_on and self.object.is_active and has_perms
        context["can_edit"] = can_edit
        # Seed add buttons should be enabled.
        context["can_edit_seeds"] = can_edit if self.object.is_streaming() else self.object.is_active and has_perms
        context["can_toggle_on"] = has_perms
        context["can_toggle_active"] = not self.object.is_on and has_perms
        # If last harvest is stopping
        context["stream_stopping"] = self.object.last_harvest().status == Harvest.STOP_REQUESTED \
            if self.object.last_harvest() else False
        # For not enough seeds
        seed_warning_message = None
        # For too many seeds
        seed_error_message = None
        # No active seeds.
        if self.object.required_seed_count() == 0 and self.object.active_seed_count() != 0:
            seed_error_message = "All seeds must be deleted before harvesting can be turned on."
        # Specific number of active seeds.
        elif self.object.required_seed_count() == 1:
            if self.object.active_seed_count() == 0:
                seed_warning_message = "1 active seed must be added before harvesting can be turned on."
            elif self.object.active_seed_count() > 1:
                seed_error_message = "Delete all seeds except 1 before harvesting can be turned on."
        elif self.object.required_seed_count() is not None and self.object.required_seed_count() > 1:
            if self.object.active_seed_count() < self.object.required_seed_count():
                seed_warning_message = "{} active seeds must be added before harvesting can be turned on.".format(
                    self.object.required_seed_count())
            elif self.object.active_seed_count > self.object.required_seed_count():
                seed_error_message = "Delete all seeds except {} before harvesting can be turned on.".format(
                    self.object.required_seed_count())
        # At least one active seeds
        elif self.object.required_seed_count() is None and self.object.active_seed_count() == 0:
            seed_warning_message = "At least 1 active seed must be added before harvesting can be turned on."
        context["seed_error_message"] = seed_error_message
        context["seed_warning_message"] = seed_warning_message

        # Adding credential active message, only one filter can occupy the credential
        credential_used_col = None
        if self.object.harvest_type in Collection.STREAMING_HARVEST_TYPES:
            credential_used_col_object = Collection.objects.filter(credential=self.object.credential.pk,
                                                                   harvest_type__in=Collection.STREAMING_HARVEST_TYPES,
                                                                   is_on=True)
            if credential_used_col_object.count() != 0:
                credential_used_col = credential_used_col_object[0]
        context["credential_used_col"] = credential_used_col
        # Harvest types that are not limited support bulk add
        context["can_add_bulk_seeds"] = self.object.required_seed_count() is None
        # Can export if there is a WARC
        context["can_export"] = Warc.objects.filter(harvest__harvest_type=self.object.harvest_type,
                                                    harvest__historical_collection__id=self.object.id).exists()
        context["item_id"] = self.object.id
        context["model_name"] = "collection"
        context["status_choices"] = Harvest.STATUS_CHOICES
        context["harvest_fields"] = Collection.HARVEST_FIELDS
        return context

def download_seed_list(request, pk):
    collection = get_object_or_404(Collection, pk=pk)
    # Check permissions to download
    check_collection_set_based_permission(collection, request.user, allow_staff=True, allow_collection_visibility=True)
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="seedlist.csv"'
    writer = csv.writer(response, delimiter=',')
    writer.writerow([
        "Token",
        "Uid",
        "Link",
    ])
    for seed in collection.seeds.all():
        if seed.is_active:
            writer.writerow([
                seed.token,
                seed.uid,
                (seed.social_url() or "")
            ])
    return response

def _add_duplicate_seed_warnings(collection, seed_warnings):
    for result in collection.seeds.exclude(token__exact="").values("token").annotate(count=Count("id")).filter(
            count__gt=1):
        for seed in Seed.objects.filter(token=result["token"]):
            if seed.seed_id not in seed_warnings:
                seed_warnings[seed.seed_id] = []
            seed_warnings[seed.seed_id].append("Duplicate seeds exist with this token.")
    for result in collection.seeds.exclude(uid__exact="").values("uid").annotate(count=Count("id")).filter(count__gt=1):
        for seed in Seed.objects.filter(token=result["uid"]):
            if seed.seed_id not in seed_warnings:
                seed_warnings[seed.seed_id] = []
            seed_warnings[seed.seed_id].append("Duplicate seeds exist with this uid.")

def _get_seed_msg_map(msgs):
    seed_msg_map = {}
    for msg in msgs:
        if "seed_id" in msg:
            seed_id = msg["seed_id"]
            if seed_id not in seed_msg_map:
                seed_msg_map[seed_id] = []
            seed_msg_map[seed_id].append(msg["message"])
    return seed_msg_map

def _get_collection_form_class(harvest_type):
    return "Collection{}Form".format(harvest_type.replace("_", " ").title().replace(" ", ""))

def _get_harvest_type_name(harvest_type):
    for harvest_type_choice, harvest_type_name in Collection.HARVEST_CHOICES:
        if harvest_type_choice == harvest_type:
            return harvest_type_name

def _get_credential_list(collection_set_pk, harvest_type, extra_credential=None):
    collection_set = CollectionSet.objects.get(pk=collection_set_pk)
    platform = Collection.HARVEST_TYPES_TO_PLATFORM[harvest_type]
    q = Q(platform=platform, user__in=User.objects.filter(groups=collection_set.group))
    if extra_credential:
        q = q | Q(pk=extra_credential.pk)
    return Credential.objects.filter(q).filter(is_active=True).order_by('name')

def _get_credential_use_map(credentials, harvest_type):
    credential_use_map = {}
    if harvest_type in Collection.RATE_LIMITED_HARVEST_TYPES:
        for credential in credentials:
            active_collections = 0
            inactive_collections = 0
            for collection in credential.collections.all():
                if collection.is_on:
                    active_collections += 1
                else:
                    inactive_collections += 1
            if active_collections == 0 and inactive_collections == 0:
                credential_use_map[credential.id] = ("", "")
            else:
                credential_use_map[credential.id] = ("warning",
                                                     "Credential is in use by {0} collections that are turned on and "
                                                     "{1} collections that are turned off. Be mindful that over-using "
                                                     "credentials may result in collecting being rate limited by the "
                                                     "social media API.".format(active_collections,
                                                                                inactive_collections))
        return credential_use_map

class CollectionCreateView(LoginRequiredMixin, CollectionSetOrSuperuserPermissionMixin, SuccessMessageMixin,
                           CreateView):
    model = Collection
    template_name = 'ui/collection_create.html'

    def get_initial(self):
        initial = super(CollectionCreateView, self).get_initial()
        initial["collection_set"] = CollectionSet.objects.get(pk=self.kwargs["collection_set_pk"])
        return initial

    def get_context_data(self, **kwargs):
        context = super(CollectionCreateView, self).get_context_data(**kwargs)
        context["collection_set"] = CollectionSet.objects.get(pk=self.kwargs["collection_set_pk"])
        harvest_type = self.kwargs["harvest_type"]
        context["harvest_type_name"] = _get_harvest_type_name(harvest_type)
        credentials = _get_credential_list(self.kwargs["collection_set_pk"], harvest_type)
        context["credentials"] = credentials
        context["credential_use_map"] = _get_credential_use_map(credentials, harvest_type)
        context["platform"] = Collection.HARVEST_TYPES_TO_PLATFORM[self.kwargs["harvest_type"]]
        return context

    def get_form_kwargs(self):
        kwargs = super(CollectionCreateView, self).get_form_kwargs()
        kwargs["coll"] = self.kwargs["collection_set_pk"]
        kwargs['credential_list'] = _get_credential_list(self.kwargs["collection_set_pk"], self.kwargs["harvest_type"])
        return kwargs

    def get_form_class(self):
        return getattr(forms, _get_collection_form_class(self.kwargs["harvest_type"]))

    def get_success_url(self):
        return reverse('collection_detail', args=(self.object.pk,))

    def get_success_message(self, cleaned_data):
        if self.object.required_seed_count() != 0:
            return "New collection added. You can now add seeds."
        return "New collection added."

class CollectionUpdateView(LoginRequiredMixin, CollectionSetOrSuperuserPermissionMixin, UpdateView):
    model = Collection
    template_name = 'ui/collection_update.html'
    initial = {'history_note': ''}

    def get_context_data(self, **kwargs):
        context = super(CollectionUpdateView, self).get_context_data(**kwargs)
        context["collection_set"] = self.object.collection_set
        context["seed_list"] = Seed.objects.filter(collection=self.object.pk).order_by('token', 'uid')
        context["has_seeds_list"] = self.object.required_seed_count() != 0
        credentials = _get_credential_list(self.object.collection_set.pk, self.object.harvest_type,
                                           self.object.credential)
        context["credential_use_map"] = _get_credential_use_map(credentials, self.object.harvest_type)
        return context

    def get_form_kwargs(self):
        kwargs = super(CollectionUpdateView, self).get_form_kwargs()
        kwargs["coll"] = self.object.collection_set.pk
        kwargs['credential_list'] = _get_credential_list(self.object.collection_set.pk, self.object.harvest_type,
                                                         self.object.credential)
        return kwargs

    def get_form_class(self):
        return getattr(forms, _get_collection_form_class(self.object.harvest_type))

    def get_success_url(self):
        return reverse("collection_detail", args=(self.object.pk,))

class CollectionToggleOnView(LoginRequiredMixin, RedirectView):
    permanent = False
    pattern_name = "collection_detail"
    http_method_names = ['post', 'put']

    def get_redirect_url(self, *args, **kwargs):
        collection = get_object_or_404(Collection, pk=kwargs['pk'])
        # Check permissions to toggle
        check_collection_set_based_permission(collection, self.request.user)
        collection.is_on = not collection.is_on
        collection.history_note = self.request.POST.get("history_note", "")
        if collection.is_on:
            messages.info(self.request, "Harvesting is turned on.")
        else:
            messages.info(self.request, "Harvesting is turned off.")
        collection.save()
        return super(CollectionToggleOnView, self).get_redirect_url(*args, **kwargs)

class CollectionToggleActiveView(LoginRequiredMixin, RedirectView):
    permanent = False
    pattern_name = "collection_detail"
    http_method_names = ['post', 'put']

    def get_redirect_url(self, *args, **kwargs):
        collection = get_object_or_404(Collection, pk=kwargs['pk'])
        # Check permissions to toggle
        check_collection_set_based_permission(collection, self.request.user)
        collection.is_active = not collection.is_active
        collection.history_note = self.request.POST.get("history_note", "")
        if collection.is_active:
            messages.info(self.request, "Collection is turned active.")
        else:
            messages.info(self.request, "Collection is turned inactive.")
        collection.save()
        return super(CollectionToggleActiveView, self).get_redirect_url(*args, **kwargs)

class CollectionAddNoteView(LoginRequiredMixin, RedirectView):
    permanent = False
    pattern_name = "collection_detail"
    http_method_names = ['post', 'put']

    def get_redirect_url(self, *args, **kwargs):
        collection = get_object_or_404(Collection, pk=kwargs['pk'])
        # Check permissions to add note
        check_collection_set_based_permission(collection, self.request.user)
        collection.history_note = self.request.POST.get("history_note", "")
        if collection.history_note:
            log.debug("Adding note %s to %s", collection.history_note, collection)
            collection.save()
            messages.info(self.request, "Note added.")
        return super(CollectionAddNoteView, self).get_redirect_url(*args, **kwargs)

class SeedDetailView(LoginRequiredMixin, CollectionSetOrCollectionVisibilityOrSuperuserOrStaffPermissionMixin,
                     DetailView):
    model = Seed
    template_name = 'ui/seed_detail.html'

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(SeedDetailView, self).get_context_data(**kwargs)
        context["diffs"], context["diffs_len"] = top_object_diffs(self.object)
        context["collection_set"] = CollectionSet.objects.get(id=self.object.collection.collection_set.id)
        context["item_id"] = self.object.id
        context["model_name"] = "seed"
        can_edit = has_collection_set_based_permission(self.object,
                                                       self.request.user) and self.object.collection.is_active
        if self.object.collection.is_streaming():
            can_edit = can_edit and not self.object.collection.is_on
        context["can_edit"] = can_edit
        return context

def _get_seed_form_class(harvest_type):
    return "Seed{}Form".format(harvest_type.replace("_", " ").title().replace(" ", ""))

def top_object_diffs(obj):
    diffs = []
    for historical_obj in obj.history.all()[0:3]:
        diffs.append(diff_historical_object(historical_obj))
    return diffs, obj.history.all().count()

class SeedCreateView(LoginRequiredMixin, CollectionSetOrSuperuserPermissionMixin, SuccessMessageMixin, CreateView):
    model = Seed
    template_name = 'ui/seed_create.html'

    def get_initial(self):
        initial = super(SeedCreateView, self).get_initial()
        collection = Collection.objects.get(pk=self.kwargs["collection_pk"])
        initial["collection"] = collection
        initial["collection_set"] = collection.collection_set
        return initial

    def get_context_data(self, **kwargs):
        context = super(SeedCreateView, self).get_context_data(**kwargs)
        collection = Collection.objects.get(pk=self.kwargs["collection_pk"])
        context["collection"] = collection
        context["collection_set"] = context["collection"].collection_set
        context["harvest_type_name"] = _get_harvest_type_name(collection.harvest_type)
        return context

    def get_form_kwargs(self):
        kwargs = super(SeedCreateView, self).get_form_kwargs()
        kwargs["collection"] = self.kwargs["collection_pk"]
        kwargs["view_type"] = Seed.CREATE_VIEW
        return kwargs

    def get_form_class(self):
        return getattr(forms,
                       _get_seed_form_class(Collection.objects.get(pk=self.kwargs["collection_pk"]).harvest_type))

    def get_success_url(self):
        return reverse("collection_detail", args=(self.kwargs["collection_pk"],))

    def get_success_message(self, cleaned_data):
        if self.object.collection.required_seed_count() == 1:
            return "New seed added."
        return "New seed added."

class SeedUpdateView(LoginRequiredMixin, CollectionSetOrSuperuserPermissionMixin, UpdateView):
    model = Seed
    template_name = 'ui/seed_update.html'
    initial = {'history_note': ''}

    def get_form_kwargs(self):
        kwargs = super(SeedUpdateView, self).get_form_kwargs()
        kwargs["collection"] = self.object.collection.pk
        kwargs["view_type"] = Seed.UPDATE_VIEW
        kwargs["entry"] = self.get_object()
        return kwargs

    def get_context_data(self, **kwargs):
        context = super(SeedUpdateView, self).get_context_data(**kwargs)
        context["collection_set"] = CollectionSet.objects.get(id=self.object.collection.collection_set.id)
        return context

    def get_form_class(self):
        return getattr(forms, _get_seed_form_class(self.object.collection.harvest_type))

    def get_success_url(self):
        return reverse("seed_detail", args=(self.object.pk,))

class BulkSeedCreateView(LoginRequiredMixin, View):
    template_name = 'ui/bulk_seed_create.html'

    def get(self, request, *args, **kwargs):
        collection = Collection.objects.get(pk=kwargs["collection_pk"])
        # Check permissions
        check_collection_set_based_permission(collection, request.user)
        form = self._form_class(collection)(initial={}, collection=kwargs["collection_pk"])
        return self._render(request, form, collection)

    def post(self, request, *args, **kwargs):
        collection = Collection.objects.get(pk=kwargs["collection_pk"])
        # Check permissions
        check_collection_set_based_permission(collection, request.user)
        form = self._form_class(collection)(request.POST, collection=kwargs["collection_pk"])
        if form.is_valid():
            tokens = form.cleaned_data['tokens'].splitlines()
            seeds_type = form.cleaned_data['seeds_type']
            seed_count = 0
            if collection.harvest_type == collection.TUMBLR_BLOG_POSTS:
                cleaned_data = [clean_blogname(t) for t in tokens]
            else:
                cleaned_data = [clean_token(t) for t in tokens]

            for token in cleaned_data:
                # Can't save a token called "null". It causes problems.
                if token and token.lower() != "null":
                    param = {'uid': token} if seeds_type == 'uid' else {'token__iexact': token}
                    if not Seed.objects.filter(collection=collection, **param).exists():
                        log.debug("Creating seed %s for collection %s", token, collection.pk)
                        param = {'uid': token} if seeds_type == 'uid' else {'token': token}
                        Seed.objects.create(collection=collection,
                                            history_note=form.cleaned_data['history_note'], **param)
                        seed_count += 1
                    else:
                        log.debug("Skipping creating seed %s for collection %s since it exists", token,
                                  collection.pk)
            # <process form cleaned data>
            messages.info(request, "{} seeds added.".format(seed_count))
            return HttpResponseRedirect(reverse("collection_detail", args=(self.kwargs["collection_pk"],)))

        return self._render(request, form, collection)

    @staticmethod
    def _form_class(collection):
        return getattr(forms,
                       "BulkSeed{}Form".format(collection.harvest_type.replace("_", " ").title().replace(" ", "")))

    def _render(self, request, form, collection):
        return render(request, self.template_name,
                      {'form': form, 'collection': collection, 'collection_set': collection.collection_set,
                       'harvest_type_name': _get_harvest_type_name(collection.harvest_type)})

class SeedToggleActiveView(LoginRequiredMixin, RedirectView):
    permanent = False
    pattern_name = "seed_detail"
    http_method_names = ['post', 'put']

    def get_redirect_url(self, *args, **kwargs):
        seed = get_object_or_404(Seed, pk=kwargs['pk'])
        # Check permissions to toggle
        check_collection_set_based_permission(seed.collection, self.request.user)
        seed.is_active = not seed.is_active
        seed.history_note = self.request.POST.get("history_note", "")
        if seed.is_active:
            messages.info(self.request, "Seed undeleted.")
        else:
            messages.info(self.request, "Seed deleted.")
        seed.save()
        return super(SeedToggleActiveView, self).get_redirect_url(*args, **kwargs)

class SeedAddNoteView(LoginRequiredMixin, RedirectView):
    permanent = False
    pattern_name = "seed_detail"
    http_method_names = ['post', 'put']

    def get_redirect_url(self, *args, **kwargs):
        seed = get_object_or_404(Seed, pk=kwargs['pk'])
        # Check permissions to add note
        check_collection_set_based_permission(seed, self.request.user)
        seed.history_note = self.request.POST.get("history_note", "")
        if seed.history_note:
            log.debug("Adding note %s to %s", seed.history_note, seed)
            seed.save()
            messages.info(self.request, "Note added.")
        return super(SeedAddNoteView, self).get_redirect_url(*args, **kwargs)

class CredentialDetailView(LoginRequiredMixin, UserOrSuperuserOrStaffPermissionMixin, DetailView):
    model = Credential
    template_name = 'ui/credential_detail.html'

    def get_context_data(self, **kwargs):
        # Call the base implementation first to get a context
        context = super(CredentialDetailView, self).get_context_data(**kwargs)
        context["diffs"], context["diffs_len"] = top_object_diffs(self.object)
        context["can_edit"] = self.request.user.is_superuser or self.object.user == self.request.user
        context["item_id"] = self.object.id
        context["model_name"] = "credential"
        context['collection_list'] = Collection.objects.filter(credential=self.object.pk).order_by('name')
        return context

class CredentialCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
    model = Credential
    template_name = 'ui/credential_create.html'
    success_message = "New credential added."

    def get_form_kwargs(self):
        kwargs = super(CredentialCreateView, self).get_form_kwargs()
        kwargs["view_type"] = Credential.CREATE_VIEW
        return kwargs

    def get_form_class(self):
        class_name = "Credential{}Form".format(self.kwargs["platform"].title())
        return getattr(forms, class_name)

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(CredentialCreateView, self).form_valid(form)

    def get_success_url(self):
        return reverse("credential_detail", args=(self.object.pk,))

    def get_initial(self):
        initial = super(CredentialCreateView, self).get_initial()
        credential_name = "{}'s {} credential".format(self.request.user.username, self.kwargs["platform"])
        if not Credential.objects.filter(name=credential_name).exists():
            initial['name'] = credential_name
        return initial

class CredentialListView(LoginRequiredMixin, ListView):
    model = Credential
    template_name = 'ui/credential_list.html'
    allow_empty = True

    def get_context_data(self, **kwargs):
        context = super(CredentialListView, self).get_context_data(**kwargs)
        # (type, credential_list, can_connect_app)
        credentials = []
        for social_type, _ in Credential.PLATFORM_CHOICES:
            credential_objs = Credential.objects.filter(user=self.request.user,
                                                        platform=social_type).order_by('name')
            credentials.append((social_type, credential_objs, self._can_connect_credential(social_type)))
        context["credentials_lists"] = credentials
        return context

    @staticmethod
    def _can_connect_credential(platform):
        """
        Returns True if a Social App is configured for this platform.
        """
        return SocialApp.objects.filter(provider=platform).exists()

class CredentialUpdateView(LoginRequiredMixin, UserOrSuperuserPermissionMixin, UpdateView):
    model = Credential
    template_name = 'ui/credential_update.html'
    initial = {'history_note': ''}

    def get_form_kwargs(self):
        kwargs = super(CredentialUpdateView, self).get_form_kwargs()
        kwargs["view_type"] = Credential.UPDATE_VIEW
        kwargs["entry"] = self.get_object()
        return kwargs

    def get_form_class(self):
        class_name = "Credential{}Form".format(self.object.platform.title())
        return getattr(forms, class_name)

    def get_success_url(self):
        return reverse("credential_detail", args=(self.object.pk,))

class CredentialToggleActiveView(LoginRequiredMixin, UserOrSuperuserPermissionMixin, RedirectView):
    permanent = False
    pattern_name = "credential_detail"
    http_method_names = ['post', 'put']

    def get_redirect_url(self, *args, **kwargs):
        credential = get_object_or_404(Credential, pk=kwargs['pk'])
        credential.is_active = not credential.is_active
        credential.history_note = self.request.POST.get("history_note", "")
        if credential.is_active:
            messages.info(self.request, "Credential undeleted.")
        else:
            messages.info(self.request, "Credential deleted.")
        credential.save()
        return super(CredentialToggleActiveView, self).get_redirect_url(*args, **kwargs)

class ExportListView(LoginRequiredMixin, ListView):
    model = Export
    template_name = 'ui/export_list.html'
    paginate_by = 20
    allow_empty = True
    paginate_orphans = 0

    def get_context_data(self, **kwargs):
        context = super(ExportListView, self).get_context_data(**kwargs)
        exports = Export.objects.filter(
            user=self.request.user).order_by(
            '-date_requested')
        export_list = []
        for export in exports:
            seeds = list(export.seeds.all())
            collection = seeds[0].collection if seeds else export.collection
            if collection:
                export_list.append((collection.collection_set, collection, export))
        context['export_list'] = export_list
        return context

class ExportCreateView(LoginRequiredMixin, CollectionSetOrCollectionVisibilityOrSuperuserOrStaffPermissionMixin,
                       SuccessMessageMixin, CreateView):
    model = Export
    form_class = ExportForm
    template_name = 'ui/export_create.html'
    success_message = "Export requested. Check the Exports section for the status of your export. Large datasets may " \
                      "take a substantial amount of time. You will receive an email when your export is ready."

    def get_initial(self):
        initial = super(ExportCreateView, self).get_initial()
        collection = Collection.objects.get(pk=self.kwargs["collection_pk"])
        initial["collection"] = collection
        initial["collection_set"] = collection.collection_set
        return initial

    def get_context_data(self, **kwargs):
        context = super(ExportCreateView, self).get_context_data(**kwargs)
        context["collection"] = Collection.objects.get(pk=self.kwargs["collection_pk"])
        return context

    def get_form_kwargs(self):
        kwargs = super(ExportCreateView, self).get_form_kwargs()
        kwargs["collection"] = self.kwargs["collection_pk"]
        return kwargs

    def form_valid(self, form):
        # This will set user
        form.instance.user = self.request.user
        return super(ExportCreateView, self).form_valid(form)

    def get_success_url(self):
        return reverse('export_detail', args=(self.object.pk,))

def _get_fileinfos(path):
    """
    Returns list of file names, bytes within directory
    """
    fileinfos = list()
    if os.path.exists(path):
        contents = os.listdir(path)
        for item in contents:
            p = os.path.join(path, item)
            if os.path.isfile(p):
                fileinfos.append((item, os.path.getsize(p)))
    return sorted(fileinfos)

class ExportDetailView(LoginRequiredMixin, CollectionSetOrCollectionVisibilityOrSuperuserOrStaffPermissionMixin,
                       DetailView):
    model = Export
    template_name = 'ui/export_detail.html'

    def get_context_data(self, **kwargs):
        context = super(ExportDetailView, self).get_context_data(**kwargs)
        seeds = list(self.object.seeds.all())
        collection = seeds[0].collection if seeds else self.object.collection
        context["collection_set"] = collection.collection_set
        context["collection"] = collection
        context["fileinfos"] = _get_fileinfos(self.object.path) if self.object.status == Export.SUCCESS else ()
        return context

class HarvestListView(LoginRequiredMixin, ListView):
    context_object_name = "harvest_list"
    template_name = "ui/harvest_list.html"
    paginate_by = 15

    def get_queryset(self):
        self.collection = get_object_or_404(Collection, pk=self.kwargs["pk"])
        # Check permissions
        check_collection_set_based_permission(self.collection, self.request.user, allow_staff=True)
        return self.collection.harvests.all().order_by('-date_requested')

    def get_context_data(self, **kwargs):
        context = super(HarvestListView, self).get_context_data(**kwargs)
        context["collection_set"] = self.collection.collection_set
        context['collection'] = self.collection
        return context

class HarvestDetailView(LoginRequiredMixin, CollectionSetOrCollectionVisibilityOrSuperuserOrStaffPermissionMixin,
                        DetailView):
    model = Harvest
    template_name = 'ui/harvest_detail.html'

    def get_context_data(self, **kwargs):
        context = super(HarvestDetailView, self).get_context_data(**kwargs)
        context["collection_set"] = self.object.collection.collection_set
        context["collection"] = self.object.collection
        # If status is running or requested and not a streaming and if this is the most recent harvest
        context["can_void"] = False
        if self.object.status in (Harvest.RUNNING, Harvest.REQUESTED, Harvest.STOP_REQUESTED, Harvest.PAUSED) \
                and self.object == self.object.collection.last_harvest() \
                and has_collection_set_based_permission(self.object, self.request.user):
            context["can_void"] = True
        return context

class HarvestVoidView(LoginRequiredMixin, RedirectView):
    permanent = False
    pattern_name = "harvest_detail"
    http_method_names = ['post', 'put']

    def get_redirect_url(self, *args, **kwargs):
        harvest = get_object_or_404(Harvest, pk=kwargs['pk'])

        # Check permissions to void
        check_collection_set_based_permission(harvest, self.request.user)

        if harvest.status in (Harvest.RUNNING, Harvest.REQUESTED, Harvest.STOP_REQUESTED, Harvest.PAUSED):
            log.debug("Voiding %s", harvest)
            harvest.status = Harvest.VOIDED
            harvest.save()
        else:
            log.debug("Not voiding %s since status is %s", harvest, harvest.status)
        return super(HarvestVoidView, self).get_redirect_url(*args, **kwargs)

def _read_file_chunkwise(file_obj):
    """
    Reads file in 32Kb chunks
    """
    while True:
        data = file_obj.read(32768)
        if not data:
            break
        yield data

def export_file(request, pk, file_name):
    """
    Allows authorized user to export a file.

    Adapted from https://github.com/ASKBOT/django-directory
    """
    export = get_object_or_404(Export, pk=pk)
    if (request.user == export.user) or request.user.is_superuser:
        filepath = os.path.join(export.path, file_name)
        log.debug("Exporting %s", filepath)
        if os.path.exists(filepath):
            response = StreamingHttpResponse()
            response['Content-Disposition'] = 'attachment; filename=%s' % file_name
            response['Content-Type'] = 'application/octet-stream'
            file_obj = open(filepath, 'rb')
            response.streaming_content = _read_file_chunkwise(file_obj)
            return response
        else:
            raise Http404
    else:
        raise PermissionDenied

class CollectionChangeLogView(LoginRequiredMixin, TemplateView):
    template_name = "ui/change_log.html"

    def get_context_data(self, **kwargs):
        context = super(CollectionChangeLogView, self).get_context_data(**kwargs)
        collection_id = self.kwargs["item_id"]
        collection = Collection.objects.get(pk=collection_id)

        # Check permissions to view
        check_collection_set_based_permission(collection, self.request.user, allow_staff=True,
                                              allow_collection_visibility=True)

        context["item"] = collection

        paginator = Paginator(CollectionHistoryIter(collection.history.all(),
                                                    Seed.history.filter(collection=collection).order_by(
                                                        "-history_date")), 15)
        # if no page in URL, show first
        page = self.request.GET.get("page", 1)
        diffs_page = paginator.page(page)
        context["paginator"] = paginator
        context["page_obj"] = diffs_page
        diff_list = list()
        for historical_obj in diffs_page:
            diff_list.append(diff_historical_object(historical_obj))
        context['diff_list'] = diff_list

        context["model_name"] = "collection"
        context["name"] = collection.name
        return context

class ChangeLogView(LoginRequiredMixin, ListView):
    context_object_name = "history_list"
    template_name = "ui/change_log.html"
    paginate_by = 15

    def get_queryset(self):
        item_id = self.kwargs["item_id"]
        model_name = self.kwargs["model"].replace("_", "")
        ModelName = apps.get_model(app_label="ui", model_name=model_name)
        item = ModelName.objects.get(pk=item_id)
        # Check permissions
        check_collection_set_based_permission(item, self.request.user, allow_staff=True)
        return item.history.all()

    def get_context_data(self, **kwargs):
        context = super(ChangeLogView, self).get_context_data(**kwargs)

        item_id = self.kwargs["item_id"]
        model_name = self.kwargs["model"].replace("_", "")
        ModelName = apps.get_model(app_label="ui", model_name=model_name)
        item = ModelName.objects.get(pk=item_id)

        context["item"] = item
        context["model_name"] = self.kwargs["model"].replace("_", " ")
        try:
            context["name"] = item.name
        except Exception:
            context["name"] = item.token

        diff_list = list()
        for historical_obj in context['history_list']:
            diff_list.append(diff_historical_object(historical_obj))
        context['diff_list'] = diff_list
        return context

def collection_set_stats(_, pk, item, period):
    collection_set = get_object_or_404(CollectionSet, pk=pk)
    return JsonResponse(collection_set.item_stats(item,
                                                  days={
                                                      "week": 7,
                                                      "month": 30,
                                                      "year": 365
                                                  }.get(period, 0)), safe=False)

class HomeView(TemplateView):
    template_name = "ui/home.html"

    def get_context_data(self, **kwargs):
        context = super(HomeView, self).get_context_data(**kwargs)
        context['collection_set_list'] = CollectionSet.objects.filter(
            group__in=self.request.user.groups.all(), collections__is_active=True).distinct().order_by('name')
        context['space_data'] = get_free_space()
        context['queue_data'] = get_queue_data()
        context['shared_data'] = is_shared()
        context['shared_dir_id'] = settings.SFM_DATA_SHARED_DIR if is_shared() else None
        return context

class UserProfileDetailView(LoginRequiredMixin, DetailView):
    model = User
    template_name = 'ui/profile_details.html'

    def get_context_data(self, **kwargs):
        context = super(UserProfileDetailView, self).get_context_data(**kwargs)
        context["user_groups"] = ','.join([g.name for g in self.request.user.groups.all()])
        superusers = User.objects.filter(is_superuser=True)
        # get the first super user email information
        context["email_info"] = 'mailto:' + superusers[0].email if superusers else ''
        return context

    def get_object(self, queryset=None):
        return self.request.user

class UserProfileUpdateView(LoginRequiredMixin, UpdateView):
    model = User
    template_name = 'ui/profile_update.html'

    def get_object(self, queryset=None):
        return self.request.user

    def get_form_class(self):
        class_name = "UserProfileForm"
        return getattr(forms, class_name)

    def get_success_url(self):
        return reverse("user_profile_detail")

class MonitorView(LoginRequiredMixin, TemplateView):
    template_name = "ui/monitor.html"

    def get_context_data(self, **kwargs):
        context = super(MonitorView, self).get_context_data(**kwargs)
        allharvests = monitor_harvests()
        harvests = [harvest for harvest in allharvests if
                    has_collection_set_based_permission(harvest,
                                                        self.request.user,
                                                        allow_superuser=True,
                                                        allow_staff=True,
                                                        allow_collection_visibility=True)]
        allexports = monitor_exports()
        exports = [export for export in allexports if
                   has_collection_set_based_permission(export,
                                                       self.request.user,
                                                       allow_superuser=True,
                                                       allow_collection_visibility=True)]
        context['harvests'] = harvests
        context['exports'] = exports
        context["harvester_queues"], context["exporter_queues"], context["ui_queues"] = monitor_queues()
        return context


text:
/**
 *
 */
package com.gemstone.gemfire.internal.cache.wan;

/**
 * An exception indicating that a gateway configuration will not work with the
 * remote side of the gateway's configuration.
 *
 * @since 7.0
 * @author skumar
 *
 */
public class GatewaySenderConfigurationException extends GatewaySenderException {
  private static final long serialVersionUID = 1L;

  public GatewaySenderConfigurationException() {
    super();
  }

  public GatewaySenderConfigurationException(String msg, Throwable cause) {
    super(msg, cause);
  }

  public GatewaySenderConfigurationException(String msg) {
    super(msg);
  }

  public GatewaySenderConfigurationException(Throwable cause) {
    super(cause);
  }

}


text:
<mat-card class="settings-card mat-elevation-z0">
    <mat-card-header *ngIf="!saveInProgress">
      <mat-card-title>
User Settings
</mat-card-title>
    </mat-card-header>

      <mat-spinner></mat-spinner>

      Saving User Settings...

    <form *ngIf="!saveInProgress" class="form-container" #f="ngForm" [formGroup]="settingsGroup" (ngSubmit)="saveSettings(f.value, f.valid)" novalidate>
      <mat-card-content align="center" class="align-input">
        <mat-form-field class="form-input">
          <input matInput type="text" placeholder="Name" formControlName="name" required />
          <mat-error *ngIf="settingsGroup.hasError('required', ['name'])">Name is required</mat-error>
        </mat-form-field>
        <mat-form-field class="form-input">
          <input matInput type="text" placeholder="Email" formControlName="email" required />
          <mat-error *ngIf="settingsGroup.hasError('required', ['email'])">Email is required</mat-error>
        </mat-form-field>

          <button mat-button class="btn-file">
            <mat-icon>add</mat-icon>
            Upload Profile Picture
            <input type="file" name="file" id="file" class="inputfile" formControlName="profilePicture" (change)="fileChangeListener($event)" accept="image/*"/>
          </button>
          <button mat-mini-fab *ngIf="canPreviewImage()" type="button" (click)="openPreview()" [color]="'preview'">
            <mat-icon aria-label="Preview profile picture" title="Preview profile picture">picture_in_picture</mat-icon>
          </button>

      </mat-card-content>
      <mat-card-actions align="end">
        <button mat-raised-button type="submit" [disabled]="!f.valid" color="accent"> Save Settings </button>
      </mat-card-actions>
    </form>
</mat-card>


text:
<?php
Configure::write('Env.hostname', str_replace('www.', '', env('HTTP_HOST')));

$folder = (isset($_REQUEST['url'])) ? '/'.$_REQUEST['url'] : '';
$folder = rtrim(str_replace($folder, '', env('REQUEST_URI')), '/'); //calculates the current sub-folder of installation
$folder = (strpos($folder, '?') !== false)
	? substr($folder, 0, strpos($folder, '?')) // remove request params
	: $folder;

Configure::write('Env.subfolder', $folder); //current subfolder where app resides

//TMP folder writable?
Configure::write('Env.tmp_writable', (is_writable(TMP)));

//database.php present?
Configure::write('Env.dbconfig.present', (file_exists(CONFIGS.'database.php')));

//database.php can be written?
Configure::write('Env.dbconfig.writable', (is_writable(CONFIGS)));

//database connectable?
$connectable = false;
if(Configure::read('Env.dbconfig.present'))
{
	uses('model' . DS . 'connection_manager');
	$db = ConnectionManager::getInstance();
	@$connected = $db->getDataSource('default');
	$connectable = $connected->isConnected();
}

Configure::write('Env.dbconfig.connected', $connectable);

Configure::write('Env.installed', (Configure::read('Env.dbconfig.connected')) ? true : false); //installed, when connected

Configure::write('Env.site_hash', md5(Configure::read('Env.hostname').Configure::read('Env.subfolder')));

if(!function_exists('get_ip')) {

	/**
	 * Returns current IP or 127.0.0.1 on localhost
	 *
	 * @return string current ip
	 */
	function get_ip() {
		return (env('REMOTE_ADDR') != '::1')
			? env('REMOTE_ADDR')
			: '127.0.0.1';
	}
}