Le repo des sources pour le site web des JM2L
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

185 wiersze
6.2 KiB

  1. # -*- coding: utf8 -*-
  2. import io
  3. from pyramid.response import Response
  4. from pyramid.view import view_config
  5. from .models import DBSession, Event, Salles
  6. from reportlab.pdfgen import canvas
  7. from reportlab.pdfbase import pdfmetrics
  8. from reportlab.pdfbase.ttfonts import TTFont
  9. from reportlab.lib.units import mm
  10. from .upload import MediaPath
  11. from jm2l.const import CurrentYear
  12. # Create PDF container
  13. EXPIRATION_TIME = 300 # seconds
  14. WIDTH = 210 * mm
  15. HEIGHT = 297 * mm
  16. ICONSIZE = 10 * mm
  17. def JM2L_large_Logo(canvas, Offset=(0, 0)):
  18. OffX, OffY = Offset
  19. canvas.setFont('Logo', 110)
  20. canvas.setFillColorRGB(.83, 0, .33)
  21. canvas.drawCentredString(WIDTH / 2 - OffY, HEIGHT - 100 - OffX, "JM2L")
  22. canvas.setFont("Helvetica-Bold", 30)
  23. year_object = canvas.beginText()
  24. year_object.setFillColorRGB(1, 1, 1)
  25. year_object.setTextRenderMode(0)
  26. year_object.setTextOrigin(WIDTH / 2 - OffY - 120, HEIGHT - 36 - OffX)
  27. year_object.setWordSpace(48)
  28. year_object.textLines("2 0 1 5")
  29. year_object.setWordSpace(1)
  30. canvas.drawText(year_object)
  31. def one_time_step(canvas, str, hour, max_size, offset):
  32. max_x, max_y = max_size
  33. off_x, off_y = offset
  34. step_y = max_y / 9
  35. half_step = step_y / 2
  36. canvas.drawCentredString(off_x - 30, max_y - step_y * hour + off_y - 3, str)
  37. hour_place = step_y * hour + off_y
  38. canvas.line(off_x - 5, hour_place, off_x, hour_place)
  39. if hour < 9:
  40. canvas.line(off_x - 2, hour_place + half_step, off_x, hour_place + half_step)
  41. @view_config(route_name='stand_print', http_cache=(EXPIRATION_TIME, {'public': True}))
  42. def stand_print(request):
  43. # Ok let's generate a print for place schedule
  44. # Register LiberationMono font
  45. ttf_file = "jm2l/static/fonts/LiberationMono-Regular.ttf"
  46. pdfmetrics.registerFont(TTFont("Liberation", ttf_file))
  47. #  Import font
  48. ttf_file_logo = "jm2l/static/fonts/PWTinselLetters.ttf"
  49. pdfmetrics.registerFont(TTFont("Logo", ttf_file_logo))
  50. pdf = io.BytesIO()
  51. c = canvas.Canvas(pdf, pagesize=(HEIGHT, WIDTH))
  52. c.translate(mm, mm)
  53. # Feed some metadata
  54. c.setCreator("linux-azur.org")
  55. c.setTitle("Affiches stand")
  56. c.saveState()
  57. year = int(request.matchdict.get('year', CurrentYear))
  58. Events = DBSession.query(Event) \
  59. .filter(Event.for_year == year) \
  60. .filter(Event.event_type == "Stand")
  61. for ev in Events:
  62. c.setFont('Logo', 50)
  63. c.setFillColorRGB(.5, .5, .5)
  64. c.drawString(HEIGHT - 150, 30, "JM2L")
  65. c.setFont('Logo', 100)
  66. c.setFillColorRGB(0.5, 0.5, 0.5)
  67. c.drawCentredString(HEIGHT / 2, WIDTH - 90, "STAND", 0)
  68. c.setFillColorRGB(0, 0, 0)
  69. c.setFont('Helvetica', 42)
  70. c.drawCentredString(HEIGHT / 2, WIDTH / 2, ev.name, 0)
  71. c.showPage()
  72. c.save()
  73. pdf.seek(0)
  74. return Response(app_iter=pdf, content_type='application/pdf')
  75. @view_config(route_name='place_print', http_cache=(EXPIRATION_TIME, {'public': True}))
  76. def place_print(request):
  77. # Ok let's generate a print for place schedule
  78. # Register LiberationMono font
  79. ttf_file = "jm2l/static/fonts/LiberationMono-Regular.ttf"
  80. pdfmetrics.registerFont(TTFont("Liberation", ttf_file))
  81. #  Import font
  82. ttf_file_logo = "jm2l/static/fonts/PWTinselLetters.ttf"
  83. pdfmetrics.registerFont(TTFont("Logo", ttf_file_logo))
  84. pdf = io.BytesIO()
  85. c = canvas.Canvas(pdf, pagesize=(WIDTH, HEIGHT))
  86. c.translate(mm, mm)
  87. # Feed some metadata
  88. c.setCreator("linux-azur.org")
  89. c.setTitle("Planning Salle")
  90. c.saveState()
  91. year = int(request.matchdict.get('year', CurrentYear))
  92. # Initialization
  93. # Compute days used by all events matching the specified input year
  94. place_used = DBSession.query(Event.salle_uid) \
  95. .filter(Event.for_year == year) \
  96. .filter(Event.event_type != 'Stand') \
  97. .group_by(Event.salle_uid)
  98. for place in place_used:
  99. place_uid = place[0]
  100. place_obj = Salles.by_id(place_uid)
  101. # Logo on Top
  102. JM2L_large_Logo(c)
  103. max_size = (WIDTH - 110, HEIGHT - 300)
  104. offset = (70, 90)
  105. c.setFillColorRGB(.5, .5, .5)
  106. c.setFont('Liberation', 40)
  107. c.drawCentredString(WIDTH / 2, HEIGHT - 190, place_obj.name, 1)
  108. c.setFont('Liberation', 35)
  109. c.drawCentredString(WIDTH / 2, HEIGHT - 145, place_obj.place_type, 0)
  110. c.setFont('Helvetica', 20)
  111. c.drawCentredString(WIDTH / 2, 55, place_obj.phy.name, 0)
  112. # Timetable container
  113. c.setLineWidth(.1)
  114. c.setLineCap(2)
  115. # c.setFillColorRGB(0,0,1)
  116. c.rect(offset[0], offset[1], max_size[0], max_size[1], fill=0, stroke=1)
  117. c.setLineWidth(.5)
  118. # create time mark
  119. c.setFillColorRGB(0, 0, 0)
  120. c.setFont('Helvetica', 10)
  121. for i in range(0, 10):
  122. one_time_step(c, "%.2dh00" % (i + 10), i, max_size, offset)
  123. # c.setFont('Helvetica', 12)
  124. Events = DBSession.query(Event) \
  125. .filter(Event.for_year == year) \
  126. .filter(Event.salle_uid == place_uid) \
  127. .order_by(Event.start_time)
  128. for ev in Events:
  129. place_time(c, ev, max_size, offset)
  130. # c.rect(70, 50, WIDTH-100, HEIGHT-250, fill=0, stroke=1)
  131. c.showPage()
  132. c.save()
  133. pdf.seek(0)
  134. return Response(app_iter=pdf, content_type='application/pdf')
  135. def place_time(c, ev, max_size, offset):
  136. max_x, max_y = max_size
  137. off_x, off_y = offset
  138. minute = max_y / (9 * 60)
  139. start_pos_y = ((int(ev.start_time.strftime('%H')) - 10) * 60 + int(ev.start_time.strftime('%M'))) * minute
  140. stop_pos_y = ((int(ev.end_time.strftime('%H')) - 10) * 60 + int(ev.end_time.strftime('%M'))) * minute
  141. c.setFillColorRGB(0.98, 0.98, 0.98)
  142. c.rect(offset[0], max_y + off_y - start_pos_y, max_size[0], start_pos_y - stop_pos_y, fill=1, stroke=1)
  143. c.setFillColorRGB(0, 0, 0)
  144. # c.drawString(off_x+5, max_y + off_y - 15 - start_pos_y, ev.start_time.strftime('%H:%M'), 0)
  145. c.setFont('Helvetica', 12)
  146. c.drawCentredString(WIDTH / 2, max_y + off_y - 35 - start_pos_y, ev.name, 0)
  147. intervs = ', '.join([x.slug for x in ev.intervenants])
  148. c.setFont('Helvetica', 10)
  149. c.drawCentredString(WIDTH / 2, max_y + off_y - 55 - start_pos_y, intervs, 0)