Changeset - 35b75b6f963c
[Not reviewed]
0 1 0
James Polley - 7 years ago 2017-09-30 01:01:39
jp@jamezpolley.com
Badger should fail gracefully if auth_groups hasn't been populated

Let's say you've just installed symposion for the first time, and
you're running the intial `./manage.py migrate`

In that circumstance, there isn't an auth_group table. Naturally this
means you get Some Errors when trying to look for a particular group.

This change handles that error and drives on.
1 file changed with 8 insertions and 2 deletions:
0 comments (0 inline, 0 general)
vendor/registrasion/registrasion/contrib/badger.py
Show inline comments
 
'''
 
Generate Conference Badges
 
==========================
 

	
 
Nearly all of the code in this was written by Richard Jones for the 2016 conference.
 
That code relied on the user supplying the attendee data in a CSV file, which Richard's
 
code then processed.
 

	
 
The main (and perhaps only real) difference, here, is that the attendee data are taken
 
directly from the database.  No CSV file is required.
 

	
 
This is now a library with functions / classes referenced by the generate_badges
 
management command, and by the tickets/badger and tickets/badge API functions.
 
'''
 
import sys
 
import os
 
import csv
 
from lxml import etree
 
import tempfile
 
from copy import deepcopy
 
import subprocess
 

	
 
import pdb
 

	
 
from django.core.management.base import BaseCommand
 

	
 
from django.contrib.auth.models import User, Group
 
from django.db.utils import OperationalError
 
from pinaxcon.registrasion.models import AttendeeProfile
 
from registrasion.controllers.cart import CartController
 
from registrasion.controllers.invoice import InvoiceController
 
from registrasion.models import Voucher
 
from registrasion.models import Attendee
 
from registrasion.models import Product
 
from registrasion.models import Invoice
 
from symposion.speakers.models import Speaker
 

	
 
# A few unicode encodings ...
 
GLYPH_PLUS = '+'
 
GLYPH_GLASS = u'\ue001'
 
GLYPH_DINNER = u'\ue179'
 
GLYPH_SPEAKER = u'\ue122'
 
GLYPH_SPRINTS = u'\ue254'
 
GLYPH_CROWN = u'\ue211'
 
GLYPH_SNOWMAN = u'\u2603'
 
GLYPH_STAR = u'\ue007'
 
GLYPH_FLASH = u'\ue162'
 
GLYPH_EDU = u'\ue233'
 

	
 
# Some company names are too long to fit on the badge, so, we
 
# define abbreviations here.
 
