<commit_before>#!/usr/bin/env python
# coding=utf-8

from datetime import date
import time
import hashlib
import os
import imghdr
import mimetypes

from webapp.web import Application, BaseHandler
from model import dbapi


URLS = (
    ("/", "IndexHandler"),
    ("/hello/(.*)", "Hello"),
    ("/register?", "RegisterHandler"),
    ("/user", "UserHandler"),
    ("/signin", "SigninHandler"),
    ("/signout", "SignoutHandler"),
    ("/upload", "UploadHandler"),
    ("/avatar/(.*)", "AvatarHandler"),
    ("/error", "ErrorHandler")
)

class ErrorHandler(BaseHandler):
    def get(self):
        self.write("Page Not Exist")
    

class AvatarHandler(BaseHandler):
    def get(self, name):
        fullpath = "images/" + name
        filetype = "jpeg"
        if os.path.isfile("images/"+name):
            with open(fullpath, "rb") as f:
                image = f.read()
                length = len(image)
                self.set_header("Content-Length", length)
                filetype = imghdr.what(fullpath)
            mtype = mimetypes.types_map.get("."+filetype, "image/jpeg")
            self.set_header("Content-Type", mtype)
            self.write(image)
        else:
            self.redirect("/error")

    def post(self, name):
        self.get(name)

class UploadHandler(BaseHandler):
    def check(self):
        email = self.get_secure_cookie("email")
        user = dbapi.User()
        if email and user.get_user(email) == 0:
            profile = user.get_user_all(email)
            if profile:
                self.id = profile[0]
                self.time = profile[4]
                self.email = email
        else:
            self.clear_cookies()
            self.redirect("/")

    def post(self):
        self.check()
        fileitem = self.request.files["filename"]
        if fileitem.filename:
            #fn = os.path.basename(fileitem.filename)
            #filetype = imghdr.what(fileitem.file)
            #if filetype is None:
            #    self.redirect("user")
            m = hashlib.md5()
            m.update(self.email)
            email_md5 = m.hexdigest()
            open("images/" + email_md5 , "wb").write(fileitem.file.read())
            #(x, y, z) = imgfile.getsizes(fileitem.file)
            #thumbnail = imageop.scale(fileitem.file, z, x, y, 64, 64)
            #open("images/" + email_md5+"_small"+filetype, "wb").write(thumbnail.read())
            self.redirect("user")
        else:
            self.redirect("user")


class SignoutHandler(BaseHandler):

    def get(self):
        self.clear_cookies()
        self.redirect("/")

    def post(self):
        self.get()

class UserHandler(BaseHandler):

    def check(self):
        email = self.get_secure_cookie("email")
        user = dbapi.User()
        if email and user.get_user(email) == 0:
            profile = user.get_user_all(email)
            if profile:
                self.time = profile[4]
                self.email = email
        else:
            self.clear_cookies()
            self.redirect("/")

    def get(self):
        self.check()
    
        m = hashlib.md5()
        m.update(self.email)
        email_md5 = m.hexdigest()
        fn = "images/" + email_md5
        if os.path.isfile(fn):
            imagetag =  """<img src="%s" height="400"  class="rounded" alt="Upload Image Below">""" % fn
        else:
            imagetag = """<p>Avatar Not Available. Upload One!</p>"""
        params = {'name': self.email, 'time': self.time, 'image': imagetag}
        body = self.wrap_html('templates/user.html', params)
        self.write(body)


class SigninHandler(BaseHandler):

    def post(self):
        email = self.get_arg("email")
        password = self.get_arg("password")
        if email:
            user = dbapi.User()
            if user.check_user(email, password) == 0:
                self.set_secure_cookie('email', str(email))
                self.redirect("/user")

        self.redirect("/")


class RegisterHandler(BaseHandler):
<commit_msg>default avatar added<commit_after>#!/usr/bin/env python
# coding=utf-8

from datetime import date
import time
import hashlib
import os
import imghdr
import mimetypes

