def manage_persons(request): persons = Person.objects.all() form_data = request.POST if request.POST else None if form_data: # debug info print(form_data) # POST data (for debug) form_add = AddPersonForm(form_data) if form_add.is_valid(): new_person = Person( short_name=form_data.get('short_name'), full_name=form_data.get('full_name'), email=form_data.get('email') ) new_person.save() # write new row to DB return redirect('/persons/') form_rem = RemovePersonForm(form_data) if form_rem.is_valid(): persons_to_be_removed = Person.objects.filter( short_name__icontains=form_data.get('short_name_substr')) persons_to_be_removed.delete() # delete row from DB return redirect('/persons/') t = loader.get_template('manage.html') return HttpResponse(t.render(RequestContext(request, {'form_add' : form_add, 'form_rem' : form_rem, 'persons_count' : len(persons),} )))
class sPerson: def __init__(self, surname, name, gender, pseudonym, birth_year, death_year): self.last_name = surname self.first_name = name self.gender = gender self.pseudonym = pseudonym self.birth_year = birth_year self.death_year = death_year try: self.instance = Person.objects.get(first_name=self.first_name, last_name=self.last_name, birth_year=birth_year) self.load = True except: self.instance = Person(first_name=self.first_name, last_name=self.last_name, birth_year=birth_year) self.load = False self.instance.save() def __repr__(self): m = str(self.first_name) + ' | ' + str(self.last_name) + ' | ' + str( self.gender) m += ' | ' + str(self.pseudonym) return m def __eq__(self, other): return self.__dict__ == other.__dict__
def update_persons(self, start, batch_size = 100, verbosity = 1): '''Updates persons from etvnet.''' cred = Credentials(ETVNET_PERSONS_PUBLIC_KEY, ETVNET_PERSONS_PRIVATE_KEY) conn = etvIntegrationConnection(cred, ETVNET_PERSONS_ENDPOINT) result = conn.make_request( 'GET', '/integration/person_list.json', args={'start': start, 'batch_size': batch_size, 'type': 'person'} ) data = simplejson.load(result) result = None if data: for p in data: try: person = Person.objects.get(external_ident=p.get('id')) except Person.DoesNotExist: person = Person(external_ident=p.get('id')) person.first_name = p.get('first_name') person.last_name = p.get('last_name') person.name = p.get('name') person.birthday = p.get('birthday') person.death = p.get('death') person.is_available = p.get('is_approved') person.updated = result = p.get('updated') person.url = p.get('url') if verbosity >= 2: print( "id: %s name: %s (%s)" \ % (p.get('id'), p.get('name').encode('utf-8'), person.pk and 'updated' or 'created')) person.save() return result
def index(request): today = date.today() sDate = str(today.year) + '-01-01' eDate = str(today.year) + '-' + str(today.month) + '-' + str(today.day) sM = 1 eM = today.month if request.GET.get('range'): dates = request.GET.get('range').split(sep=" - ") sDate = dates[0] eDate = dates[1] sM = int(sDate.split('-')[1]) eM = int(eDate.split('-')[1]) if request.user.person: person = request.user.person else: person = Person() person.name = request.user.username person.owner = request.user person.save() request.user.person = person request.user.save() months = [] for i in range(sM, eM + 1): months.append(getMonthText(i)) # get working wSales = graphWork(request, Sale, sDate, eDate, months) wSalesReturns = graphWork(request, SalesReturn, sDate, eDate, months) wPurchases = graphWork(request, Purchase, sDate, eDate, months) wPurchasesReturns = graphWork(request, PurchasesReturn, sDate, eDate, months) wExpenses = graphWork(request, Expense, sDate, eDate, months) args = { 'nav': 'charts', 'months': months, 'sales': wSales['list'], 'total_sales': wSales['total'], 'sales_returns': wSalesReturns['list'], 'total_sales_returns': wSalesReturns['total'], 'purchases': wPurchases['list'], 'total_purchases': wPurchases['total'], 'purchases_returns': wPurchasesReturns['list'], 'total_purchases_returns': wPurchasesReturns['total'], 'expenses': wExpenses['list'], 'total_expenses': wExpenses['total'], 'startDate': sDate, 'endDate': eDate, 'range': sDate + ' - ' + eDate, } return render(request, 'charts/index.html', args)
def test_save(self): person = Person(name="Hans Müller") person.save() self.assertIsNotNone(person.id) self.assertEquals("hans-muller", person.slug) person = Person(name="J. T. Do") person.save() self.assertIsNotNone(person.id) self.assertEquals("j-t-do", person.slug)
def after_user_activation(sender, user, request, **kwargs): person = Person() person.owner = user person.name = user.username person.save() groups = Group.objects.all() if groups: for group in groups: if group.name.find('Owner') != -1 or group.name.find('owner') != -1: user.groups.add(group) user.person = person user.save()
def _person_by_cnd(self, cnd): ''' use the mk_id (if there) or the name to return the person ''' if cnd.mk_id: try: return Person.objects.get(mk_id=cnd.mk_id) except Person.DoesNotExist: pass try: return Person.objects.get_by_name(cnd.name) except Person.DoesNotExist: person = Person(name=cnd.name) if cnd.mk_id: person.mk_id = mk_id person.save()
def register(request): form = UserCreationForm(request.POST or None) if request.method == 'POST': if form.is_valid(): form.save() user = form.instance user.backend = 'django.contrib.auth.backends.ModelBackend' created_person = Person(user=user) created_person.populate_notification_settings() created_person.save() login(request, user) return redirect('/register_success/') csrf(request) return render(request, 'register.html', locals())
def _person_by_cnd(self, cnd): ''' use the mk_id (if there) or the name to return the person ''' if cnd.mk_id: try: return Person.objects.get(mk_id = cnd.mk_id) except Person.DoesNotExist: pass try: return Person.objects.get_by_name(cnd.name) except Person.DoesNotExist: person = Person(name = cnd.name) if cnd.mk_id: person.mk_id = mk_id person.save()
def add_person(request): if request.method != 'POST': return HttpResponseBadRequest('You can only use POST') else: short_name = request.POST['name'] full_name = request.POST['full_name'] email = request.POST['email'] job = choice(Job.objects.all()) new_person = Person(short_name = short_name, full_name = full_name, email = email, job = job) new_person.save() return HttpResponse('new person "%s" is added' % Person.objects.get(short_name = short_name).short_name)
def add_person(request): if request.method != 'POST': return HttpResponseBadRequest('You can only use POST') else: short_name = request.POST['name'] full_name = request.POST['full_name'] email = request.POST['email'] job = choice(Job.objects.all()) new_person = Person(short_name=short_name, full_name=full_name, email=email, job=job) new_person.save() return HttpResponse( 'new person "%s" is added' % Person.objects.get(short_name=short_name).short_name)
def make_persons(filename_text='data/Person.xlsx'): d = pd.read_excel(filename_text) o = [] for line in d.values: p_id, lname, fname, func, pseud, sex, dob, dod, pob, pod, res, trav, ac, notes = line if type(lname) == type(fname) == float: continue print(lname, fname) pseud = extract_names(pseud) pseuds = [ load_model_instance(x, 'Pseudonym', 'persons') for x in pseud ] by, dy = extract_number(dob), extract_number(dod) o = Person(first_name=_ms(fname), last_name=_ms(lname), sex=set_sex(sex), birth_year=by, death_year=dy, notes=_ms(notes)) s, a, e = save_model([o], 'Persons', verbose=False) if len(s) != 1: continue [o.pseudonym.add(pseud) for pseud in pseuds] o.save() return o
#! /usr/bin/env python # import and setup Django environment (as "./manage.py shell" does): from sys import argv, path as sys_path import os import django sys_path.append('..') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example.settings") django.setup() # actual import script: from persons.models import Person if __name__ == '__main__': if len(argv) != 2: print('USAGE: $ ./import.py /some/path/file.txt') else: imported_persons_count = 0 with open(argv[1], 'r') as input_file: for line in input_file: short, full, mail = line.split('\t') person = Person(short_name=short, full_name=full, email=mail) person.save() # write new row to DB imported_persons_count += 1 print('Imported %s persons.' % imported_persons_count)
from sys import argv, path as sys_path import os from random import choice import django sys_path.append('..') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example.settings") django.setup() # actual import script: from persons.models import Person from jobs.models import Job if __name__ == '__main__': if len(argv) != 2: print('USAGE: $ ./import.py /some/path/file.txt') else: imported_persons_count = 0 with open(argv[1], 'r') as input_file: for line in input_file: short, full, mail = line.split('\t') job = choice(Job.objects.all()) person = Person(short_name=short, full_name=full, email=mail, job=job) person.save() # write new row to DB imported_persons_count += 1 print('Imported %s persons.' % imported_persons_count)