3
6b/                 @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d d	lmZmZ d
d ZeG dd de
ZdS )    N)urljoin)ObjectId)	InvalidId)ImproperlyConfigured)Storage)deconstructible)filepath_to_uri)GridFSNoFilec             c   sF   x@| j j D ]2}|d|jd }|| jkr|j| jr|V  qW dS )z6
    Returns all sub-collections of `collection`.
    N.)databaseZcollection_namesrfindname
startswith)
collectionr   cleaned r   M/var/www/html/sandeepIITI/myenv/lib/python3.6/site-packages/djongo/storage.py_get_subcollections   s    r   c               @   s   e Zd ZdZd#ddZd$d	d
Zdd Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd  Zd!d" ZdS )%GridFSStoragea  
    GridFS Storage backend for Django.
    Based on https://github.com/django-nonrel/mongodb-engine/blob/master/django_mongodb_engine/storage.py

    This backend aims to add a GridFS storage to upload files to
    using Django's file fields.

    For performance, the file hierarchy is represented as a tree of
    MongoDB sub-collections.

    (One could use a flat list, but to list a directory '/this/path/'
    we would have to execute a search over the whole collection and
    then filter the results to exclude those not starting by
    '/this/path' using that model.)

    :param location:
       (optional) Name of the top-level node that holds the files. This
       value of `location` is prepended to all file paths, so it works
       like the `location` setting for Django's built-in
       :class:`~django.core.files.storage.FileSystemStorage`.
    :param collection:
        Name of the collection the file tree shall be stored in.
        Defaults to 'storage'.
    :param database:
        Alias of the Django database to use. Defaults to 'default' (the
        default Django database).
    :param base_url:
        URL that serves the files in GridFS (for instance, through
        nginx-gridfs).
        Defaults to None (file not accessible through a URL).
     storagedefaultNc             C   sN   |j tj| _|| _|| _|| _| js.td| jrJ| jjd rJtdd S )Nz'collection' may not be empty./z)If set, 'base_url' must end with a slash.)	striposseplocationr   r   base_urlr   endswith)selfr   r   r   r   r   r   r   __init__;   s    zGridFSStorage.__init__rbc             C   sH   | j |\}}y
|j|S  tk
rB   d|kr<|j|dS  Y nX dS )z
        Returns a :class:`~gridfs.GridOut` file opened in `mode`, or
        raises :exc:`~gridfs.errors.NoFile` if the requested file
        doesn't exist and mode is not 'w'.
        w)filenameN)_get_gridfsget_last_versionr
   Znew_file)r    pathmodegridfsr$   r   r   r   _openI   s    
zGridFSStorage._openc             C   s$   | j |\}}|j|||jd |S )z:
        Saves `content` into the file at `path`.
        )r$   contentType)r%   putcontent_type)r    r'   contentr)   r$   r   r   r   _saveX   s    zGridFSStorage._savec             C   sD   | j |\}}y|j|j|djd W n tk
r>   Y nX dS )z:
        Deletes the file at `path` if it exists.
        )r$   _idN)r%   deleter&   __getattribute__r
   )r    r'   r)   r$   r   r   r   r1   `   s
    zGridFSStorage.deletec             C   s   | j |\}}|j|dS )zH
        Returns `True` if the file at `path` exists in GridFS.
        )r$   )r%   exists)r    r'   r)   r$   r   r   r   r3   j   s    zGridFSStorage.existsc             C   s@   | j |\}}| stt|jd}tdd |D |j fS )zc
        Returns a tuple (folders, lists) that are contained in the
        folder `path`.
        Z__collectionc             s   s   | ]}|j d d V  qdS )r      N)split).0cr   r   r   	<genexpr>y   s    z(GridFSStorage.listdir.<locals>.<genexpr>)r%   AssertionErrorr   r2   setlist)r    r'   r)   r$   Zsubcollectionsr   r   r   listdirq   s    
zGridFSStorage.listdirc             C   s   | j |\}}|j|djS )z9
        Returns the size of the file at `path`.
        )r$   )r%   r&   length)r    r'   r)   r$   r   r   r   size{   s    zGridFSStorage.sizec             C   s   | j d krtd| j|\}}y|j|djd}W nJ tk
r   yt|}W n tttfk
rl   d S X |j	|s|d S Y nX t
| j tt|S )Nz&This file is not accessible via a URL.)r$   r0   )r   
ValueErrorr%   r&   __getattr__r
   r   r   	TypeErrorr3   r   r   str)r    r   r)   r$   Zfile_oidr   r   r   url   s    


zGridFSStorage.urlc             C   s   | j |\}}|j|djS )zF
        Returns the datetime the file at `path` was created.
        )r$   )r%   r&   Zupload_date)r    r'   r)   r$   r   r   r   created_time   s    zGridFSStorage.created_timec             C   st   t jj|\}}t jj| j| j|jt j}|jt jdjd}t	| dsdddl
m} || j j| _t| j||fS )z`
        Returns a :class:`~gridfs.GridFS` using the sub-collection for
        `path`.
        r   _dbr   )connections)r   r'   r6   joinr   r   r   r   replacehasattrZ	django.dbrG   r   
connectionrF   r	   )r    r'   r$   Zcollection_namerG   r   r   r   r%      s    
zGridFSStorage._get_gridfsc             C   s   d S )Nr   )r    r   r   r   r   get_accessed_time   s    zGridFSStorage.get_accessed_timec             C   s   d S )Nr   )r    r   r   r   r   get_created_time   s    zGridFSStorage.get_created_timec             C   s   d S )Nr   )r    r   r   r   r   get_modified_time   s    zGridFSStorage.get_modified_timec             C   s   d S )Nr   )r    r   r   r   r   r'      s    zGridFSStorage.path)r   r   r   N)r"   )__name__
__module____qualname____doc__r!   r*   r/   r1   r3   r=   r?   rD   rE   r%   rL   rM   rN   r'   r   r   r   r   r      s      



r   )r   urllib.parser   Zbsonr   Zbson.errorsr   Zdjango.core.exceptionsr   Zdjango.core.files.storager   Zdjango.utils.deconstructr   Zdjango.utils.encodingr   r)   r	   r
   r   r   r   r   r   r   <module>   s   