overrides = {
 
 "Optiver Pty. Ltd.": "Optiver",
 
 "IRESS Market Tech": "IRESS",
 
 "The Bureau of Meteorology": "BoM",
 
 "Google Australia": "Google",
 
 "Facebook Inc.": "Facebook",
 
 "Rhapsody Solutions Pty Ltd": "Rhapsody Solutions",
 
 "PivotNine Pty Ltd": "PivotNine",
 
 "SEEK Ltd.": "SEEK",
 
 "UNSW Australia": "UNSW",
 
 "Dev Demand Co": "Dev Demand",
 
 "Cascode Labs Pty Ltd": "Cascode Labs",
 
 "CyberHound Pty Ltd": "CyberHound",
 
 "Self employed Contractor": "",
 
 "Data Processors Pty Lmt": "Data Processors",
 
 "Bureau of Meterology": "BoM",
 
 "Google Australia Pty Ltd": "Google",
 
 # "NSW Rural Doctors Network": "",
 
 "Sense of Security Pty Ltd": "Sense of Security",
 
 "Hewlett Packard Enterprose": "HPE",
 
 "Hewlett Packard Enterprise": "HPE",
 
 "CISCO SYSTEMS INDIA PVT LTD": "CISCO",
 
 "The University of Melbourne": "University of Melbourne",
 
 "Peter MacCallum Cancer Centre": "Peter Mac",
 
 "Commonwealth Bank of Australia": "CBA",
...
 
@@ -79,98 +80,103 @@ overrides = {
 
 "Australian Synchrotron | ANSTO": "Australian Synchrotron",
 
 "Bureau of Meteorology, Australia": "BoM",
 
 "QUT Digital Media Research Centre": "QUT",
 
 "Dyn - Dynamic Network Services Inc": "Dyn",
 
 "The Australian National University": "ANU",
 
 "Murdoch Childrens Research Institute": "MCRI",
 
 "Centenary Institute, University of Sydney": "Centenary Institute",
 
 "Synchrotron Light Source Australia Pty Ltd": "Australian Synchrotron",
 
 "Australian Communication and Media Authority": "ACMA",
 
 "Dept. of Education - Camden Haven High School": "Camden Haven High School",
 
 "Australian Government - Bureau of Meteorology": "BoM",
 
 "The Walter and Eliza Hall Institute of Medical Research": "WEHI",
 
 "Dept. Parliamentary Services, Australian Parliamentary Library": "Dept. Parliamentary Services",
 
}
 

	
 

	
 
def text_size(text, prev=9999):
 
    '''
 
    Calculate the length of a text string as it relates to font size.
 
    '''
 
    n = len(text)
 
    size = int(min(48, max(28, 28 + 30 * (1 - (n-8) / 11.))))
 
    return min(prev, size)
 

	
 

	
 
def set_text(soup, text_id, text, resize=None):
 
    '''
 
    Set the text value of an element (via beautiful soup calls).
 
    '''
 
    elem = soup.find(".//*[@id='%s']/{http://www.w3.org/2000/svg}tspan" % text_id)
 
    if elem is None:
 
        raise ValueError('could not find tag id=%s' % text_id)
 
    elem.text = text
 
    if resize:
 
        style = elem.get('style')
 
        elem.set('style', style.replace('font-size:60px', 'font-size:%dpx' % resize))
 

	
 

	
 
def set_colour(soup, slice_id, colour):
 
    '''
 
    Set colour of an element (using beautiful soup calls).
 
    '''
 
    elem = soup.find(".//*[@id='%s']" % slice_id)
 
    if elem is None:
 
        raise ValueError('could not find tag id=%s' % slice_id)
 
    style = elem.get('style')
 
    elem.set('style', style.replace('fill:#316a9a', 'fill:#%s' % colour))
 

	
 
Volunteers = Group.objects.filter(name='Conference volunteers').first().user_set.all()
 
Organisers = Group.objects.filter(name='Conference organisers').first().user_set.all()
 
## It's possible that this script will be run before the database has been populated
 
try:
 
    Volunteers = Group.objects.filter(name='Conference volunteers').first().user_set.all()
 
    Organisers = Group.objects.filter(name='Conference organisers').first().user_set.all()
 
except (OperationalError, AttributeError):
 
    Volunteers = []
 
    Organisers = []
 

	
 
def is_volunteer(attendee):
 
    '''
 
    Returns True if attendee is in the Conference volunteers group.
 
    False otherwise.
 
    '''
 
    return attendee.user in Volunteers
 

	
 
def is_organiser(attendee):
 
    '''
 
    Returns True if attendee is in the Conference volunteers group.
 
    False otherwise.
 
    '''
 
    return attendee.user in Organisers
 

	
 

	
 
def svg_badge(soup, data, n):
 
    '''
 
    Do the actual "heavy lifting" to create the badge SVG
 
    '''
 

	
 
    # Python2/3 compat ...
 
    try:
 
        xx = filter(None, [1, 2, None, 3])[2]
 
        filter_None = lambda lst: filter(None, lst)
 
    except (TypeError,):
 
        filter_None = lambda lst: list(filter(None, lst))
 

	
 
    side = 'lr'[n]
 
    for tb in 'tb':
 
        part = tb + side
 
        lines = [data['firstname'], data['lastname']]
 
        if data['promote_company']:
 
            lines.append(data['company'])
 
        lines.extend([data['line1'], data['line2']])
 
        lines = filter_None(lines)[:4]
 

	
 
        lines.extend('' for n in range(4-len(lines)))
 
        prev = 9999
 
        for m, line in enumerate(lines):
 
            size = text_size(line, prev)
 
            set_text(soup, 'line-%s-%s' % (part, m), line, size)
 
            prev = size
 

	
 
        lines = []
 
        if data['organiser']:
 
            lines.append('Organiser')
 
            set_colour(soup, 'colour-' + part, '319a51')
0 comments (0 inline, 0 general)