update_proposals.rkt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #lang racket/base
  2. (require racket/cmdline)
  3. (require racket/date)
  4. (require db)
  5. (define progname "update_proposals.rkt")
  6. ; load configuration file
  7. (require (file "config.rkt"))
  8. ; give us the date in YYYY-MM-DD format
  9. (date-display-format 'iso-8601)
  10. ; set up command line arguments
  11. (define mode (command-line
  12. #:program "update_proposals"
  13. #:args (updatetype) ; (add, update)
  14. updatetype))
  15. ; print some help
  16. (define (printhelp)
  17. (write-string (string-append "Usage: "
  18. progname " MODE\n\n"))
  19. (write-string "Where MODE is one of:\n")
  20. (write-string " add\t\t - add new proposal to database.\n")
  21. (write-string " update\t\t - update a a proposal with results.\n")
  22. (write-string " help\t\t - Show this help message.\n")
  23. (write-string "\nCopyright 2019 George C. Privon\n"))
  24. ; set up a condensed prompt for getting information
  25. (define (getinput prompt)
  26. (write-string prompt)
  27. (write-string ": ")
  28. (read-line))
  29. ; take an input result from the SQL search and write it out nicely
  30. (define (printentry entry)
  31. (write-string (string-append
  32. (number->string (vector-ref entry 0))
  33. ": "
  34. (vector-ref entry 1)
  35. "("
  36. (vector-ref entry 2)
  37. ") "
  38. (vector-ref entry 3)
  39. ".\n"))
  40. )
  41. ; add a new proposal to the database
  42. (define (addnew)
  43. (write-string "Adding new proposal to database.\n")
  44. ; user inputs proposal data
  45. (define proptype (getinput "Proposal type"))
  46. (define org (getinput "Submitting organization"))
  47. (define solic (getinput "Solitation/call"))
  48. (define tele (getinput "Telescope"))
  49. (define title (getinput "Proposal title"))
  50. (define pi (getinput "PI"))
  51. (define coi (getinput "CoIs"))
  52. ; assume all these proposals are submitted, don't ask the user
  53. (define status "submitted")
  54. (define submitdate (getinput "Submit date (YYYY-MM-DD)"))
  55. (define oID (getinput "Organization's proposal ID"))
  56. ; do the INSERT into the Sqlite database
  57. (query-exec conn "INSERT INTO proposals (type, organization, solicitation, telescope, PI, title, CoI, status, submitdate, orgpropID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
  58. proptype org solic tele pi title coi status submitdate oID))
  59. ; update an entry with new status (accepted, rejected, etc.)
  60. (define (update ID)
  61. (write-string (string-append "Updating entry " (number->string ID) "\n"))
  62. (define entry (query-maybe-row conn "SELECT * FROM proposals WHERE ID=?" ID))
  63. (cond
  64. [((eq? #f entry) (error "Invalid ID. Row not found"))])
  65. (write-string (string-append "Current status is: "
  66. (vector-ref entry 9)
  67. " ("
  68. (vector-ref entry 10)
  69. ")\n"))
  70. (write-string "Please enter new status: ")
  71. (define newstatus (read-line))
  72. ;(write-string "Please enter date of updated status (leave blank to use current date): ")
  73. ;(define resdate (read-line))
  74. (define resdate (date->string (seconds->date (current-seconds))))
  75. ; now update that entry
  76. (query-exec conn "UPDATE proposals SET status=?, resultdate=? WHERE ID=?"
  77. newstatus
  78. resdate
  79. ID)
  80. (write-string "Entry updated.\n")
  81. )
  82. ; find proposals waiting for updates
  83. (define (findpending)
  84. (write-string "Updating proposals")
  85. ; retrieve all proposals whose status is still listed as "submitted"
  86. (define unfinished (query-rows conn "SELECT ID,telescope,solicitation,title FROM proposals WHERE status='submitted'"))
  87. (write-string (string-append (make-string (length unfinished)) " pending proposals found:\n"))
  88. ; print all the unresolved proposals to the screen
  89. (map printentry unfinished)
  90. (write-string "Please enter a proposal number to edit (enter 0 or nothing to exit): ")
  91. (define upID (read-line))
  92. (cond
  93. [(eq? (string->number upID) 0) (exit)]
  94. [(string->number upID) (update (string->number upID))]
  95. [else (exit)])
  96. )
  97. ; make sure we can use the sqlite3 connection
  98. (cond (not (sqlite3-available?))
  99. (error "Sqlite3 library not available."))
  100. ; open the database file
  101. (define conn (sqlite3-connect #:database dbloc))
  102. ; determine which mode we're in
  103. (cond
  104. [(regexp-match "help" mode) (printhelp)]
  105. [(regexp-match "add" mode) (addnew)]
  106. [(regexp-match "update" mode) (findpending)])
  107. ; close the databse
  108. (disconnect conn)