<s> import functools <EOL> import httplib as http <EOL> import datetime <EOL> import itertools <EOL> from dateutil . parser import parse as parse_date <EOL> from flask import request , redirect <EOL> from modularodm import Q <EOL> from modularodm . exceptions import ValidationValueError <EOL> from framework . mongo import database <EOL> from framework . mongo . utils import get_or_http_error , autoload <EOL> from framework . exceptions import HTTPError <EOL> from framework . status import push_status_message <EOL> from website . exceptions import NodeStateError <EOL> from website . util . permissions import ADMIN <EOL> from website . project . decorators import ( <EOL> must_be_valid_project , <EOL> must_have_permission , <EOL> http_error_if_disk_saving_mode <EOL> ) <EOL> from website import language , settings <EOL> from website . models import NodeLog <EOL> from website . prereg import utils as prereg_utils <EOL> from website . project import utils as project_utils <EOL> from website . project . model import MetaSchema , DraftRegistration <EOL> from website . project . metadata . schemas import ACTIVE_META_SCHEMAS <EOL> from website . project . metadata . utils import serialize_meta_schema , serialize_draft_registration <EOL> from website . project . utils import serialize_node <EOL> from website . util import rapply <EOL> from website . util . sanitize import strip_html <EOL> get_schema_or_fail = lambda query : get_or_http_error ( MetaSchema , query ) <EOL> autoload_draft = functools . partial ( autoload , DraftRegistration , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def must_be_branched_from_node ( func ) : <EOL> @ autoload_draft <EOL> @ must_be_valid_project <EOL> @ functools . wraps ( func ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> node = kwargs [ '<STR_LIT>' ] <EOL> draft = kwargs [ '<STR_LIT>' ] <EOL> if not draft . branched_from . _id == node . _id : <EOL> raise HTTPError ( <EOL> http . BAD_REQUEST , <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> ) <EOL> return func ( * args , ** kwargs ) <EOL> return wrapper <EOL> def validate_embargo_end_date ( end_date_string , node ) : <EOL> """<STR_LIT>""" <EOL> end_date = parse_date ( end_date_string , ignoretz = True ) <EOL> today = datetime . datetime . utcnow ( ) <EOL> if ( end_date - today ) <= settings . DRAFT_REGISTRATION_APPROVAL_PERIOD : <EOL> raise HTTPError ( http . BAD_REQUEST , data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( settings . DRAFT_REGISTRATION_APPROVAL_PERIOD ) <EOL> } ) <EOL> elif not node . _is_embargo_date_valid ( end_date ) : <EOL> max_end_date = today + settings . DRAFT_REGISTRATION_APPROVAL_PERIOD <EOL> raise HTTPError ( http . BAD_REQUEST , data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( max_end_date . isoformat ( ) ) <EOL> } ) <EOL> def validate_registration_choice ( registration_choice ) : <EOL> if registration_choice not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise HTTPError ( <EOL> http . BAD_REQUEST , <EOL> data = { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" <EOL> } <EOL> ) <EOL> def check_draft_state ( draft ) : <EOL> registered_and_deleted = draft . registered_node and draft . registered_node . is_deleted <EOL> if draft . registered_node and not registered_and_deleted : <EOL> raise HTTPError ( http . FORBIDDEN , data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> if draft . is_pending_review : <EOL> raise HTTPError ( http . FORBIDDEN , data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> if draft . requires_approval and draft . is_approved and ( not registered_and_deleted ) : <EOL> raise HTTPError ( http . FORBIDDEN , data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> @ must_have_permission ( ADMIN ) <EOL> @ must_be_branched_from_node <EOL> def submit_draft_for_review ( auth , node , draft , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> data = request . get_json ( ) <EOL> meta = { } <EOL> registration_choice = data . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> validate_registration_choice ( registration_choice ) <EOL> if registration_choice == '<STR_LIT>' : <EOL> end_date_string = data [ '<STR_LIT>' ] <EOL> validate_embargo_end_date ( end_date_string , node ) <EOL> meta [ '<STR_LIT>' ] = end_date_string <EOL> meta [ '<STR_LIT>' ] = registration_choice <EOL> draft . submit_for_review ( <EOL> initiated_by = auth . user , <EOL> meta = meta , <EOL> save = True <EOL> ) <EOL> if prereg_utils . get_prereg_schema ( ) == draft . registration_schema : <EOL> node . add_log ( <EOL> action = NodeLog . PREREG_REGISTRATION_INITIATED , <EOL> params = { '<STR_LIT>' : node . _primary_key } , <EOL> auth = auth , <EOL> save = False <EOL> ) <EOL> node . save ( ) <EOL> push_status_message ( language . AFTER_SUBMIT_FOR_REVIEW , <EOL> kind = '<STR_LIT:info>' , <EOL> trust = False ) <EOL> return { <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : node . web_url_for ( '<STR_LIT>' ) <EOL> } <EOL> } , http . ACCEPTED <EOL> @ must_have_permission ( ADMIN ) <EOL> @ must_be_branched_from_node <EOL> def draft_before_register_page ( auth , node , draft , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> ret = serialize_node ( node , auth , primary = True ) <EOL> ret [ '<STR_LIT>' ] = serialize_draft_registration ( draft , auth ) <EOL> return ret <EOL> @ must_have_permission ( ADMIN ) <EOL> @ must_be_branched_from_node <EOL> @ http_error_if_disk_saving_mode <EOL> def register_draft_registration ( auth , node , draft , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> data = request . get_json ( ) <EOL> registration_choice = data . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> validate_registration_choice ( registration_choice ) <EOL> register = draft . register ( auth ) <EOL> draft . save ( ) <EOL> if registration_choice == '<STR_LIT>' : <EOL> embargo_end_date = parse_date ( data [ '<STR_LIT>' ] , ignoretz = True ) <EOL> try : <EOL> register . embargo_registration ( auth . user , embargo_end_date ) <EOL> except ValidationValueError as err : <EOL> raise HTTPError ( http . BAD_REQUEST , data = dict ( message_long = err . message ) ) <EOL> else : <EOL> try : <EOL> register . require_approval ( auth . user ) <EOL> except NodeStateError as err : <EOL> raise HTTPError ( http . BAD_REQUEST , data = dict ( message_long = err . message ) ) <EOL> register . save ( ) <EOL> push_status_message ( language . AFTER_REGISTER_ARCHIVING , <EOL> kind = '<STR_LIT:info>' , <EOL> trust = False ) <EOL> return { <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : node . web_url_for ( '<STR_LIT>' ) <EOL> } <EOL> } , http . ACCEPTED <EOL> @ must_have_permission ( ADMIN ) <EOL> @ must_be_branched_from_node <EOL> def get_draft_registration ( auth , node , draft , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return serialize_draft_registration ( draft , auth ) , http . OK <EOL> @ must_have_permission ( ADMIN ) <EOL> @ must_be_valid_project <EOL> def get_draft_registrations ( auth , node , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> count = request . args . get ( '<STR_LIT:count>' , <NUM_LIT:100> ) <EOL> drafts = itertools . islice ( node . draft_registrations_active , <NUM_LIT:0> , count ) <EOL> return { <EOL> '<STR_LIT>' : [ serialize_draft_registration ( d , auth ) for d in drafts ] <EOL> } , http . OK <EOL> @ must_have_permission ( ADMIN ) <EOL> @ must_be_valid_project <EOL> def new_draft_registration ( auth , node , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if node . is_registration : <EOL> raise HTTPError ( http . FORBIDDEN , data = { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" <EOL> } ) <EOL> data = request . values <EOL> schema_name = data . get ( '<STR_LIT>' ) <EOL> if not schema_name : <EOL> raise HTTPError ( <EOL> http . BAD_REQUEST , <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> ) <EOL> schema_version = data . get ( '<STR_LIT>' , <NUM_LIT:2> ) <EOL> meta_schema = get_schema_or_fail ( <EOL> Q ( '<STR_LIT:name>' , '<STR_LIT>' , schema_name ) & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , int ( schema_version ) ) <EOL> ) <EOL> draft = DraftRegistration . create_from_node ( <EOL> node , <EOL> user = auth . user , <EOL> schema = meta_schema , <EOL> data = { } <EOL> ) <EOL> return redirect ( node . web_url_for ( '<STR_LIT>' , draft_id = draft . _id ) ) <EOL> @ must_have_permission ( ADMIN ) <EOL> @ must_be_branched_from_node <EOL> def edit_draft_registration_page ( auth , node , draft , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> check_draft_state ( draft ) <EOL> ret = project_utils . serialize_node ( node , auth , primary = True ) <EOL> ret [ '<STR_LIT>' ] = serialize_draft_registration ( draft , auth ) <EOL> return ret <EOL> @ must_have_permission ( ADMIN ) <EOL> @ must_be_branched_from_node <EOL> def update_draft_registration ( auth , node , draft , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> check_draft_state ( draft ) <EOL> data = request . get_json ( ) <EOL> schema_data = data . get ( '<STR_LIT>' , { } ) <EOL> schema_data = rapply ( schema_data , strip_html ) <EOL> schema_name = data . get ( '<STR_LIT>' ) <EOL> schema_version = data . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> if schema_name : <EOL> meta_schema = get_schema_or_fail ( <EOL> Q ( '<STR_LIT:name>' , '<STR_LIT>' , schema_name ) & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , schema_version ) <EOL> ) <EOL> existing_schema = draft . registration_schema <EOL> if ( existing_schema . name , existing_schema . schema_version ) != ( meta_schema . name , meta_schema . schema_version ) : <EOL> draft . registration_schema = meta_schema <EOL> draft . update_metadata ( schema_data ) <EOL> draft . save ( ) <EOL> return serialize_draft_registration ( draft , auth ) , http . OK <EOL> @ must_have_permission ( ADMIN ) <EOL> @ must_be_branched_from_node <EOL> def delete_draft_registration ( auth , node , draft , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if draft . registered_node and not draft . registered_node . is_deleted : <EOL> raise HTTPError ( <EOL> http . FORBIDDEN , <EOL> data = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> ) <EOL> DraftRegistration . remove_one ( draft ) <EOL> return None , http . NO_CONTENT <EOL> def get_metaschemas ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> count = request . args . get ( '<STR_LIT:count>' , <NUM_LIT:100> ) <EOL> include = request . args . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> meta_schema_collection = database [ '<STR_LIT>' ] <EOL> meta_schemas = [ ] <EOL> if include == '<STR_LIT>' : <EOL> schema_names = meta_schema_collection . distinct ( '<STR_LIT:name>' ) <EOL> for name in schema_names : <EOL> meta_schema_set = MetaSchema . find ( <EOL> Q ( '<STR_LIT:name>' , '<STR_LIT>' , name ) & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:2> ) <EOL> ) <EOL> meta_schemas = meta_schemas + [ s for s in meta_schema_set ] <EOL> else : <EOL> meta_schemas = MetaSchema . find ( ) <EOL> meta_schemas = [ <EOL> schema <EOL> for schema in meta_schemas <EOL> if schema . name in ACTIVE_META_SCHEMAS <EOL> ] <EOL> meta_schemas . sort ( key = lambda a : ACTIVE_META_SCHEMAS . index ( a . name ) ) <EOL> return { <EOL> '<STR_LIT>' : [ <EOL> serialize_meta_schema ( ms ) for ms in meta_schemas [ : count ] <EOL> ] <EOL> } , http . OK </s>
