<commit_before>require [
  "jquery"
  "underscore"
  "backbone"
  "bootstrap"
  "backbonePaginator"
  "backboneRelational"
], ($, _, Backbone, Bootstrap, PageableCollection) ->
  htmlEncode = (value) ->
    $("<div/>").text(value).html()
  $.fn.serializeObject = ->
    o = {}
    a = @serializeArray()
    $.each a, ->
      if o[@name] isnt `undefined`
        o[@name] = [o[@name]]  unless o[@name].push
        o[@name].push @value or ""
      else
        o[@name] = @value or ""
      return

    o

  $.ajaxPrefilter (options, originalOptions, jqXHR) ->
    options.url = "http://researchdb.warwick.ac.uk/liverpool/api" + options.url
    return

  Address = Backbone.RelationalModel.extend()
  Addresses = Backbone.Collection.extend(
    model: Address
  )

  Occupation = Backbone.RelationalModel.extend()
  Occupations = Backbone.Collection.extend 
    url: "/occupations"
    model: Occupation

  Relationship = Backbone.RelationalModel.extend()

  Relationships = Backbone.Collection.extend(
    url: ->
      "/relationships/" + @models[0].id
    model: Relationship
  )

  FlatPerson = Backbone.Model.extend()

  Person = Backbone.RelationalModel.extend(
    urlRoot: "/people"
    relations: [
      type: Backbone.HasMany
      key: 'relationships'
      relatedModel: Relationship
      collectionType: Occupations
      reverseRelation: 
        key: 'person',
        includeInJSON: 'id'
    ,
      type: Backbone.HasMany
      key: 'occupations'
      relatedModel: Occupation
      collectionType: Occupations
      reverseRelation: 
        key: 'person',
        includeInJSON: 'id'
    ,
      type: Backbone.HasMany
      key: 'addresses'
      relatedModel: Address
      collectionType: Addresses
      reverseRelation: 
        key: 'person',
        includeInJSON: 'id'
    ]
    parse: (response) ->
          # use some render helpers to add to the model some text to format fuzzy date types
      @.birthdate = @fuzzyDateMaker @birthtype, @birthdate1, @birthdate2
      @.deathdate = @fuzzyDateMaker @deathtype, @deathdate1, @deathdate2

      response
    fuzzyDateMaker: (type, date1, date2) ->
      
      #takes the HL way of representing fuzzy dates, and returns a formatted string. 
      switch type
        when "exactly"
          return date1
        when "between"
          return "between " + ((if isset(date1) then date1 else "unknown")) + " and " + ((if isset(date2) then date2 else "unknown"))
        when "unknown", null
          "unknown"
    )

  People = Backbone.Collection.extend(
    url: "/people"
    model: FlatPerson
    parse: (response) ->
      response
  )
  PeopleSearch = Backbone.Collection.extend(
    url: ->
      "/peoplesearch?" + options.querystring
    model: FlatPerson
    parse: (response) ->
      response
    )

  Backbone.PageableCollection = PageableCollection

  PaginatedPeople = Backbone.PageableCollection.extend(
    model: FlatPerson
    url: "/peoplepaginated"
    state:
      firstPage: 1
      sortKey: "surname"
      pageSize: 20

    parse: (response) -> 
      @state.totalRecords = parseInt(response.total)
      @state.lastPage = Math.ceil(response.total / @state.pageSize)
      response.data
    parseRecords: (response) ->
      response.data
    parseState: (response) ->
      @state.totalRecords = parseInt(response.total)
      @state.lastPage = Math.ceil(response.total / @state.pageSize)
      @state
  )

  Relationship = Backbone.Model.extend(urlRoot: "/relationships")

  Place = Backbone.Model.extend

  Places = Backbone.Collection.extend
    model: Place
    parse: (response)->
      console.log @, response, "parsing place"
      response
  
 

  Country = Backbone.Model.extend
    initialize: ->
      @places = new Places
      @places.url = '/placesfor/' + this.get("country")
    parse: (response) ->
      console.log @, response, "parsing country"
      response

  Countries = Backbone.Collection.extend(
    url: "/countries"
    model: Country
  )


