<commit_before>from corrdb.common import logAccess, logStat, logTraffic, crossdomain, basicAuthSession
from corrdb.common.models import UserModel
from corrdb.common.models import ProjectModel
from corrdb.common.models import EnvironmentModel
from corrdb.common.models import RecordModel
from corrdb.common.models import RecordBodyModel
from corrdb.common.models import TrafficModel
from corrdb.common.models import StatModel
from flask.ext.stormpath import user
from flask.ext.stormpath import login_required
from flask.ext.api import status
import flask as fk
from cloud import app, cloud_response, storage_manager, access_manager, data_pop, CLOUD_URL, VIEW_HOST, VIEW_PORT, MODE, ACC_SEC, CNT_SEC
import datetime
import simplejson as json
import traceback
import smtplib
from email.mime.text import MIMEText
import mimetypes

@app.route(CLOUD_URL + '/private/record/remove/<record_id>', methods=['GET','POST','PUT','UPDATE','DELETE','POST', 'OPTIONS'])
@crossdomain(fk=fk, app=app, origin='*')
def record_remove(record_id):
    logTraffic(CLOUD_URL, endpoint='/private/record/remove/<record_id>')
    hash_session = basicAuthSession(fk.request)     
    if fk.request.method in ['GET', 'DELETE']:
        access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC)
        current_user = access_resp[1]
        if current_user is None:
            return fk.Response('Unauthorized action on this record.', status.HTTP_401_UNAUTHORIZED)
        else:
            try:
                logAccess(CLOUD_URL, 'cloud', '/private/record/remove/<record_id>')
                record = RecordModel.objects.with_id(record_id)
            except:
                print(str(traceback.print_exc()))
            if record is None:
                return fk.Response('Unable to find this record.', status.HTTP_404_NOT_FOUND)
                return fk.redirect('{0}:{1}/error/?code=204'.format(VIEW_HOST, VIEW_PORT))
            else:
                if record.project.owner == current_user or current_user.group == "admin":
                    storage_manager.delete_record_files(record, logStat)
                    logStat(deleted=True, record=record)
                    env_id = None
                    if record.environment:
                        env_id = str(record.environment.id)
                    record.delete()
                    if env_id:
                        try:
                            record.project.history.remove(env_id)
                            record.project.save()
                        except:
                            pass
                    return cloud_response(200, 'Deletion succeeded', 'The record %s was succesfully deleted.'%record_id)
                else:
                    return fk.Response('Unauthorized action on this record.', status.HTTP_401_UNAUTHORIZED)
    else:
       return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)

@app.route(CLOUD_URL + '/private/record/comment/<record_id>', methods=['GET','POST','PUT','UPDATE','DELETE','POST', 'OPTIONS'])
@crossdomain(fk=fk, app=app, origin='*')
def record_comment(record_id):
    logTraffic(CLOUD_URL, endpoint='/private/record/comment/<record_id>')
    hash_session = basicAuthSession(fk.request)
    if fk.request.method == 'POST':
        access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC)
        current_user = access_resp[1]
        if current_user is not None:
            try:
                logAccess(CLOUD_URL, 'cloud', '/private/record/comment/<record_id>')
                record = RecordModel.objects.with_id(record_id)
            except:
                print(str(traceback.print_exc()))
            if record is None:
                return fk.redirect('{0}:{1}/error/?code=204'.format(VIEW_HOST, VIEW_PORT))
            else:
                # if record.project.owner == current_user  or current_user.group == "admin":
                if fk.request.data:
                    data = json.loads(fk.request.data)
                    comment = data.get("comment", {})
                    if len(comment) != 0:
                        record.comments.append(comment)
                        record.save()
                        return fk.Response('Projject comment posted', status.HTTP_200_OK)
                    else:
                        return fk.redirect('{0}:{1}/error/?code=400'.format(VIEW_HOST, VIEW_PORT))
                else:
                    return fk.redirect('{0}:{1}/error/?code=415'.format(VIEW_HOST, VIEW_PORT))
                # else:
                #     return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT))
        else:
            return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT))
    else:
       return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT)) 

