def ik_chsmoel(request): if not 'smoel' in request.FILES: raise ValueError, "Missing `smoel' in FILES" if not 'id' in request.POST: raise ValueError, "Missing `id' in POST" user = Es.by_id(request.POST['id']) if not request.user.may_upload_smoel_for(request.user): raise PermissionDenied original = default_storage.open(path.join(settings.SMOELEN_PHOTOS_PATH, str(user.name)) + ".orig", 'wb+') for chunk in request.FILES['smoel'].chunks(): original.write(chunk) original.seek(0) img = Image.open(original) if img._getexif() is not None: orientation = int(img._getexif().get(274, '1')) # Orientation if orientation == 3: img = img.transpose(Image.ROTATE_180) elif orientation == 6: img = img.transpose(Image.ROTATE_270) elif orientation == 8: img = img.transpose(Image.ROTATE_90) width, height = resize_proportional(img.size[0], img.size[1], settings.SMOELEN_WIDTH*2, settings.SMOELEN_HEIGHT*2) img = img.resize((width, height), Image.ANTIALIAS) img.save(default_storage.open(path.join(settings.SMOELEN_PHOTOS_PATH, str(user.name)) + ".jpg", 'w'), "JPEG") Es.notify_informacie('set_smoel', request.user, entity=user) return redirect_to_referer(request)
def ik_chsmoel(request): if 'smoel' not in request.FILES: raise ValueError(_("Missende `smoel' in FILES")) if 'id' not in request.POST: raise ValueError(_("Missende `id' in POST")) user = Es.by_id(request.POST['id']) if not user.name: raise ValueError(_("Entiteit heeft geen naam")) if not request.user.may_upload_smoel_for(request.user): raise PermissionDenied original = default_storage.open( os.path.join(settings.SMOELEN_PHOTOS_PATH, str(user.name)) + ".orig", 'wb+') for chunk in request.FILES['smoel'].chunks(): original.write(chunk) original.seek(0) img = PIL.Image.open(original) if hasattr(img, '_getexif') and img._getexif() is not None: orientation = int(img._getexif().get(274, '1')) # Orientation if orientation == 3: img = img.transpose(PIL.Image.ROTATE_180) elif orientation == 6: img = img.transpose(PIL.Image.ROTATE_270) elif orientation == 8: img = img.transpose(PIL.Image.ROTATE_90) width, height = resize_proportional(img.size[0], img.size[1], settings.SMOELEN_WIDTH * 2, settings.SMOELEN_HEIGHT * 2) img = img.resize((width, height), PIL.Image.ANTIALIAS) img.save( default_storage.open( os.path.join(settings.SMOELEN_PHOTOS_PATH, str(user.name)) + ".jpg", 'w'), "JPEG") Es.notify_informacie('set_smoel', request.user, entity=user) return redirect_to_referer(request)
def _user_detail(request, user): ctx = _entity_detail(request, user) ctx['photosUrl'] = reverse('fotos', kwargs={'path':''}) + \ '?q=tag:'+str(user.name) photos_path = path.join(settings.SMOELEN_PHOTOS_PATH, str(user.name)) if default_storage.exists(photos_path + '.jpg'): img = Image.open(default_storage.open(photos_path + '.jpg')) width, height = img.size if default_storage.exists(photos_path + '.orig'): # smoel was created using newer strategy. Shrink until it fits the # requirements. width, height = resize_proportional(img.size[0], img.size[1], settings.SMOELEN_WIDTH, settings.SMOELEN_HEIGHT) elif width > settings.SMOELEN_WIDTH: # smoel was created as high-resolution image, probably 600px wide width /= 2 height /= 2 else: # smoel was created as normal image, probably 300px wide pass ctx.update({ 'hasPhoto': True, 'photoWidth': width, 'photoHeight': height}) return render_to_response('leden/user_detail.html', ctx, context_instance=RequestContext(request))
def ik_chsmoel(request): if not "smoel" in request.FILES: raise ValueError, _("Missende `smoel' in FILES") if not "id" in request.POST: raise ValueError, _("Missende `id' in POST") user = Es.by_id(request.POST["id"]) if not user.name: raise ValueError, _("Entiteit heeft geen naam") if not request.user.may_upload_smoel_for(request.user): raise PermissionDenied original = default_storage.open(path.join(settings.SMOELEN_PHOTOS_PATH, str(user.name)) + ".orig", "wb+") for chunk in request.FILES["smoel"].chunks(): original.write(chunk) original.seek(0) img = Image.open(original) if hasattr(img, "_getexif") and img._getexif() is not None: orientation = int(img._getexif().get(274, "1")) # Orientation if orientation == 3: img = img.transpose(Image.ROTATE_180) elif orientation == 6: img = img.transpose(Image.ROTATE_270) elif orientation == 8: img = img.transpose(Image.ROTATE_90) width, height = resize_proportional( img.size[0], img.size[1], settings.SMOELEN_WIDTH * 2, settings.SMOELEN_HEIGHT * 2 ) img = img.resize((width, height), Image.ANTIALIAS) img.save(default_storage.open(path.join(settings.SMOELEN_PHOTOS_PATH, str(user.name)) + ".jpg", "w"), "JPEG") Es.notify_informacie("set_smoel", request.user, entity=user) return redirect_to_referer(request)
def get_cache_size(self, cache): c = self.CACHES[cache] if self.rotation in [90, 270]: # rotated height, width = self.size else: # normal width, height = self.size return resize_proportional(width, height, c.maxwidth, c.maxheight)
def photo_size(self): if not self.name: return None path = os.path.join(settings.SMOELEN_PHOTOS_PATH, str(self.name)) if not default_storage.exists(path + '.jpg'): return None img = PIL.Image.open(default_storage.open(path + '.jpg')) width, height = img.size if default_storage.exists(path + '.orig'): # smoel was created using newer strategy. Shrink until it fits the # requirements. width, height = resize_proportional(img.size[0], img.size[1], settings.SMOELEN_WIDTH, settings.SMOELEN_HEIGHT) elif width > settings.SMOELEN_WIDTH: # smoel was created as high-resolution image, probably 600px wide width /= 2 height /= 2 else: # smoel was created as normal image, probably 300px wide pass return width, height
def _entity_detail(request, e): related = sorted(e.get_related(), key=lambda x: (Es.DT_MIN - Es.relation_until(x), Es.entity_humanName(x['with']), Es.entity_humanName(x['how']))) rrelated = sorted(e.get_rrelated(), key=lambda x: (Es.DT_MIN - Es.relation_until(x), Es.entity_humanName(x['how']), Es.entity_humanName(x['who']))) for r in chain(related, rrelated): r['may_end'] = Es.user_may_end_relation(request.user, r) r['id'] = r['_id'] r['until_year'] = (None if r['until'] is None or r['until'] >= now() else Es.date_to_year(r['until'])) r['virtual'] = Es.relation_is_virtual(r) tags = [t.as_primary_type() for t in e.get_tags()] # mapping of year => set of members year_sets = {} for r in rrelated: year = r['until_year'] if year is None: year = 'this' if year not in year_sets: year_sets[year] = set() year_sets[year].add(r['who']) year_counts = {} for year in year_sets: year_counts[year] = len(year_sets[year]) ctx = {'related': related, 'rrelated': rrelated, 'year_counts': year_counts, 'now': now(), 'tags': sorted(tags, key=Es.entity_humanName), 'object': e, 'chiefs': [], 'pipos': [], 'reps': []} for r in rrelated: if r['how'] and Es.relation_is_active(r): if str(r['how'].name) == '!brand-hoofd': r['hidden'] = True ctx['chiefs'].append(r) if str(r['how'].name) == '!brand-bestuurspipo': r['hidden'] = True ctx['pipos'].append(r) if str(r['how'].name) == '!brand-vertegenwoordiger': r['hidden'] = True ctx['reps'].append(r) # Is request.user allowed to add (r)relations? if ('secretariaat' in request.user.cached_groups_names and (e.is_group or e.is_user)): groups = [g for g in Es.groups() if not g.is_virtual] groups.sort(key=Es.entity_humanName) users = sorted(Es.users(), key=Es.entity_humanName) brands = sorted(Es.brands(), key=Es.entity_humanName) ctx.update({'users': users, 'brands': brands, 'groups': groups, 'may_add_related': True, 'may_add_rrelated': True, 'may_tag': True, 'may_untag': True}) ctx['may_upload_smoel'] = e.name and request.user.may_upload_smoel_for(e) if e.is_tag: ctx.update({'tag_bearers': sorted(e.as_tag().get_bearers(), key=Es.entity_humanName)}) # Check whether entity has a photo photos_path = (path.join(settings.SMOELEN_PHOTOS_PATH, str(e.name)) if e.name else None) if photos_path and default_storage.exists(photos_path + '.jpg'): img = PIL.Image.open(default_storage.open(photos_path + '.jpg')) width, height = img.size if default_storage.exists(photos_path + '.orig'): # smoel was created using newer strategy. Shrink until it fits the # requirements. width, height = resize_proportional(img.size[0], img.size[1], settings.SMOELEN_WIDTH, settings.SMOELEN_HEIGHT) elif width > settings.SMOELEN_WIDTH: # smoel was created as high-resolution image, probably 600px wide width /= 2 height /= 2 else: # smoel was created as normal image, probably 300px wide pass ctx.update({ 'hasPhoto': True, 'photoWidth': width, 'photoHeight': height}) return ctx
def _entity_detail(request, e): def _cmp(x, y): r = Es.relation_cmp_until(y, x) if r: return r r = cmp(unicode(x["with"].humanName), unicode(y["with"].humanName)) if r: return r r = cmp(unicode(x["how"].humanName) if x["how"] else None, unicode(y["how"].humanName) if y["how"] else None) if r: return r return Es.relation_cmp_from(x, y) def _rcmp(x, y): r = Es.relation_cmp_until(y, x) if r: return r r = cmp(unicode(x["how"].humanName) if x["how"] else None, unicode(y["how"].humanName) if y["how"] else None) if r: return r r = cmp(unicode(x["who"].humanName), unicode(y["who"].humanName)) if r: return r return Es.relation_cmp_from(x, y) related = sorted(e.get_related(), cmp=_cmp) rrelated = sorted(e.get_rrelated(), cmp=_rcmp) for r in chain(related, rrelated): r["may_end"] = Es.user_may_end_relation(request.user, r) r["id"] = r["_id"] r["until_year"] = None if r["until"] is None or r["until"] >= now() else Es.date_to_year(r["until"]) r["virtual"] = Es.relation_is_virtual(r) tags = [t.as_primary_type() for t in e.get_tags()] # mapping of year => set of members year_sets = {} for r in rrelated: year = r["until_year"] if year is None: year = "this" if not year in year_sets: year_sets[year] = set() year_sets[year].add(r["who"]) year_counts = {} for year in year_sets: year_counts[year] = len(year_sets[year]) ctx = { "related": related, "rrelated": rrelated, "year_counts": year_counts, "now": now(), "tags": sorted(tags, Es.entity_cmp_humanName), "object": e, "chiefs": [], "pipos": [], "reps": [], } for r in rrelated: if r["how"] and Es.relation_is_active(r): if str(r["how"].name) == "!brand-hoofd": r["hidden"] = True ctx["chiefs"].append(r) if str(r["how"].name) == "!brand-bestuurspipo": r["hidden"] = True ctx["pipos"].append(r) if str(r["how"].name) == "!brand-vertegenwoordiger": r["hidden"] = True ctx["reps"].append(r) # Is request.user allowed to add (r)relations? if "secretariaat" in request.user.cached_groups_names and (e.is_group or e.is_user): groups = [g for g in Es.groups() if not g.is_virtual] groups.sort(cmp=lambda x, y: cmp(unicode(x.humanName), unicode(y.humanName))) users = sorted(Es.users(), cmp=Es.entity_cmp_humanName) brands = sorted(Es.brands(), cmp=Es.entity_cmp_humanName) ctx.update( { "users": users, "brands": brands, "groups": groups, "may_add_related": True, "may_add_rrelated": True, "may_tag": True, "may_untag": True, } ) ctx["may_upload_smoel"] = e.name and request.user.may_upload_smoel_for(e) if e.is_tag: ctx.update({"tag_bearers": sorted(e.as_tag().get_bearers(), cmp=Es.entity_cmp_humanName)}) # Check whether entity has a photo photos_path = path.join(settings.SMOELEN_PHOTOS_PATH, str(e.name)) if e.name else None if photos_path and default_storage.exists(photos_path + ".jpg"): img = Image.open(default_storage.open(photos_path + ".jpg")) width, height = img.size if default_storage.exists(photos_path + ".orig"): # smoel was created using newer strategy. Shrink until it fits the # requirements. width, height = resize_proportional( img.size[0], img.size[1], settings.SMOELEN_WIDTH, settings.SMOELEN_HEIGHT ) elif width > settings.SMOELEN_WIDTH: # smoel was created as high-resolution image, probably 600px wide width /= 2 height /= 2 else: # smoel was created as normal image, probably 300px wide pass ctx.update({"hasPhoto": True, "photoWidth": width, "photoHeight": height}) return ctx