def create_phones(base): for i in range(0,100): p = Phone() p.number = '%s%02d' % (base, i) p.department_id = -1 DBSession.add(p) DBSession.flush() transaction.commit()
def main(): args = parse_args() load_config(args.conf_file) if len(DBSession.query(Phone).all())==0: create_phones('4643') create_phones('4752') data = csv.reader(open(args.csv_file,'rb'), delimiter=',') try: dptm = '' for row in data: uname = unicode(re.sub('\W', '', (row[1][0:2] + row[0]).lower()), 'utf-8') dname = unicode(row[0] + ' ' + row[1], 'utf-8') num = unicode(row[3].replace(' ',''), 'utf-8') fax = unicode(row[4].replace(' ',''), 'utf-8') dptm = unicode(row[5], 'utf-8') #print ' * * * ', uname, dname, num, dptm try: p = DBSession.query(Phone).filter(Phone.number==num).one() except NoResultFound, e: # Nouveau tél ??? p = Phone() p.number = num p.department_id = -1 DBSession.add(p) try: d = DBSession.query(Department).filter(Department.name==dptm).one() except NoResultFound, e: # Nouveau département d = Department() d.name = dptm DBSession.add(d) u = User() u.user_name = uname u.display_name = dname u.phone = [p] u.password = u'n5oBwdpytxdvj~Rz1uum' p.department = d DBSession.add(u) DBSession.add(p)
def create(self, **kw): ''' Create phone: Create provisionning file. If an extension is attached to the phone, create exten in Asterisk database. If a user is attached to the phone, add callerid to phone; if the user has email, add voicemail info to sip.conf and add entry in voicemail.conf Create entry in Asterisk sip.conf. ''' # Check exten is not already used if kw['exten']: log.debug('Check exten ' + kw['exten']) p = DBSession.query(Phone).filter(Phone.exten==kw['exten']).all() if len(p): return dict(status='bad_exten') exten = re.sub(r'\D', '', kw['exten']) # Check dnis is not already used if kw['dnis']: log.debug('Check dnis ' + kw['dnis']) p = DBSession.query(Phone).filter(Phone.dnis==kw['dnis']).all() if len(p): return dict(status='bad_dnis') dnis = re.sub(r'\D', '', kw['dnis']) # Generate SIP id and secret while True: sip_id = ''.join([choice(letters + digits) for i in xrange(8)]) log.debug('Generate SIP id: ' + sip_id) try: DBSession.query(Phone).filter(Phone.sip_id==sip_id).one() except: pwd = ''.join([choice(letters + digits) for i in xrange(8)]) break # Configure phone sip_display_name = None mwi_subscribe = 0 need_voicemail_update = False sip_server = server_sip sip_server2 = server_sip2 sip_display_name = '' mwi_subscribe = 0 if kw['user_id']!='-9999': u = DBSession.query(User).get(kw['user_id']) sip_display_name = u.ascii_name if u.email_address: mwi_subscribe = 1 need_voicemail_update = True # Save phone info to database log.debug('Save to database ' + kw['mac']) p = Phone() p.sip_id = sip_id new_phone = None if kw['mac']: new_phone = session['new_phone'] p.mac = kw['mac'] p.vendor = new_phone.vendor p.model = new_phone.model log.debug('%s %s, session %s', new_phone.vendor, new_phone.model, new_phone.sid ) p.password = pwd if kw['exten']: p.exten = exten if kw['dnis']: p.dnis = dnis if kw['dptm_id']!='-9999': p.department_id = kw['dptm_id'] if kw['user_id']!='-9999': p.user_id = kw['user_id'] if 'callgroups' in kw: p.callgroups = ','.join([str(x) for x in kw['callgroups']]) if 'pickupgroups' in kw: p.pickupgroups = ','.join([str(x) for x in kw['pickupgroups']]) if 'contexts' in kw: p.contexts = ','.join([str(x) for x in kw['contexts']]) p.hide_from_phonebook = True if kw['hide_from_phonebook']==u'True' else False p.fax = True if kw['fax']==u'True' else False p.block_cid_in = True if kw['block_cid_in']==u'True' else False p.block_cid_out = True if kw['block_cid_out']==u'True' else False p.priority = True if kw['priority']==u'True' else False p.phonebook_label = kw['phonebook_label'] p.secretary = kw['secretary'] DBSession.add(p) asterisk_update_phone(p) if new_phone: # Create provisionning file if MAC exists log.debug('Configure ' + kw['mac']) new_phone.configure( pwd, directory_tftp, server_firmware + '/phones/firmware', server_config + '/phones/config', server_ntp, server_config, '', '', '', sip_server, sip_id, sip_display_name, mwi_subscribe, screen_url = server_config, exten=p.exten, sip_server2=sip_server2) session.save() flash(u'Nouveau téléphone "%s" créé' % (kw['exten'])) return {'status': 'created'}
#from astportal2.model import * from astportal2.model import init_model, DBSession, Phone config = ConfigParser.ConfigParser({'here': os.getcwd()}) config.read(os.path.join(os.getcwd(), 'development.ini')) sqlalchemy_url = config.get('app:main', 'sqlalchemy.url') engine = create_engine(sqlalchemy_url) init_model(engine) import csv, sys numbers = [p.number for p in DBSession.query(Phone.number).all()] #for i in range(0,400): #for i in range(500,700): for i in range(800,900): n = '2%03d' % i if n not in numbers: p = Phone() p.number = n p.department_id = 28 try: DBSession.add(p) DBSession.flush() except: sys.exit('ERREUR sur poste ' + n) print n, 'OK !' transaction.commit()