@app.route(CLOUD_URL + '/private/record/comments/<record_id>', methods=['GET','POST','PUT','UPDATE','DELETE','POST', 'OPTIONS'])
@crossdomain(fk=fk, app=app, origin='*')
def record_comments(record_id):
    logTraffic(CLOUD_URL, endpoint='/private/record/comments/<record_id>')
    hash_session = basicAuthSession(fk.request)
    if fk.request.method == 'GET':
        access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC)
        current_user = access_resp[1]
        if current_user is not None:
            try:
                logAccess(CLOUD_URL, 'cloud', '/private/record/comments/<record_id>')
                record = RecordModel.objects.with_id(record_id)
            except:
                print(str(traceback.print_exc()))
            if record is None or (record != None and record.access != 'public'):
                return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT))
            else:
                return fk.Response(json.dumps(record.comments, sort_keys=True, indent=4, separators=(',', ': ')), mimetype='application/json')
        else:
            return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT))
    else:
        return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT)) 

@app.route(CLOUD_URL + '/private/record/view/<record_id>', methods=['GET','POST','PUT','UPDATE','DELETE','POST', 'OPTIONS'])
@crossdomain(fk=fk, app=app, origin='*')
def record_view(record_id):
    logTraffic(CLOUD_URL, endpoint='/private/record/view/<record_id>')
    hash_session = basicAuthSession(fk.request)
    if fk.request.method == 'GET':
        access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC)
        current_user = access_resp[1]
        if current_user is not None:
            try:
                logAccess(CLOUD_URL, 'cloud', '/private/record/view/<record_id>')
                record = RecordModel.objects.with_id(record_id)
            except:
                print(str(traceback.print_exc()))
            if record is None:
                return fk.Response('Unable to find this record.', status.HTTP_404_NOT_FOUND)
            else:
                if record.project.owner == current_user or record.access == 'public' or current_user.group == "admin":
                    return fk.Response(record.to_json(), mimetype='application/json')
                else:
                    return fk.Response('Unauthorized action on this record.', status.HTTP_401_UNAUTHORIZED)
        else:
            return fk.Response('Unauthorized action on this record.', status.HTTP_401_UNAUTHORIZED)
    else:
        return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)

@app.route(CLOUD_URL + '/private/record/create/<project_id>', methods=['GET','POST','PUT','UPDATE','DELETE','POST', 'OPTIONS'])
@crossdomain(fk=fk, app=app, origin='*')
def record_create(project_id):
    logTraffic(CLOUD_URL, endpoint='/private/record/create/<project_id>')
    hash_session = basicAuthSession(fk.request)
    if fk.request.method == 'POST':
        access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC)
        current_user = access_resp[1]
        if current_user is None:
            return fk.Response('Unauthorized action on this endpoint.', status.HTTP_401_UNAUTHORIZED)
        else:
            logAccess(CLOUD_URL, 'cloud', '/private/record/create/<project_id>')
            try:
                project = ProjectModel.objects.with_id(project_id)
            except:
                print(str(traceback.print_exc()))
            if project is None:
                return fk.Response('Unable to find the referenced project.', status.HTTP_404_NOT_FOUND)
            else:
                if project.owner == current_user  or current_user.group == "admin":
                    if fk.request.data:
                            data = json.loads(fk.request.data)
                            try:
                                record = RecordModel(created_at=str(datetime.datetime.utcnow()), project=project)
                                tags = data.get("tags", "")
                                rationels = data.get("rationels", "")
                                status = data.get("status", "unknown")
                                content = data.get("content", "no content")
                                access = data.get("access", "public")
                                record.tags = [tags]
                                record.rationels = [rationels]
                                record.status = status
                                record.access = access
                                record.extend = {"uploaded":content}
                                record.save()
                                # project_content = {"project":json.loads(project.summary_json())}
                                # records = []
                                # for r in RecordModel.objects(project=project):
                                #     records.append(r)
                                # project_content["activity"] = {"number":len(records), "records":[{"id":str(record.id), "created":str(record.created_at), "updated":str(record.updated_at), "status":str(record.status)} for record in records]}
                                return cloud_response(201, 'Record successfully created.', json.loads(project.summary_json()))
                            except:<commit_msg>Record always redirected to latest env in project.<commit_after>from corrdb.common import logAccess, logStat, logTraffic, crossdomain, basicAuthSession
