def clipboard_paste(folder, nodeids, action): nodes = Node.query.filter(Node.uuid.in_(nodeids)).all() returnids = [] nameconflicts = dict( db.session.query(Node.name, Node.id).filter_by(folder=folder).all()) for node in nodes: if action == 'cut': # Move the node node.folder = folder elif action == 'copy': # Copy and paste data data = node.as_json() newnode = node.__class__(folder=folder) newnode.user = g.user newnode.import_from(data) newnode.import_from_internal(data) newnode.uuid = newid( ) # import_from will copy the UUID. Regenerate it. node = newnode # For the namecheck below # If the name conflicts, give it a new name. maxlength=250 from coaster.sqlalchemy returnids.append(node.uuid) if node.name in nameconflicts: if node.id != nameconflicts[node.name]: node.name = make_name(node.name, maxlength=250, checkused=lambda n: n in nameconflicts) return returnids
def rename(self, title): name = make_name(title) if self.query.filter_by(name=name).first() is not None: raise ValueError(u"Name already in use") else: self.name = name self.title = title
def make_name(self): if self.title: if self.id: checkused = lambda c: self.__class__.query.filter(self.__class__.id != self.id).filter_by(name=c).first() else: checkused = lambda c: self.__class__.query.filter_by(name=c).first() self.name = make_name(self.title, maxlength=250, checkused=checkused)
def label_doesnt_exist(form, field): profile_id = form.profile_id.data label_name = make_name(field.data) if label_name in reserved_words(): raise ValidationError('"%s" is reserved and cannot be used as a label. Please try another name.' % label_name) exists = Label.query.filter_by(profile_id=profile_id, name=label_name).first() if exists: raise ValidationError('Label "%s" already exists. Please try another name.' % field.data)
def make_name(self): if self.title: if self.id: checkused = lambda c: self.__class__.query.filter( self.__class__.id != self.id).filter_by(name=c).first() else: checkused = lambda c: self.__class__.query.filter_by(name=c ).first() self.name = make_name(self.title, maxlength=250, checkused=checkused)
def get(cls, title): tag = cls.query.filter_by(title=title).first() if tag: return tag else: name = make_name(title) # Is this name already in use? If yes, return it tag = cls.query.filter_by(name=name).first() if tag: return tag else: tag = cls(name=name, title=title) db.session.add(tag) return tag
def clipboard_paste(folder, nodeids, action): nodes = Node.query.filter(Node.uuid.in_(nodeids)).all() returnids = [] nameconflicts = dict(db.session.query(Node.name, Node.id).filter_by(folder=folder).all()) for node in nodes: if action == 'cut': # Move the node node.folder = folder elif action == 'copy': # Copy and paste data data = node.as_json() newnode = node.__class__(folder=folder) newnode.user = g.user newnode.import_from(data) newnode.import_from_internal(data) newnode.uuid = newid() # import_from will copy the UUID. Regenerate it. node = newnode # For the namecheck below # If the name conflicts, give it a new name. maxlength=250 from coaster.sqlalchemy returnids.append(node.uuid) if node.name in nameconflicts: if node.id != nameconflicts[node.name]: node.name = make_name(node.name, maxlength=250, checkused=lambda n: n in nameconflicts) return returnids
def contact_exchange(event): if request.method=='GET': return render_template('contact_exchange.html', event=event, debug=str(app.config['DEBUG']).lower(), ui_test=str(request.args.get('ui_test', False)).lower()) if request.method == 'POST': ids = tuple(request.form.getlist('ids[]')) if len(ids) < 2: return jsonify(status=False, error=u'Insufficient users to connect') users = Participant.query.filter(Participant.event_id == event.id, Participant.nfc_id.in_(ids)).all() mail = Mail(app) message = Message("You connected with " + str(len(users) - 1) + " people using ContactExchange") message.cc = list() for user in users: email = '"' + user.name + '"<' + user.email + '>' if message.reply_to is None: message.reply_to = email message.add_recipient(email) else: message.cc.append(email) message.attach(make_name(user.name) + '.vcf', 'text/vcard', render_template('user_card.vcf', user=user, event=event)) message.sender = '"HasGeek"<*****@*****.**>' message.body = render_template('connectemail.md', users=users, event=event) message.html = markdown(message.body) log = CXLog() try: mail.send(message) log.sent = True log.log_message = u"Mail delivered to postfix server" except Exception as error: log.sent = True log.log_message = unicode(error) log.users = u','.join(ids) db.session.add(log) db.session.commit() return jsonify(success=True)
def update_from_user(cls, user, session, parent=None, type_user=None, type_org=None, type_col='type', make_user_profiles=True, make_org_profiles=True): """ Update profiles from the given user's organizations. :param user: User account with organization data. :param session: Database session (typically db.session). :param parent: Parent object, if applicable. :param type_user: Type value for user profiles, if applicable. :param type_org: Type value for organization profiles, if applicable. :param type_col: Column for type value, if applicable. :param bool make_user_profiles: Should user profiles be created? :param bool make_org_profiles: Should organization profiles be created? """ idsnames = {user.userid: {'name': user.profile_name, 'title': user.fullname}} for org in user.organizations_memberof(): idsnames[org['userid']] = {'name': org['name'], 'title': org['title']} namesids = dict([(value['name'], key) for key, value in idsnames.items()]) # First, check if Profile userids and names match for profile in cls.query.filter(cls.name.in_(namesids.keys())).all(): if profile.userid != namesids[profile.name]: # This profile's userid and name don't match. Knock off the name profile.name = make_name(profile.userid, maxlength=250, checkused=lambda c: True if session.query(cls.name).filter_by(name=c).first() else False) # Flush this to the db for constraint integrity session.flush() # Second, check the other way around and keep this list of profiles profiles = dict([(p.userid, p) for p in cls.query.filter(cls.userid.in_(idsnames.keys())).all()]) for profile in profiles.values(): if profile.name != idsnames[profile.userid]['name']: profile.name = idsnames[profile.userid]['name'] if profile.title != idsnames[profile.userid]['title']: profile.title = idsnames[profile.userid]['title'] # Flush this too session.flush() # Third, make new profiles if required if make_user_profiles: if user.userid not in profiles: if parent is not None: profile = cls(userid=user.userid, name=user.profile_name, title=user.fullname, parent=parent) else: profile = cls(userid=user.userid, name=user.profile_name, title=user.fullname) if type_user is not None: setattr(profile, type_col, type_user) session.add(profile) if make_org_profiles: for org in user.organizations_memberof(): if org['userid'] not in profiles: if parent is not None: profile = cls(userid=org['userid'], name=org['name'], title=org['title'], parent=parent) else: profile = cls(userid=org['userid'], name=org['name'], title=org['title']) if type_org is not None: setattr(profile, type_col, type_org) session.add(profile)
def make_name(self): if self.title: self.name = make_name(self.title, maxlength=250)
def __call__(self, form, field): if make_name(field.data) != field.data: raise wtforms.validators.StopValidation(self.message)
def valid_name(form, field): field.data = make_name(field.data)
def __call__(self, form, field): if make_name(field.data) != field.data: raise wtforms.ValidationError(self.message)
def update_from_user(cls, user, session, parent=None, type_user=None, type_org=None, type_col='type', make_user_profiles=True, make_org_profiles=True): """ Update profiles from the given user's organizations. :param user: User account with organization data. :param session: Database session (typically db.session). :param parent: Parent object, if applicable. :param type_user: Type value for user profiles, if applicable. :param type_org: Type value for organization profiles, if applicable. :param type_col: Column for type value, if applicable. :param bool make_user_profiles: Should user profiles be created? :param bool make_org_profiles: Should organization profiles be created? """ idsnames = { user.userid: { 'name': user.profile_name, 'title': user.fullname } } for org in user.organizations_memberof(): idsnames[org['userid']] = { 'name': org['name'], 'title': org['title'] } namesids = dict([(value['name'], key) for key, value in idsnames.items()]) # First, check if Profile userids and names match for profile in cls.query.filter(cls.name.in_(namesids.keys())).all(): if profile.userid != namesids[profile.name]: # This profile's userid and name don't match. Knock off the name profile.name = make_name(profile.userid, maxlength=250, checkused=lambda c: True if session.query(cls.name).filter_by( name=c).first() else False) # Flush this to the db for constraint integrity session.flush() # Second, check the other way around and keep this list of profiles profiles = dict([ (p.userid, p) for p in cls.query.filter(cls.userid.in_(idsnames.keys())).all() ]) for profile in profiles.values(): if profile.name != idsnames[profile.userid]['name']: profile.name = idsnames[profile.userid]['name'] if profile.title != idsnames[profile.userid]['title']: profile.title = idsnames[profile.userid]['title'] # Flush this too session.flush() # Third, make new profiles if required if make_user_profiles: if user.userid not in profiles: if parent is not None: profile = cls(userid=user.userid, name=user.profile_name, title=user.fullname, parent=parent) else: profile = cls(userid=user.userid, name=user.profile_name, title=user.fullname) if type_user is not None: setattr(profile, type_col, type_user) session.add(profile) if make_org_profiles: for org in user.organizations_memberof(): if org['userid'] not in profiles: if parent is not None: profile = cls(userid=org['userid'], name=org['name'], title=org['title'], parent=parent) else: profile = cls(userid=org['userid'], name=org['name'], title=org['title']) if type_org is not None: setattr(profile, type_col, type_org) session.add(profile)