<commit_msg>update country to relational model<commit_after>require [
  "jquery"
  "underscore"
  "backbone"
  "bootstrap"
  "backbonePaginator"
  "backboneRelational"
], ($, _, Backbone, Bootstrap, PageableCollection) ->
  htmlEncode = (value) ->
    $("<div/>").text(value).html()
  $.fn.serializeObject = ->
    o = {}
    a = @serializeArray()
    $.each a, ->
      if o[@name] isnt `undefined`
        o[@name] = [o[@name]]  unless o[@name].push
        o[@name].push @value or ""
      else
        o[@name] = @value or ""
      return

    o

  $.ajaxPrefilter (options, originalOptions, jqXHR) ->
    options.url = "http://researchdb.warwick.ac.uk/liverpool/api" + options.url
    return

  Address = Backbone.RelationalModel.extend()
  Addresses = Backbone.Collection.extend(
    model: Address
  )

  Occupation = Backbone.RelationalModel.extend()
  Occupations = Backbone.Collection.extend 
    url: "/occupations"
    model: Occupation

  Relationship = Backbone.RelationalModel.extend()

  Relationships = Backbone.Collection.extend(
    url: ->
      "/relationships/" + @models[0].id
    model: Relationship
  )

  FlatPerson = Backbone.Model.extend()

  Person = Backbone.RelationalModel.extend(
    urlRoot: "/people"
    relations: [
      type: Backbone.HasMany
      key: 'relationships'
      relatedModel: Relationship
      collectionType: Occupations
      reverseRelation: 
        key: 'person',
        includeInJSON: 'id'
    ,
      type: Backbone.HasMany
      key: 'occupations'
      relatedModel: Occupation
      collectionType: Occupations
      reverseRelation: 
        key: 'person',
        includeInJSON: 'id'
    ,
      type: Backbone.HasMany
      key: 'addresses'
      relatedModel: Address
      collectionType: Addresses
      reverseRelation: 
        key: 'person',
        includeInJSON: 'id'
    ]
    parse: (response) ->
          # use some render helpers to add to the model some text to format fuzzy date types
      @.birthdate = @fuzzyDateMaker @birthtype, @birthdate1, @birthdate2
      @.deathdate = @fuzzyDateMaker @deathtype, @deathdate1, @deathdate2

      response
    fuzzyDateMaker: (type, date1, date2) ->
      
      #takes the HL way of representing fuzzy dates, and returns a formatted string. 
      switch type
        when "exactly"
          return date1
        when "between"
          return "between " + ((if isset(date1) then date1 else "unknown")) + " and " + ((if isset(date2) then date2 else "unknown"))
        when "unknown", null
          "unknown"
    )

  People = Backbone.Collection.extend(
    url: "/people"
    model: FlatPerson
    parse: (response) ->
      response
  )
  PeopleSearch = Backbone.Collection.extend(
    url: ->
      "/peoplesearch?" + options.querystring
    model: FlatPerson
    parse: (response) ->
      response
    )

  Backbone.PageableCollection = PageableCollection

  PaginatedPeople = Backbone.PageableCollection.extend(
    model: FlatPerson
    url: "/peoplepaginated"
    state:
      firstPage: 1
      sortKey: "surname"
      pageSize: 20

    parse: (response) -> 
      @state.totalRecords = parseInt(response.total)
      @state.lastPage = Math.ceil(response.total / @state.pageSize)
      response.data
    parseRecords: (response) ->
      response.data
    parseState: (response) ->
      @state.totalRecords = parseInt(response.total)
      @state.lastPage = Math.ceil(response.total / @state.pageSize)
      @state
  )

  Relationship = Backbone.Model.extend(urlRoot: "/relationships")

  Place = Backbone.Model.extend

  Places = Backbone.Collection.extend
    model: Place
    parse: (response)->
      console.log @, response, "parsing place"
      response
  
 

  Country = Backbone.RelationalModel.extend(
    relations: [
      type: Backbone.HasMany
      key: 'places'
      relatedModel: Place
      reverseRelation: 
          key: 'country'
      ]
    parse: (response) ->
      response
  )

  Countries = Backbone.Collection.extend(
    url: "/countriesfull"
    model: Country
  )



{'fraction_non_alphanumeric': 0.1153942115768463, 'fraction_numerical': 0.0037425149700598802, 'mean_word_length': 2.55678793256433, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 38, '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': '3165637', 'n_tokens_mistral': 2534, 'n_tokens_neox': 2413, 'n_words': 557}