# Path: cms/filetempstore.py
# class MongoFileUploadTempStore(object):
# 
#     MAPPING_NAME = "_tmp_files"
# 
#     # FIXME: Should the purge really be done on every instantiation, or would
#     # it be better to have a nightly job call purge(delete_cutoff) where delete_cutoff
#     # is a timedelta?
#     def __init__(self, request, collection='fs', delete_cutoff_days=1):
#         self.gridfs = get_gridfs(request, collection)
#         self.request = request
#         self.delete_cutoff_days = datetime.timedelta(delete_cutoff_days)
#         self.purge()
# 
#     def purge(self):
#         cutoff = dateutil.utcnow() - self.delete_cutoff_days
#         old_ids = []
#         for item in self.gridfs._GridFS__files.find({'parents':[], 'uploadDate':{'$lt':cutoff}}, fields=[]):
#             old_ids.append(item['_id'])
#         if old_ids:
#             log.debug("purging old files: %s" % repr(old_ids))
#         for id in old_ids:
#             self.gridfs.delete(id)
# 
#     def get(self, name, default=None):
#         #log.debug("get(%s, %s)" % (repr(name), repr(default)))
#         files_dict = self.get_file_mapping()
#         _id = files_dict.get(name)
#         if _id:
#             try:
#                 result = self.gridfs.get(_id)
#             except NoFile, e:
#                 return default
#             return filedict(
#                 fp=result,
#                 mimetype=result.content_type,
#                 uid=name,
#                 preview_url=self.preview_url_for_id(_id),
#                 filename=result.name,
#                 size=result.length,
#                 _id=_id,
#                 parents=result.parents
#             )
#         return default
# 
#     def __setitem__(self, name, value):
#         #log.debug("__setitem__(%s, %s)" % (repr(name), repr(value)))
#         if value.has_key('_id'):
#             _id = value['_id']
#         else:
#             _id = self.gridfs.put(value['fp'], filename=value['filename'], contentType=value['mimetype'], parents=[])
#             value['_id'] = _id
#             value['parents'] = []
#             value['preview_url'] = self.preview_url_for_id(_id)
#         files_dict = self.get_file_mapping()
#         files_dict[name] = _id
#         setattr(self.request, self.MAPPING_NAME, files_dict)
# 
#     def __getitem__(self, name):
#         #log.debug("__getitem__(%s)" % repr(name))
#         return self.get(name)
# 
#     def __contains__(self, name):
#         #log.debug("__contains__(%s)" % repr(name))
#         files_dict = self.get_file_mapping()
#         _id = files_dict.get(name)
#         if _id:
#             return self.gridfs.exists(_id)
#         return False
# 
#     def preview_url(self, uid):
#         #log.debug("preview_url(%s)" % repr(uid))
#         _id = self.get_file_mapping().get(uid)
#         return self.preview_url_for_id(_id)
# 
#     def preview_url_for_id(self, _id):
#         # There should be a view to handle these urls.
#         return "%s/serve_file/%s" % (self.request.application_url, str(_id))
# 
#     def get_file_mapping(self):
#         return getattr(self.request, self.MAPPING_NAME, {})
# 
#     def serialize_file_mapping(self):
#         items = []
#         for (uid, _id) in self.get_file_mapping().items():
#             items.append("%s:%s" % (uid, str(_id)))
#         return ",".join(items)
# 
#     def _deserialize_file_mapping(self, serialized_string):
#         """ Deserialize a string such as returned from serialize_file_mapping().
#         """
#         files_dict = {}
#         if serialized_string:
#             for item in serialized_string.split(','):
#                 (uid, _id) = item.split(':', 1)
#                 files_dict[uid] = ObjectId(_id)
#         return files_dict
# 
#     def update_file_mapping_from_serialized(self, serialized_string):
#         deserialized_dict = self._deserialize_file_mapping(serialized_string)
#         files_dict = self.get_file_mapping()
#         for (uid, _id) in deserialized_dict.items():
#             # If the current mapping has this uid, the user must have uploaded a new file,
#             # so skip the uid (otherwise it would revert to the original file).
#             if not files_dict.has_key(uid):
#                 files_dict[uid] = _id
#         setattr(self.request, self.MAPPING_NAME, files_dict)


import deform
from cms.filetempstore import MongoFileUploadTempStore
# Some pre-configured widgets for use with cms.resources

def get_html_widget(**kwargs):
    return deform.widget.RichTextWidget(width='100%', theme='advanced', **kwargs)

# Be sure site includes a css rule for .wide_input { width: 100%; }
def get_wide_text_widget(**kwargs):
    return deform.widget.TextInputWidget(css_class="wide_input", **kwargs)

def get_wide_textarea_widget(**kwargs):
    return deform.widget.TextAreaWidget(css_class="wide_input", **kwargs)

def get_fileupload_widget(request, **kwargs):
    return deform.widget.FileUploadWidget(_get_fileupload_temp_store(request), **kwargs)

def _get_fileupload_temp_store(request):
    if request is None: return None
    if not hasattr(request, '_tmpstore'):

