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

import csv, datetime, json, logging, os, pprint, StringIO
import requests
from django.contrib.auth import logout
from django.core.urlresolvers import reverse
from django.db import models
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.utils.http import urlquote
from django.utils.encoding import smart_unicode
from django.conf import settings as project_settings
from easyscan_app.lib.magic_bus import Prepper, Sender
from easyscan_app.easyscan_forms import CitationForm


log = logging.getLogger(__name__)
prepper = Prepper()
sender = Sender()


## db models ##

class ScanRequest( models.Model ):
    """ Contains user & item data. """
    item_title = models.CharField( blank=True, max_length=200 )
    item_barcode = models.CharField( blank=True, max_length=50 )
    item_callnumber = models.CharField( blank=True, max_length=200 )
    item_volume_year = models.CharField( blank=True, max_length=200 )
    item_source_url = models.TextField( blank=True )
    item_chap_vol_title = models.TextField( blank=True )
    item_page_range_other = models.TextField( blank=True )
    patron_name = models.CharField( blank=True, max_length=100 )
    patron_barcode = models.CharField( blank=True, max_length=50 )
    patron_email = models.CharField( blank=True, max_length=100 )
    create_datetime = models.DateTimeField( auto_now_add=True, blank=True )  # blank=True for backward compatibility
    las_conversion = models.TextField( blank=True )

    def __unicode__(self):
        return smart_unicode( u'patbar%s_itmbar%s' % (self.patron_barcode, self.item_barcode) , u'utf-8', u'replace' )

    def save(self):
        super( ScanRequest, self ).save() # Call the "real" save() method
        maker = LasDataMaker()
        las_string = maker.make_csv_string(
            self.create_datetime, self.patron_name, self.patron_barcode, self.patron_email, self.item_title, self.item_barcode, self.item_chap_vol_title, self.item_page_range_other )
        self.las_conversion = las_string
        super( ScanRequest, self ).save() # Call the "real" save() method


## non db models below  ##


class LasDataMaker( object ):
    """ Container for code to make comma-delimited las string. """

    def make_csv_string(
        self, date_string, patron_name, patron_barcode, patron_email, item_title, item_barcode, item_chap_vol_title, item_page_range_other ):
        """ Makes and returns csv string from database data.
            Called by models.ScanRequest.save() """
        modified_date_string = self.make_date_string( date_string )
        utf8_data_list = self.make_utf8_data_list(
            modified_date_string, item_barcode, patron_name, patron_barcode, item_title, patron_email, item_chap_vol_title, item_page_range_other )
        utf8_csv_string = self.utf8list_to_utf8csv( utf8_data_list )
        csv_string = utf8_csv_string.decode( u'utf-8' )
        return csv_string

    def make_date_string( self, datetime_object ):
        """ Will convert datetime_object to date format required by LAS.
            Example returned format, `Mon Dec 05 2014`.
            Called by make_csv_string() """
        utf8_date_string = datetime_object.strftime( u'%a %b %d %Y' )
        date_string = utf8_date_string.decode( u'utf-8' )
        return date_string

    def make_utf8_data_list( self, modified_date_string, item_barcode, patron_name, patron_barcode, item_title, patron_email, item_chap_vol_title, item_page_range_other ):
        """ Assembles data elements in order required by LAS.
            Called by make_csv_string() """
        utf8_data_list = [
            'item_id_not_applicable',
            item_barcode.encode( u'utf-8', u'replace' ),
            'ED',
            'QS',
            patron_name.encode( u'utf-8', u'replace' ),
            patron_barcode.encode( u'utf-8', u'replace' ),
            item_title.encode( u'utf-8', u'replace' ),
            modified_date_string.encode( u'utf-8', u'replace' ),
            'eml, %s -- artcl-chptr-ttl, %s -- pg-rng, %s' % (
                patron_email.encode(u'utf-8', u'replace'),
                item_chap_vol_title.encode(u'utf-8', u'replace'),
                item_page_range_other.encode(u'utf-8', u'replace'),
                )
            ]
        return utf8_data_list

    def utf8list_to_utf8csv( self, utf8_data_list ):
        """ Converts list into utf8 string.
            Called by make_csv_string()
            Note; this python2 csv module requires utf-8 strings. """
        for entry in utf8_data_list:
            if not type(entry) == str:
                raise Exception( u'entry `%s` not of type str' % unicode(repr(entry)) )
        io = StringIO.StringIO()
        writer = csv.writer( io, delimiter=',', quoting=csv.QUOTE_ALL )
        writer.writerow( utf8_data_list )
        csv_string = io.getvalue()
        io.close()
        return csv_string


class RequestViewGetHelper( object ):
    """ Container for views.request_def() helpers for handling GET. """

    def __init__( self ):
        self.AVAILABILITY_API_URL_ROOT = os.environ[u'EZSCAN__AVAILABILITY_API_URL_ROOT']

    def handle_get( self, request ):
        """ Handles request-page GET; returns response.
            Called by views.request_def() """
        https_check = self.check_https( request.is_secure(), request.get_host(), request.get_full_path() )
        if https_check[u'is_secure'] == False:
            return HttpResponseRedirect( https_check[u'redirect_url'] )
        title = self.check_title( request )
        self.initialize_session( request, title )<commit_msg>adds logging to better grab referrer.<commit_after># -*- coding: utf-8 -*-

