<commit_before>from pyramid.response import Response
from pyramid.view import view_config
from cornice import Service
import logging
import sqlalchemy.exc
from datetime import datetime
from io import BytesIO

from pi_director.models.models import (
    DBSession,
    MyModel,
    Screenshot
    )

from pi_director.controllers.user_controls import make_an_admin

screenshot = Service(name='pi_screen', path='/api/v1/screen/{uid}', description="Service to handle insertion and deletion of screenshots")

ping = Service(name='pi_ping', path='/api/v1/ping/{uid}', description="Enable tracking of pi last seen")

authme = Service(name='user_create', path='/api/v1/authorize/{email}', description="Create new admin if none exists already")

@ping.get(permission='anon')
def view_api_ping(request):
    uid = request.matchdict['uid']

    now=datetime.now()

    row=DBSession.query(MyModel).filter(MyModel.uuid==uid).first()
    if row==None:
        row=MyModel()
        row.uuid=uid
        row.url="http://www.stackexchange.com"
        row.landscape=True
        row.description=""

    row.lastseen=now
    DBSession.add(row)
    DBSession.flush()


@screenshot.get(permission='admin')
def view_api_screenshow_show(request):
    uid=request.matchdict['uid']
    shot=DBSession.query(Screenshot).filter(Screenshot.uuid==uid).first()
    with BytesIO() as ss:
        ss.write(shot.image)
        response = Response(content_type='image/png',content_length=len(ss.getvalue()),body=ss.getvalue())
        return response

@screenshot.post(permission='anon')
def view_api_screenshot_save(request):
    uid=request.matchdict['uid']
    imgblob=request.POST['screenshot'].file

    '''first, delete previous screenshot'''
    DBSession.query(Screenshot).filter(Screenshot.uuid==uid).delete()

    '''Now, lets make a new screenshot'''
    foo=Screenshot()
    foo.uuid=uid
    foo.image=imgblob.read()
    DBSession.add(foo)
    DBSession.flush()


@authme.get(permission='anon')
def view_api_create_user(request):

    return make_an_admin(request)
<commit_msg>Create new api ping route to include IP address<commit_after>from pyramid.response import Response
from pyramid.view import view_config
from cornice import Service
import logging
import sqlalchemy.exc
from datetime import datetime
from io import BytesIO

from pi_director.models.models import (
    DBSession,
    MyModel,
    Screenshot
    )

from pi_director.controllers.user_controls import make_an_admin

screenshot = Service(name='pi_screen', path='/api/v1/screen/{uid}', description="Service to handle insertion and deletion of screenshots")

ping = Service(name='pi_ping', path='/api/v1/ping/{uid}', description="Enable tracking of pi last seen")
ping2 = Service(name='pi_pingv2', path='/api/v2/ping/{uid}/{ip}', description="Enable tracking of pi last seen and its ip address")

authme = Service(name='user_create', path='/api/v1/authorize/{email}', description="Create new admin if none exists already")

@ping2.get(permission='anon')
def view_api_ping_v2(request):
    uid = request.matchdict['uid']
    ip = request.matchdict['ip']

    now=datetime.now()

    row=DBSession.query(MyModel).filter(MyModel.uuid==uid).first()
    if row==None:
        row=MyModel()
        row.uuid=uid
        row.url="http://www.stackexchange.com"
        row.landscape=True
        row.description=""

    row.lastseen=now
    row.ip=ip
    DBSession.add(row)
    DBSession.flush()

@ping.get(permission='anon')
def view_api_ping(request):
    uid = request.matchdict['uid']

    now=datetime.now()

    row=DBSession.query(MyModel).filter(MyModel.uuid==uid).first()
    if row==None:
        row=MyModel()
        row.uuid=uid
        row.url="http://www.stackexchange.com"
        row.landscape=True
        row.description=""

    row.lastseen=now
    DBSession.add(row)
    DBSession.flush()


@screenshot.get(permission='admin')
def view_api_screenshow_show(request):
    uid=request.matchdict['uid']
    shot=DBSession.query(Screenshot).filter(Screenshot.uuid==uid).first()
    with BytesIO() as ss:
        ss.write(shot.image)
        response = Response(content_type='image/png',content_length=len(ss.getvalue()),body=ss.getvalue())
        return response

@screenshot.post(permission='anon')
def view_api_screenshot_save(request):
    uid=request.matchdict['uid']
    imgblob=request.POST['screenshot'].file

    '''first, delete previous screenshot'''
    DBSession.query(Screenshot).filter(Screenshot.uuid==uid).delete()

    '''Now, lets make a new screenshot'''
    foo=Screenshot()
    foo.uuid=uid
    foo.image=imgblob.read()
    DBSession.add(foo)
    DBSession.flush()


@authme.get(permission='anon')
def view_api_create_user(request):

    return make_an_admin(request)

{'fraction_non_alphanumeric': 0.13376869601853802, 'fraction_numerical': 0.0023172530018959344, 'mean_word_length': 4.322869955156951, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, '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': '7453177', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1516, 'n_words': 283}