from corrdb.common.models import UserModel
from corrdb.common.models import ProjectModel
from corrdb.common.models import EnvironmentModel
from corrdb.common.models import RecordModel
from corrdb.common.models import RecordBodyModel
from corrdb.common.models import TrafficModel
from corrdb.common.models import StatModel
from flask.ext.stormpath import user
from flask.ext.stormpath import login_required
from flask.ext.api import status
import flask as fk
from cloud import app, cloud_response, storage_manager, access_manager, data_pop, CLOUD_URL, VIEW_HOST, VIEW_PORT, MODE, ACC_SEC, CNT_SEC
import datetime
import simplejson as json
import traceback
import smtplib
from email.mime.text import MIMEText
import mimetypes

@app.route(CLOUD_URL + '/private/record/remove/<record_id>', methods=['GET','POST','PUT','UPDATE','DELETE','POST', 'OPTIONS'])
@crossdomain(fk=fk, app=app, origin='*')
def record_remove(record_id):
    logTraffic(CLOUD_URL, endpoint='/private/record/remove/<record_id>')
    hash_session = basicAuthSession(fk.request)     
    if fk.request.method in ['GET', 'DELETE']:
        access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC)
        current_user = access_resp[1]
        if current_user is None:
            return fk.Response('Unauthorized action on this record.', status.HTTP_401_UNAUTHORIZED)
        else:
            try:
                logAccess(CLOUD_URL, 'cloud', '/private/record/remove/<record_id>')
                record = RecordModel.objects.with_id(record_id)
            except:
                print(str(traceback.print_exc()))
            if record is None:
                return fk.Response('Unable to find this record.', status.HTTP_404_NOT_FOUND)
                return fk.redirect('{0}:{1}/error/?code=204'.format(VIEW_HOST, VIEW_PORT))
            else:
                if record.project.owner == current_user or current_user.group == "admin":
                    storage_manager.delete_record_files(record, logStat)
                    logStat(deleted=True, record=record)
                    env_id = None
                    if record.environment:
                        env_id = str(record.environment.id)
                    record.delete()
                    if env_id:
                        try:
                            record.project.history.remove(env_id)
                            record.project.save()
                        except:
                            pass
                    return cloud_response(200, 'Deletion succeeded', 'The record %s was succesfully deleted.'%record_id)
                else:
                    return fk.Response('Unauthorized action on this record.', status.HTTP_401_UNAUTHORIZED)
    else:
       return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)

@app.route(CLOUD_URL + '/private/record/comment/<record_id>', methods=['GET','POST','PUT','UPDATE','DELETE','POST', 'OPTIONS'])
@crossdomain(fk=fk, app=app, origin='*')
def record_comment(record_id):
    logTraffic(CLOUD_URL, endpoint='/private/record/comment/<record_id>')
    hash_session = basicAuthSession(fk.request)
    if fk.request.method == 'POST':
        access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC)
        current_user = access_resp[1]
        if current_user is not None:
            try:
                logAccess(CLOUD_URL, 'cloud', '/private/record/comment/<record_id>')
                record = RecordModel.objects.with_id(record_id)
            except:
                print(str(traceback.print_exc()))
            if record is None:
                return fk.redirect('{0}:{1}/error/?code=204'.format(VIEW_HOST, VIEW_PORT))
            else:
                # if record.project.owner == current_user  or current_user.group == "admin":
                if fk.request.data:
                    data = json.loads(fk.request.data)
                    comment = data.get("comment", {})
                    if len(comment) != 0:
                        record.comments.append(comment)
                        record.save()
                        return fk.Response('Projject comment posted', status.HTTP_200_OK)
                    else:
                        return fk.redirect('{0}:{1}/error/?code=400'.format(VIEW_HOST, VIEW_PORT))
                else:
                    return fk.redirect('{0}:{1}/error/?code=415'.format(VIEW_HOST, VIEW_PORT))
                # else:
                #     return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT))
        else:
            return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT))
    else:
       return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT)) 