import csv, datetime, json, logging, os, pprint, StringIO
import requests
from django.contrib.auth import logout
from django.core.urlresolvers import reverse
from django.db import models
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.utils.http import urlquote
from django.utils.encoding import smart_unicode
from django.conf import settings as project_settings
from easyscan_app.lib.magic_bus import Prepper, Sender
from easyscan_app.easyscan_forms import CitationForm


log = logging.getLogger(__name__)
prepper = Prepper()
sender = Sender()


## db models ##

class ScanRequest( models.Model ):
    """ Contains user & item data. """
    item_title = models.CharField( blank=True, max_length=200 )
    item_barcode = models.CharField( blank=True, max_length=50 )
    item_callnumber = models.CharField( blank=True, max_length=200 )
    item_volume_year = models.CharField( blank=True, max_length=200 )
    item_source_url = models.TextField( blank=True )
    item_chap_vol_title = models.TextField( blank=True )
    item_page_range_other = models.TextField( blank=True )
    patron_name = models.CharField( blank=True, max_length=100 )
    patron_barcode = models.CharField( blank=True, max_length=50 )
    patron_email = models.CharField( blank=True, max_length=100 )
    create_datetime = models.DateTimeField( auto_now_add=True, blank=True )  # blank=True for backward compatibility
    las_conversion = models.TextField( blank=True )

    def __unicode__(self):
        return smart_unicode( u'patbar%s_itmbar%s' % (self.patron_barcode, self.item_barcode) , u'utf-8', u'replace' )

    def save(self):
        super( ScanRequest, self ).save() # Call the "real" save() method
        maker = LasDataMaker()
        las_string = maker.make_csv_string(
            self.create_datetime, self.patron_name, self.patron_barcode, self.patron_email, self.item_title, self.item_barcode, self.item_chap_vol_title, self.item_page_range_other )
        self.las_conversion = las_string
        super( ScanRequest, self ).save() # Call the "real" save() method


## non db models below  ##


class LasDataMaker( object ):
    """ Container for code to make comma-delimited las string. """

    def make_csv_string(
        self, date_string, patron_name, patron_barcode, patron_email, item_title, item_barcode, item_chap_vol_title, item_page_range_other ):
        """ Makes and returns csv string from database data.
            Called by models.ScanRequest.save() """
        modified_date_string = self.make_date_string( date_string )
        utf8_data_list = self.make_utf8_data_list(
            modified_date_string, item_barcode, patron_name, patron_barcode, item_title, patron_email, item_chap_vol_title, item_page_range_other )
        utf8_csv_string = self.utf8list_to_utf8csv( utf8_data_list )
        csv_string = utf8_csv_string.decode( u'utf-8' )
        return csv_string

    def make_date_string( self, datetime_object ):
        """ Will convert datetime_object to date format required by LAS.
            Example returned format, `Mon Dec 05 2014`.
            Called by make_csv_string() """
        utf8_date_string = datetime_object.strftime( u'%a %b %d %Y' )
        date_string = utf8_date_string.decode( u'utf-8' )
        return date_string

    def make_utf8_data_list( self, modified_date_string, item_barcode, patron_name, patron_barcode, item_title, patron_email, item_chap_vol_title, item_page_range_other ):
        """ Assembles data elements in order required by LAS.
            Called by make_csv_string() """
        utf8_data_list = [
            'item_id_not_applicable',
            item_barcode.encode( u'utf-8', u'replace' ),
            'ED',
            'QS',
            patron_name.encode( u'utf-8', u'replace' ),
            patron_barcode.encode( u'utf-8', u'replace' ),
            item_title.encode( u'utf-8', u'replace' ),
            modified_date_string.encode( u'utf-8', u'replace' ),
            'eml, %s -- artcl-chptr-ttl, %s -- pg-rng, %s' % (
                patron_email.encode(u'utf-8', u'replace'),
                item_chap_vol_title.encode(u'utf-8', u'replace'),
                item_page_range_other.encode(u'utf-8', u'replace'),
                )
            ]
        return utf8_data_list

    def utf8list_to_utf8csv( self, utf8_data_list ):
        """ Converts list into utf8 string.
            Called by make_csv_string()
            Note; this python2 csv module requires utf-8 strings. """
        for entry in utf8_data_list:
            if not type(entry) == str:
                raise Exception( u'entry `%s` not of type str' % unicode(repr(entry)) )
        io = StringIO.StringIO()
        writer = csv.writer( io, delimiter=',', quoting=csv.QUOTE_ALL )
        writer.writerow( utf8_data_list )
        csv_string = io.getvalue()
        io.close()
        return csv_string


class RequestViewGetHelper( object ):
    """ Container for views.request_def() helpers for handling GET. """

    def __init__( self ):
        self.AVAILABILITY_API_URL_ROOT = os.environ[u'EZSCAN__AVAILABILITY_API_URL_ROOT']

    def handle_get( self, request ):
        """ Handles request-page GET; returns response.
            Called by views.request_def() """
        log.debug( u'in models.RequestViewGetHelper.handle_get(); referrer, `%s`' % request.META.get(u'HTTP_REFERER', u'not_in_request_meta'), )
        https_check = self.check_https( request.is_secure(), request.get_host(), request.get_full_path() )
        if https_check[u'is_secure'] == False:
            return HttpResponseRedirect( https_check[u'redirect_url'] )
        title = self.check_title( request )
        self.initialize_session( request, title )
{'fraction_non_alphanumeric': 0.10197855835439729, 'fraction_numerical': 0.009936372352479735, 'mean_word_length': 3.226151012891344, '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': '6575019', 'n_tokens_mistral': 3639, 'n_tokens_neox': 3522, 'n_words': 822}