<commit_before># -*- coding: utf-8 -*-

from datetime import datetime, timedelta

from google.appengine.api import users
from google.appengine.api import memcache
from google.appengine.api import images

from aha.widget.field import *
from aha.widget.form import Form, Fieldset, ULFieldset, ListForm, TableForm
from aha.modelcontroller.formcontrol import FormControl
FC = FormControl
from formencode import validators as v

from aha.modelcontroller.crudcontroller import ModelCRUDController
from aha.controller.decorator import authenticate, expose, cache

from aha.controller.util import get_current_user

from model import Image, Path
from page import ContentBase
from mimetypes import guess_type

from page import ContentEditHandler, ContentAddHandler

def detect_imagetype(image):
    if image[6:10] == 'JFIF': return 'image/jpeg'
    if image[0:3] == 'GIF': return 'image/gif'
    if image[1:4] == 'PNG': return 'image/png'

class ImageEditHandler(ContentEditHandler):
    FC = FormControl()

    def get_value(self, controller):
        """
        A method to obtain value from db, to supply to form fields.
        """
        obj = controller.content
        d = {}
        """
        for f in controller.form:
            n = f.get_name()
            if n != 'body' and hasattr(obj, n):
                d[n] = getattr(obj, n)
        d['body'] = controller.path_obj.get_path()
        """
        return d

    def make_form(self, controller):
        """
        A method to create edit form.
        """
        form = controller.get_form('edit')
        return form
        key = controller.params.get('id', '')
        form.values['body'] = controller.path_obj.get_path()
        form.set_action(controller.BASEPATH+'/edit/'+key)
        return form

    @FC.handle_state(FormControl.SUCCESS)
    def process_data(self, controller):
        from google.appengine.api import memcache
        memcache.flush_all()

        obj = controller.content
        v = controller.form.validate_result
        replace_body = True
        for k in v:
            if k == 'body' and not v[k]:
                replace_body = False
            else:
                setattr(obj, k, v[k])
        if replace_body:
            f = controller.request.body_file.vars["body"]
            v['content_type'] = detect_imagetype(v['body'])
            i = images.Image(v['body'])
            v['width'], v['height'] = i.width, i.height
            v['filename'] = f.filename
        if not v.get('name', None) and v.get('filename'):
            v['name'] = v['filename']
        if not v.get('title', None):
            v['title'] = v['name']
        obj.modified_at = datetime.now()
        obj.put()
        obj.sync_path(obj.get_path())

        controller.set_state(FC.INITIAL)
        controller.redirect(obj.get_parent().get_path()+'/list')


class ImageController(ContentBase):
    """
    The controller for image
    """
    MODEL = Image

    @expose
    @cache()
    def index(self):
        obj = self.content
        hdrs = {'Content-Type':str(obj.content_type)}
        self.view.render(obj.body, hdrs)
        self.has_rendered = True


    @classmethod
    def add_new_object(cls, v, ins):
        """
        A method to obtain new object
        """
        from aha import Config
        config = Config()
        f = ins.request.body_file.vars["body"]
        v['content_type'] = detect_imagetype(v['body'])
        i = images.Image(v['body'])
        v['width'], v['height'] = i.width, i.height
        v['filename'] = f.filename
        if not v.get('name', None):
            v['name'] = v['filename']
        if not v.get('title', None):
            v['title'] = v['name']
        cu = config.auth_obj().get_user(ins)
        v['creator'] = cu.get('nickname', '') or cu.get('nickname', '')<commit_msg>added feature of write expires header<commit_after># -*- coding: utf-8 -*-

from datetime import datetime, timedelta

from google.appengine.api import users
from google.appengine.api import memcache
from google.appengine.api import images

from aha.widget.field import *
from aha.widget.form import Form, Fieldset, ULFieldset, ListForm, TableForm
from aha.modelcontroller.formcontrol import FormControl
FC = FormControl
from formencode import validators as v

from aha.modelcontroller.crudcontroller import ModelCRUDController
from aha.controller.decorator import authenticate, expose, cache

from aha.controller.util import get_current_user

from model import Image, Path
from blogbase import BlogContentBase
from mimetypes import guess_type

from page import ContentEditHandler, ContentAddHandler

def detect_imagetype(image):
    if image[6:10] == 'JFIF': return 'image/jpeg'
    if image[0:3] == 'GIF': return 'image/gif'
    if image[1:4] == 'PNG': return 'image/png'

class ImageEditHandler(ContentEditHandler):
    FC = FormControl()

    def get_value(self, controller):
        """
        A method to obtain value from db, to supply to form fields.
        """
        obj = controller.content
        d = {}
        """
        for f in controller.form:
            n = f.get_name()
            if n != 'body' and hasattr(obj, n):
                d[n] = getattr(obj, n)
        d['body'] = controller.path_obj.get_path()
        """
        return d

    def make_form(self, controller):
        """
        A method to create edit form.
        """
        form = controller.get_form('edit')
        return form
        key = controller.params.get('id', '')
        form.values['body'] = controller.path_obj.get_path()
        form.set_action(controller.BASEPATH+'/edit/'+key)
        return form

    @FC.handle_state(FormControl.SUCCESS)
    def process_data(self, controller):
        from google.appengine.api import memcache
        memcache.flush_all()

        obj = controller.content
        v = controller.form.validate_result
        replace_body = True
        for k in v:
            if k == 'body' and not v[k]:
                replace_body = False
            else:
                setattr(obj, k, v[k])
        if replace_body:
            f = controller.request.body_file.vars["body"]
            v['content_type'] = detect_imagetype(v['body'])
            i = images.Image(v['body'])
            v['width'], v['height'] = i.width, i.height
            v['filename'] = f.filename
        if not v.get('name', None) and v.get('filename'):
            v['name'] = v['filename']
        if not v.get('title', None):
            v['title'] = v['name']
        obj.modified_at = datetime.now()
        obj.put()
        obj.sync_path(obj.get_path())

        controller.set_state(FC.INITIAL)
        controller.redirect(obj.get_parent().get_path()+'/list')


class ImageController(BlogContentBase):
    """
    The controller for image
    """
    MODEL = Image

    @expose
    @cache()
    def index(self):
        self.set_common_headers()

        obj = self.content
        hdrs = {'Content-Type':str(obj.content_type)}
        self.view.render(obj.body, hdrs)
        self.has_rendered = True


    @classmethod
    def add_new_object(cls, v, ins):
        """
        A method to obtain new object
        """
        from aha import Config
        config = Config()
        f = ins.request.body_file.vars["body"]
        v['content_type'] = detect_imagetype(v['body'])
        i = images.Image(v['body'])
        v['width'], v['height'] = i.width, i.height
        v['filename'] = f.filename
        if not v.get('name', None):
            v['name'] = v['filename']
        if not v.get('title', None):
            v['title'] = v['name']
        cu = config.auth_obj().get_user(ins)
        v['creator'] = cu.get('nickname', '') or cu.get('nickname', '')
{'fraction_non_alphanumeric': 0.1279054668224906, 'fraction_numerical': 0.0020776522529541617, 'mean_word_length': 2.3736311870346034, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5712832', 'n_tokens_mistral': 2318, 'n_tokens_neox': 2266, 'n_words': 576}