# Path: cms/filetempstore.py
# class MongoFileUploadTempStore(object):
# 
#     MAPPING_NAME = "_tmp_files"
# 
#     # FIXME: Should the purge really be done on every instantiation, or would
#     # it be better to have a nightly job call purge(delete_cutoff) where delete_cutoff
#     # is a timedelta?
#     def __init__(self, request, collection='fs', delete_cutoff_days=1):
#         self.gridfs = get_gridfs(request, collection)
#         self.request = request
#         self.delete_cutoff_days = datetime.timedelta(delete_cutoff_days)
#         self.purge()
# 
#     def purge(self):
#         cutoff = dateutil.utcnow() - self.delete_cutoff_days
#         old_ids = []
#         for item in self.gridfs._GridFS__files.find({'parents':[], 'uploadDate':{'$lt':cutoff}}, fields=[]):
#             old_ids.append(item['_id'])
#         if old_ids:
#             log.debug("purging old files: %s" % repr(old_ids))
#         for id in old_ids:
#             self.gridfs.delete(id)
# 
#     def get(self, name, default=None):
#         #log.debug("get(%s, %s)" % (repr(name), repr(default)))
#         files_dict = self.get_file_mapping()
#         _id = files_dict.get(name)
#         if _id:
#             try:
#                 result = self.gridfs.get(_id)
#             except NoFile, e:
#                 return default
#             return filedict(
#                 fp=result,
#                 mimetype=result.content_type,
#                 uid=name,
#                 preview_url=self.preview_url_for_id(_id),
#                 filename=result.name,
#                 size=result.length,
#                 _id=_id,
#                 parents=result.parents
#             )
#         return default
# 
#     def __setitem__(self, name, value):
#         #log.debug("__setitem__(%s, %s)" % (repr(name), repr(value)))
#         if value.has_key('_id'):
#             _id = value['_id']
#         else:
#             _id = self.gridfs.put(value['fp'], filename=value['filename'], contentType=value['mimetype'], parents=[])
#             value['_id'] = _id
#             value['parents'] = []
#             value['preview_url'] = self.preview_url_for_id(_id)
#         files_dict = self.get_file_mapping()
#         files_dict[name] = _id
#         setattr(self.request, self.MAPPING_NAME, files_dict)
# 
#     def __getitem__(self, name):
#         #log.debug("__getitem__(%s)" % repr(name))
#         return self.get(name)
# 
#     def __contains__(self, name):
#         #log.debug("__contains__(%s)" % repr(name))
#         files_dict = self.get_file_mapping()
#         _id = files_dict.get(name)
#         if _id:
#             return self.gridfs.exists(_id)
#         return False
# 
#     def preview_url(self, uid):
#         #log.debug("preview_url(%s)" % repr(uid))
#         _id = self.get_file_mapping().get(uid)
#         return self.preview_url_for_id(_id)
# 
#     def preview_url_for_id(self, _id):
#         # There should be a view to handle these urls.
#         return "%s/serve_file/%s" % (self.request.application_url, str(_id))
# 
#     def get_file_mapping(self):
#         return getattr(self.request, self.MAPPING_NAME, {})
# 
#     def serialize_file_mapping(self):
#         items = []
#         for (uid, _id) in self.get_file_mapping().items():
#             items.append("%s:%s" % (uid, str(_id)))
#         return ",".join(items)
# 
#     def _deserialize_file_mapping(self, serialized_string):
#         """ Deserialize a string such as returned from serialize_file_mapping().
#         """
#         files_dict = {}
#         if serialized_string:
#             for item in serialized_string.split(','):
#                 (uid, _id) = item.split(':', 1)
#                 files_dict[uid] = ObjectId(_id)
#         return files_dict
# 
#     def update_file_mapping_from_serialized(self, serialized_string):
#         deserialized_dict = self._deserialize_file_mapping(serialized_string)
#         files_dict = self.get_file_mapping()
#         for (uid, _id) in deserialized_dict.items():
#             # If the current mapping has this uid, the user must have uploaded a new file,
#             # so skip the uid (otherwise it would revert to the original file).
#             if not files_dict.has_key(uid):
#                 files_dict[uid] = _id
#         setattr(self.request, self.MAPPING_NAME, files_dict)


# Path: cms/resources/widgets.py
import deform
from cms.filetempstore import MongoFileUploadTempStore

# Some pre-configured widgets for use with cms.resources

def get_html_widget(**kwargs):
    return deform.widget.RichTextWidget(width='100%', theme='advanced', **kwargs)

# Be sure site includes a css rule for .wide_input { width: 100%; }
def get_wide_text_widget(**kwargs):
    return deform.widget.TextInputWidget(css_class="wide_input", **kwargs)

def get_wide_textarea_widget(**kwargs):
    return deform.widget.TextAreaWidget(css_class="wide_input", **kwargs)

def get_fileupload_widget(request, **kwargs):
    return deform.widget.FileUploadWidget(_get_fileupload_temp_store(request), **kwargs)

def _get_fileupload_temp_store(request):
    if request is None: return None
    if not hasattr(request, '_tmpstore'):

        tmpstore = MongoFileUploadTempStore(request)