from xml.etree import ElementTree
+# Note: the 'record' parameters passed to these functions must be
+# Unicode strings, not plain Python strings.
+
def marcxml_to_records(rec):
- tree = ElementTree.fromstring(rec)
+ assert isinstance(rec, unicode)
+ tree = ElementTree.fromstring(rec.encode('utf-8'))
if tree.tag == '{http://www.loc.gov/MARC21/slim}collection':
# then we may have multiple records
records = tree.findall('{http://www.loc.gov/MARC21/slim}record')
return records
def record_to_dictionary(record, multiples=True):
- tree = ElementTree.fromstring(record)
+ assert isinstance(record, unicode)
+ tree = ElementTree.fromstring(record.encode('utf-8'))
dct = {}
for df in tree.findall('{http://www.loc.gov/MARC21/slim}datafield'):
t = df.attrib['tag']
return dct
def marcxml_to_dictionary(rec, multiples=False):
- tree = ElementTree.fromstring(rec)
+ assert isinstance(rec, unicode)
+ tree = ElementTree.fromstring(rec.encode('utf-8'))
if tree.tag == '{http://www.loc.gov/MARC21/slim}collection':
# then we may have multiple records
records = tree.findall('{http://www.loc.gov/MARC21/slim}record')
parsed = []
for rec in raw_records:
- try:
- rec = _marc_utf8_pattern.sub(_decode_marc_utf8, rec)
- except 'x':
- raise rec
+ # TODO: fix this ascii/replace, once our z3950/marc encoding
+ # issues are sorted out.
+ rec = unicode(rec, 'ascii', 'replace')
+
+ assert isinstance(rec, unicode) # this must be true.
parsed.append(rec)
return parsed, len(res)
-# decoding MARC \X.. UTF-8 patterns.
-
-_marc_utf8_pattern = re.compile(r'\\X([0-9A-F]{2})', re.I)
-
-def _decode_marc_utf8(regex_match):
- return chr(int(regex_match.group(1), 16))
-
-
#------------------------------------------------------------
# some tests
return self.is_staff or \
bool(callhook('can_create_sites', self))
+ def get_list_name(self):
+ return '%s, %s (%s)' % (self.last_name, self.first_name, self.username)
+
@classmethod
def active_instructors(cls):
"""Return a queryset of all active instructors."""
# Lookup tables
class ServiceDesk(BaseModel):
- name = m.CharField(max_length=100)
+ name = m.CharField(max_length=100, unique=True)
active = m.BooleanField(default=True)
external_id = m.CharField(max_length=256, blank=True, null=True)
return self.name
class Term(BaseModel):
- code = m.CharField(max_length=64)
+ code = m.CharField(max_length=64, unique=True)
name = m.CharField(max_length=256)
start = m.DateField('Start (Y-M-D)')
finish = m.DateField('Finish (Y-M-D)')
+ class Meta:
+ ordering = ['start', 'code']
+
def __unicode__(self):
- return self.code or self.name
+ return '%s: %s' % (self.code, self.name)
+
class Department(BaseModel):
name = m.CharField(max_length=256)
def __unicode__(self):
return self.name
+
class Course(BaseModel):
"""An abstract course (not a course offering.)"""
- code = m.CharField(max_length=64)
+ code = m.CharField(max_length=64, unique=True)
name = m.CharField(max_length=1024)
department = m.ForeignKey(Department)
+ class Meta:
+ ordering = ['code']
+
def __unicode__(self):
- return self.name
+ return '%s: %s' % (self.code, self.name)
+
class Z3950Target(m.Model):
name = m.CharField(max_length=100)
return self.name
class Config(m.Model):
- name = m.CharField(max_length=256)
+ name = m.CharField(max_length=256, unique=True)
value = m.CharField(max_length=8192)
@classmethod
'user__last_name', 'user__first_name')
def can_edit(self, user):
+ """
+ Return True if the user has permission to edit this
+ site. Staff, site owners and site instructors all have editing
+ permissions.
+ """
if user.is_anonymous():
return False
- if user.id == self.owner_id:
+ if (user.id == self.owner_id) or user.is_staff:
return True
try:
mbr = self.members().get(user=user)
- except Member.DoesNotExist:
+ except Membership.DoesNotExist:
return False
return mbr.role in (u'INSTR', u'ASSIST')
"""Return True if the user could feasibly register into this
site: she's not already in it, and the site allows open
registration."""
- return user.is_authenticated() \
- and self.access in ('ANON', 'LOGIN') \
+ return self.access in ('ANON', 'LOGIN') \
and not self.is_member(user)
def is_member(self, user):
assert user
- return user.id == self.owner_id \
- or self.members().filter(user=user).exists()
+ return user.is_authenticated() and (
+ user.id == self.owner_id \
+ or bool(self.members().filter(user=user)))
#------------------------------------------------------------
# User membership in sites
else:
raise ValidationError, _('invalid course code')
+ def __init__(self, *args, **kwargs):
+ owner = self.base_fields['owner']
+ owner.label_from_instance = lambda u: u.get_list_name()
+ owner.queryset = User.objects.order_by('last_name', 'first_name', 'username')
+
+ super(NewSiteForm, self).__init__(*args, **kwargs)
+
+
# if we have course-code lookup, hack lookup support into the new-course form.
COURSE_CODE_LIST = bool(models.campus.course_code_list)
# NewSiteForm.base_fields['code'].empty_label = empty_label
#--------------------
-
+
@login_required
def add_new_site(request):
if not request.user.can_create_sites():
def edit_site(request, site_id):
instance = get_object_or_404(models.Site, pk=site_id)
return _add_or_edit_site(request, instance=instance)
-
+
def _add_or_edit_site(request, instance=None):
is_add = (instance is None)
if is_add:
site.generate_new_passkey()
site.save()
assert site.id
- user_in_site = site.is_member(request.user)
- if not user_in_site: # for edits, might already be!
- mbr = site.member_set.create(user=request.user, role='INSTR')
- mbr.save()
-
+
if is_add or (current_access_level != site.access):
# we need to configure permissions.
return HttpResponseRedirect(site.site_url('edit/permission/'))
<xi:include href="../master.xhtml"/>
<head>
<title>${title}</title>
+ <style type="text/css">
+ ul { margin-bottom: 2em; }
+ </style>
</head>
<body>
<h1>${title}</h1>
<!-- <li><a href="../zsearch/">Search Z39.50 Targets</a></li> -->
<!-- </ul> -->
<ul>
- <li><a href="../djadmin/">Django Administrative UI</a></li>
+ <li><a href="../site/new/">Create a new course site</a></li>
</ul>
<ul>
<li py:if="gethook('department_course_catalogue')">
<a href="update_terms">Automatically update terms</a>
</li>
</ul>
+ <ul>
+ <li><a href="../djadmin/">Advanced Administration</a></li>
+ </ul>
</div>
</body>
</html>
<div py:def="site_banner(site)" py:strip="True">
<div id="sitebanner">
${site_search(site)}
- <div class="deptident">${site.course.department} • ${site.term.name}</div>
+ <div class="deptident">
+ ${site.owner.last_name}
+ • ${site.term.name}
+ • ${site.course.department}</div>
<h1><a href="${site.site_url()}"><span py:if="site.course">${site.course.code}: </span>${site.course.name}</a></h1>
</div>
</div>
py:with="hier=item.hierarchy()[:-1]; lastnum=len(hier)"
class="nestedtitle">
<div id="breadcrumbs">
- <span><a href="${item.site.site_url()}">Top</a></span> »
- <span py:for="n, x in enumerate(hier)"><a href="${x.item_url()}">${x.title}</a> » </span>
- <span class="final_item">${item.title}</span>
+ <span><a href="${item.site.site_url()}">Top</a></span>
+ <span py:for="n, x in enumerate(hier)">
+ » <a href="${x.item_url()}">${x.title}</a>
+ </span>
+ <span py:if="item.item_type=='HEADING'">
+ » <span class="final_item">${item.title}</span>
+ </span>
</div>
</div>
<?python
-from xml.etree import ElementTree as E
from django.utils.simplejson import dumps
from conifer.libsystems.z3950.marcxml import record_to_dictionary
from conifer.libsystems.z3950.marcxml import marcxml_dictionary_to_dc as to_dublin
<td>
<form action="." method="POST">
<!-- !TODO: is utf8 okay here? I shouldn't have to do any decoding here. -->
- <input type="hidden" name="pickitem" value="${unicode(res, 'utf8')}"/>
+ <input type="hidden" name="pickitem" value="${res}"/>
<input type="submit" value="Pick this item"/>
</form>
</td>