update_proposals.rkt 4.4 KB

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