def save(request, contact_type, contact_id = None): if contact_id: person = Person.objects.get(id=contact_id) else: person = Person() person_form = PersonForm(instance=person) if request.method == 'POST': person_form = PersonForm(request.POST) if person_form.is_valid(): person = person_form.save(commit=False) if contact_id: person.id = contact_id person.save() #if contact_id: #person.logs.create(log="Updated contact " + person.first_name + " " + #person.last_name,user=request.user) #else: #person.logs.create(log="Added contact " + person.first_name + " " + person.last_name,user=request.user) formset_list = {'email_formset' : EmailFormSet(instance=person,data=request.POST), 'phone_formset' : PhoneFormSet(instance=person,data=request.POST), 'im_formset' : InstantMessagingFormSet(instance=person,data=request.POST), 'address_formset' : AddressFormSet(instance=person,data=request.POST), 'website_formset' : WebsiteFormSet(instance=person,data=request.POST),} for formset in formset_list: #print formset_list[formset].is_valid() if formset_list[formset].is_valid(): formset_list[formset].save() formset_list[formset] = type(formset_list[formset])(instance=person) else: formset_list = {'email_formset' : EmailFormSet(instance=person), 'phone_formset' : PhoneFormSet(instance=person), 'im_formset' : InstantMessagingFormSet(instance=person), 'address_formset' : AddressFormSet(instance=person), 'website_formset' : WebsiteFormSet(instance=person),} return render_to_response('contacts/save.html', { 'form' : person_form, 'person' : person, 'formsets' : formset_list, }, context_instance=RequestContext(request) )
def execute(srv, usr, pwd): # server = Server('srv1.test.ru') server = Server(srv) conn = Connection(server, auto_bind=True, user=usr, password=pwd) #'userAccountControl' can have these values: 512 Enabled Account; 514 Disabled Account; # conn.search('ou=АСУ,dc=test,dc=ru','(&(objectClass=person)(userAccountControl=66048))',SUBTREE, attributes =['cn','proxyAddresses','department','sAMAccountName', 'displayName', 'telephoneNumber', 'ipPhone', 'streetAddress','title','manager','objectGUID','company','lastLogon']) conn.search( "dc=test,dc=ru", "(&(objectCategory=Person)(!(UserAccountControl:1.2.840.113556.1.4.803:=2)))", SUBTREE, attributes=[ "cn", "proxyAddresses", "department", "sAMAccountName", "displayName", "telephoneNumber", "ipPhone", "streetAddress", "title", "manager", "objectGUID", "company", "lastLogon", ], ) # get or create default organization try: org = Organization.objects.get(name="Не указано") except: org = Organization(name="Не указано") org.save() # get or create default organization try: dep = Subdivision.objects.get(name="Не указано") except: dep = Subdivision(organization=org, name="Не указано") dep.save() for entry in conn.entries: print("") print(" -------------------------- ") print("entry.cn", entry.cn) # print('entry.objectGUID', entry.objectGUID) # search person in contact app try: displayName = entry.displayName except: displayName = entry.cn print("displayName", displayName) try: person_obj = Person.objects.get(ad_objectguid=entry.objectGUID) except: person_obj = None print("person_obj", person_obj) if person_obj: person_obj.name = displayName # person_obj.ad_objectguid = entry.objectGUID person_obj.save() else: person_obj = Person(name=displayName, ad_objectguid=entry.objectGUID) person_obj.save() print("Человек: ", person_obj) try: ldapinfo_obj = LdapInfo.objects.get(person=person_obj) except: ldapinfo_obj = None # add AD sAMAccountName if ldapinfo_obj: try: ldapinfo_obj.samaccountname = entry.sAMAccountName ldapinfo_obj.save() except: pass else: try: sAMAccountName = LdapInfo(person=person_obj, samaccountname=entry.sAMAccountName) sAMAccountName.save() except: pass # add AD lastLogon if ldapinfo_obj: try: ldapinfo_obj.lastlogon = convert_ad_timestamp(int(entry.lastLogon.value)) ldapinfo_obj.save() except: pass else: try: lastLogon = LdapInfo(person=person_obj, lastlogon=convert_ad_timestamp(int(entry.lastLogon.value))) lastLogon.save() except: pass # delete all person phones in contact app phones = Phone.objects.filter(person=person_obj) if phones: phones.delete() # add phones to contact from Active Directory user entry try: newphone = Phone(person=person_obj, number=entry.telephoneNumber) newphone.save() except: pass try: newphone = Phone(person=person_obj, number=entry.ipPhone) newphone.save() except: pass # delete all emails in contact app emails = Email.objects.filter(person=person_obj) if emails: emails.delete() # add emails to contact app from Active Directory user entry try: new_email = Email(person=person_obj, address=entry.proxyAddresses) new_email.save() except: pass # get organization try: entry_company = entry.company except: entry_company = None organization = None if entry_company: try: organization_obj = Organization.objects.get(name=entry.company) except: organization_obj = Organization(name=entry.company) organization_obj.save() else: organization_obj = org print("Организация: ", organization_obj.name) entry_company = None # get or create subdivision try: entry_subdiv = entry.department except: entry_subdiv = None subdiv = None if entry_subdiv: print("len entry.department %s: %s" % (entry.department, len(entry.department))) try: subdiv = Subdivision.objects.get(organization=organization_obj, name=entry.department) except: subdiv = Subdivision(organization=organization_obj, name=entry.department) subdiv.save() else: subdiv = dep print("Подразделение: ", subdiv.name) entry_subdiv = None # get person position try: position = entry.title except: position = None print("Должность", position) # create employee employee = None try: employee = Employee.objects.get( organization=organization_obj, subdivision=subdiv, person=person_obj, position=position ) print("Найден работник") except: employee = Employee(organization=organization_obj, subdivision=subdiv, person=person_obj, position=position) employee.save() print("Создан работник")
def post(self, request): message_fr = "Merci pour votre vote Bonne chance pour la tombola RDV le 18 février 2021 sur 2M" message_ar = "شكرا لتصويتكم حظ موفق موعدنا يوم 18 فبراير 2021 على قناة 2M " status = "success" data = request.data temp_person = Person.objects.all().filter(email=data["email"]) config_demo = Config.objects.all().filter(type="demo", active=True) site_demo = False if config_demo.count() == 1: site_demo = True if temp_person.count() > 0: # and site_demo == False message_fr = "Vote non enregistré. Vous avez déjà voté pour votre candidat préféré." message_ar = "لم يتم تسجيل التصويت. لقد قمتم بالفعل بالتصويت لمرشحكم المفضل. " return Response({"status": "error", "message_fr": message_fr, "message_ar": message_ar, "uid_ref": "", "site_demo": site_demo}, content_type="application/json", status=200) uid_ref = None candidat = Candidat.objects.all().filter(uid=data["candidat"]) config_vote = Config.objects.all().filter(type="vote", active=True) if candidat.count() == 1 and config_vote.count() > 0: candidat = candidat[0] uid_ref = self.generateUID() + "-" + self.generateUID() person = Person() person.name = data["name"] person.email = data["email"] person.phone = str(data["phone"]) person.uid = self.generateUID() person.uid_ref = uid_ref person.accept_reglement = True person.save() vote = Vote() vote.candidat = candidat vote.person = person vote.uid = self.generateUID() vote.project = candidat.project vote.save() candidat.total_votes = candidat.total_votes + 1 candidat.save() else: if config_vote.count() == 0 : status = "error" message_fr = "Le vote est fermé, Merci pour votre visite." message_ar = "لقد تم إغلاق التصويت، شكرا لزيارتكم." else: status = "error" message_fr = "Candidat n'existe plus, Merci de réessayer plus tard" message_ar = "لم يعد المرشح موجودًا ، يرجى المحاولة مرة أخرى لاحقًا" return Response({ "status" : status, "message_fr" : message_fr, "message_ar" : message_ar, "uid_ref" : uid_ref, "site_demo" : site_demo}, content_type="application/json", status=200)
def execute(srv, usr, pwd): #server = Server('srv1.test.ru') server = Server(srv) conn = Connection(server, auto_bind=True, user=usr, password=pwd) #'userAccountControl' can have these values: 512 Enabled Account; 514 Disabled Account; #conn.search('ou=АСУ,dc=test,dc=ru','(&(objectClass=person)(userAccountControl=66048))',SUBTREE, attributes =['cn','proxyAddresses','department','sAMAccountName', 'displayName', 'telephoneNumber', 'ipPhone', 'streetAddress','title','manager','objectGUID','company','lastLogon']) conn.search( 'dc=test,dc=ru', '(&(objectCategory=Person)(!(UserAccountControl:1.2.840.113556.1.4.803:=2)))', SUBTREE, attributes=[ 'cn', 'proxyAddresses', 'department', 'sAMAccountName', 'displayName', 'telephoneNumber', 'ipPhone', 'streetAddress', 'title', 'manager', 'objectGUID', 'company', 'lastLogon' ]) #get or create default organization try: org = Organization.objects.get(name="Не указано") except: org = Organization(name='Не указано') org.save() #get or create default organization try: dep = Subdivision.objects.get(name="Не указано") except: dep = Subdivision(organization=org, name="Не указано") dep.save() for entry in conn.entries: print('') print(' -------------------------- ') print('entry.cn', entry.cn) #print('entry.objectGUID', entry.objectGUID) #search person in contact app try: displayName = entry.displayName except: displayName = entry.cn print('displayName', displayName) try: person_obj = Person.objects.get(ad_objectguid=entry.objectGUID) except: person_obj = None print('person_obj', person_obj) if person_obj: person_obj.name = displayName #person_obj.ad_objectguid = entry.objectGUID person_obj.save() else: person_obj = Person(name=displayName, ad_objectguid=entry.objectGUID) person_obj.save() print('Человек: ', person_obj) try: ldapinfo_obj = LdapInfo.objects.get(person=person_obj) except: ldapinfo_obj = None #add AD sAMAccountName if ldapinfo_obj: try: ldapinfo_obj.samaccountname = entry.sAMAccountName ldapinfo_obj.save() except: pass else: try: sAMAccountName = LdapInfo(person=person_obj, samaccountname=entry.sAMAccountName) sAMAccountName.save() except: pass #add AD lastLogon if ldapinfo_obj: try: ldapinfo_obj.lastlogon = convert_ad_timestamp( int(entry.lastLogon.value)) ldapinfo_obj.save() except: pass else: try: lastLogon = LdapInfo(person=person_obj, lastlogon=convert_ad_timestamp( int(entry.lastLogon.value))) lastLogon.save() except: pass #delete all person phones in contact app phones = Phone.objects.filter(person=person_obj) if phones: phones.delete() #add phones to contact from Active Directory user entry try: newphone = Phone(person=person_obj, number=entry.telephoneNumber) newphone.save() except: pass try: newphone = Phone(person=person_obj, number=entry.ipPhone) newphone.save() except: pass #delete all emails in contact app emails = Email.objects.filter(person=person_obj) if emails: emails.delete() #add emails to contact app from Active Directory user entry try: new_email = Email(person=person_obj, address=entry.proxyAddresses) new_email.save() except: pass #get organization try: entry_company = entry.company except: entry_company = None organization = None if entry_company: try: organization_obj = Organization.objects.get(name=entry.company) except: organization_obj = Organization(name=entry.company) organization_obj.save() else: organization_obj = org print('Организация: ', organization_obj.name) entry_company = None #get or create subdivision try: entry_subdiv = entry.department except: entry_subdiv = None subdiv = None if entry_subdiv: print("len entry.department %s: %s" % (entry.department, len(entry.department))) try: subdiv = Subdivision.objects.get(organization=organization_obj, name=entry.department) except: subdiv = Subdivision(organization=organization_obj, name=entry.department) subdiv.save() else: subdiv = dep print('Подразделение: ', subdiv.name) entry_subdiv = None #get person position try: position = entry.title except: position = None print('Должность', position) #create employee employee = None try: employee = Employee.objects.get(organization=organization_obj, subdivision=subdiv, person=person_obj, position=position) print('Найден работник') except: employee = Employee(organization=organization_obj, subdivision=subdiv, person=person_obj, position=position) employee.save() print('Создан работник')