@app.route(CLOUD_URL + '/private/record/comments/<record_id>', methods=['GET','POST','PUT','UPDATE','DELETE','POST', 'OPTIONS'])
@crossdomain(fk=fk, app=app, origin='*')
def record_comments(record_id):
    logTraffic(CLOUD_URL, endpoint='/private/record/comments/<record_id>')
    hash_session = basicAuthSession(fk.request)
    if fk.request.method == 'GET':
        access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC)
        current_user = access_resp[1]
        if current_user is not None:
            try:
                logAccess(CLOUD_URL, 'cloud', '/private/record/comments/<record_id>')
                record = RecordModel.objects.with_id(record_id)
            except:
                print(str(traceback.print_exc()))
            if record is None or (record != None and record.access != 'public'):
                return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT))
            else:
                return fk.Response(json.dumps(record.comments, sort_keys=True, indent=4, separators=(',', ': ')), mimetype='application/json')
        else:
            return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT))
    else:
        return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT)) 

@app.route(CLOUD_URL + '/private/record/view/<record_id>', methods=['GET','POST','PUT','UPDATE','DELETE','POST', 'OPTIONS'])
@crossdomain(fk=fk, app=app, origin='*')
def record_view(record_id):
    logTraffic(CLOUD_URL, endpoint='/private/record/view/<record_id>')
    hash_session = basicAuthSession(fk.request)
    if fk.request.method == 'GET':
        access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC)
        current_user = access_resp[1]
        if current_user is not None:
            try:
                logAccess(CLOUD_URL, 'cloud', '/private/record/view/<record_id>')
                record = RecordModel.objects.with_id(record_id)
            except:
                print(str(traceback.print_exc()))
            if record is None:
                return fk.Response('Unable to find this record.', status.HTTP_404_NOT_FOUND)
            else:
                if record.project.owner == current_user or record.access == 'public' or current_user.group == "admin":
                    return fk.Response(record.to_json(), mimetype='application/json')
                else:
                    return fk.Response('Unauthorized action on this record.', status.HTTP_401_UNAUTHORIZED)
        else:
            return fk.Response('Unauthorized action on this record.', status.HTTP_401_UNAUTHORIZED)
    else:
        return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)

@app.route(CLOUD_URL + '/private/record/create/<project_id>', methods=['GET','POST','PUT','UPDATE','DELETE','POST', 'OPTIONS'])
@crossdomain(fk=fk, app=app, origin='*')
def record_create(project_id):
    logTraffic(CLOUD_URL, endpoint='/private/record/create/<project_id>')
    hash_session = basicAuthSession(fk.request)
    if fk.request.method == 'POST':
        access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC)
        current_user = access_resp[1]
        if current_user is None:
            return fk.Response('Unauthorized action on this endpoint.', status.HTTP_401_UNAUTHORIZED)
        else:
            logAccess(CLOUD_URL, 'cloud', '/private/record/create/<project_id>')
            try:
                project = ProjectModel.objects.with_id(project_id)
            except:
                print(str(traceback.print_exc()))
            if project is None:
                return fk.Response('Unable to find the referenced project.', status.HTTP_404_NOT_FOUND)
            else:
                if project.owner == current_user  or current_user.group == "admin":
                    if fk.request.data:
                            data = json.loads(fk.request.data)
                            try:
                                record = RecordModel(created_at=str(datetime.datetime.utcnow()), project=project)
                                tags = data.get("tags", "")
                                rationels = data.get("rationels", "")
                                status = data.get("status", "unknown")
                                content = data.get("content", "no content")
                                access = data.get("access", "public")
                                record.tags = [tags]
                                record.rationels = [rationels]
                                record.status = status
                                record.access = access
                                record.extend = {"uploaded":content}
                                record.save()
                                if len(project.history) > 0:
                                    head = project.history[-1]
                                    env = EnvironmentModel.objects.with_id(head)
                                    if env:
                                        record.environment = env
                                        record.save()
                                # project_content = {"project":json.loads(project.summary_json())}
                                # records = []
                                # for r in RecordModel.objects(project=project):
                                #     records.append(r)
                                # project_content["activity"] = {"number":len(records), "records":[{"id":str(record.id), "created":str(record.created_at), "updated":str(record.updated_at), "status":str(record.status)} for record in records]}
                                return cloud_response(201, 'Record successfully created.', json.loads(project.summary_json()))
                            except:
{'fraction_non_alphanumeric': 0.11882694044393745, 'fraction_numerical': 0.009220970578449545, 'mean_word_length': 2.1534772182254196, 'pattern_counts': {'":': 24, '<': 33, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7305174', 'n_tokens_mistral': 6093, 'n_tokens_neox': 5835, 'n_words': 1108}