from webapp.web import Application, BaseHandler
from model import dbapi


URLS = (
    ("/", "IndexHandler"),
    ("/hello/(.*)", "Hello"),
    ("/register?", "RegisterHandler"),
    ("/user", "UserHandler"),
    ("/signin", "SigninHandler"),
    ("/signout", "SignoutHandler"),
    ("/upload", "UploadHandler"),
    ("/avatar/(.*)", "AvatarHandler"),
    ("/error", "ErrorHandler")
)


class ErrorHandler(BaseHandler):

    def get(self):
        self.write("Page Not Exist")


class AvatarHandler(BaseHandler):

    def get(self, name):
        fullpath = "images/" + name
        filetype = "jpeg"
        if os.path.isfile("images/" + name):
            with open(fullpath, "rb") as f:
                image = f.read()
                length = len(image)
                self.set_header("Content-Length", length)
                filetype = imghdr.what(fullpath)
            mtype = mimetypes.types_map.get("." + filetype, "image/jpeg")
            self.set_header("Content-Type", mtype)
            self.write(image)
        else:
            self.redirect("/error")

    def post(self, name):
        self.get(name)


class UploadHandler(BaseHandler):

    def check(self):
        email = self.get_secure_cookie("email")
        user = dbapi.User()
        if email and user.get_user(email) == 0:
            profile = user.get_user_all(email)
            if profile:
                self.id = profile[0]
                self.time = profile[4]
                self.email = email
        else:
            self.clear_cookies()
            self.redirect("/")

    def post(self):
        self.check()
        fileitem = self.request.files["filename"]
        if fileitem.filename:
            #fn = os.path.basename(fileitem.filename)
            #filetype = imghdr.what(fileitem.file)
            # if filetype is None:
            #    self.redirect("user")
            m = hashlib.md5()
            m.update(self.email)
            email_md5 = m.hexdigest()
            open("images/" + email_md5, "wb").write(fileitem.file.read())
            #(x, y, z) = imgfile.getsizes(fileitem.file)
            #thumbnail = imageop.scale(fileitem.file, z, x, y, 64, 64)
            #open("images/" + email_md5+"_small"+filetype, "wb").write(thumbnail.read())
            self.redirect("user")
        else:
            self.redirect("user")


class SignoutHandler(BaseHandler):

    def get(self):
        self.clear_cookies()
        self.redirect("/")

    def post(self):
        self.get()


class UserHandler(BaseHandler):

    def check(self):
        email = self.get_secure_cookie("email")
        user = dbapi.User()
        if email and user.get_user(email) == 0:
            profile = user.get_user_all(email)
            if profile:
                self.time = profile[4]
                self.email = email
        else:
            self.clear_cookies()
            self.redirect("/")

    def get(self):
        self.check()

        m = hashlib.md5()
        m.update(self.email)
        email_md5 = m.hexdigest()
        fn = "images/" + email_md5
        if os.path.isfile(fn):
            imagetag =  """<img src="%s" height="400"  class="rounded" alt="Upload Image Below">""" % fn
        else:
            imagetag = """<p>Avatar Not Available. Upload One!</p>""" + \
                """<img src="static/default.jpg" height="400"  class="rounded" alt="Upload Image Below">"""
        params = {'name': self.email, 'time': self.time, 'image': imagetag}
        body = self.wrap_html('templates/user.html', params)
        self.write(body)


class SigninHandler(BaseHandler):

    def post(self):
        email = self.get_arg("email")
        password = self.get_arg("password")
        if email:
            user = dbapi.User()
            if user.check_user(email, password) == 0:
                self.set_secure_cookie('email', str(email))
                self.redirect("/user")

        self.redirect("/")


class RegisterHandler(BaseHandler):

{'fraction_non_alphanumeric': 0.14477080004915815, 'fraction_numerical': 0.0055302937200442425, 'mean_word_length': 1.9985261606484894, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '1602985', 'n_tokens_mistral': 2529, 'n_tokens_neox': 2419, 'n_words': 511}