def get(self): self.template_values["title"] = "redirectioneaza 2%" try: list_keys = NgoEntity.query(NgoEntity.active == True).fetch( keys_only=True) list_keys = sample(list_keys, 4) ngos = get_multi(list_keys) except Exception, e: info(e) ngos = NgoEntity.query(NgoEntity.active == True).fetch(4)
def get(self): # TODO: readd admin login self.template_values["title"] = "Admin" try: projection = [ NgoEntity.name, NgoEntity.county, NgoEntity.verified, NgoEntity.email ] ngos = NgoEntity.query().fetch(projection=projection) except Exception, e: ngos = NgoEntity.query().fetch()
def get(self, ngo_url): ngo = NgoEntity.get_by_id(ngo_url) if not ngo: self.abort(404) # if we have an form created for this ngo, return the url # if ngo.form_url: # self.redirect( str(ngo.form_url), abort=True ) # else, create a new one and upload to GCS for future use ngo_dict = { "name": ngo.name, "cif": ngo.cif, "account": ngo.account, "special_status": ngo.special_status } pdf = create_pdf({}, ngo_dict) # filename = "Formular 2% - {0}.pdf".format(ngo.name) filename = "Formular_donatie.pdf".format(ngo.name) ong_folder = security.hash_password(ngo.key.id(), "md5") path = "{0}/{1}/{2}".format(USER_UPLOADS_FOLDER, str(ong_folder), filename) file_url = CloudStorage.save_file(pdf, path) # close the file after it has been uploaded pdf.close() ngo.form_url = file_url ngo.put() self.redirect(str(ngo.form_url))
def get(self, ngo_url): ngo = NgoEntity.get_by_id(ngo_url) # if we didn't find it or the ngo doesn't have an active page if ngo is None or ngo.active == False: self.error(404) return # if we still have a cookie from an old session, remove it if "donor_id" in self.session: self.session.pop("donor_id") if "has_cnp" in self.session: self.session.pop("has_cnp") # also we can use self.session.clear(), but it might delete the logged in user's session self.template_values["title"] = "Donatie 2%" # make sure the ngo shows a logo ngo.logo = ngo.logo if ngo.logo else DEFAULT_NGO_LOGO self.template_values["ngo"] = ngo self.template_values["counties"] = LIST_OF_COUNTIES self.template_values['limit'] = DONATION_LIMIT # the ngo website ngo_website = ngo.website if ngo.website else None if ngo_website: # try and parse the the url to see if it's valid try: url_dict = urlparse(ngo_website) if not url_dict.scheme: url_dict = url_dict._replace(scheme='http') # if we have a netloc, than the url is valid # use the netloc as the website name if url_dict.netloc: self.template_values[ "ngo_website_description"] = url_dict.netloc self.template_values["ngo_website"] = url_dict.geturl() # of we don't have the netloc, when parsing the url # urlparse might send it to path # move that to netloc and remove the path elif url_dict.path: url_dict = url_dict._replace(netloc=url_dict.path) self.template_values[ "ngo_website_description"] = url_dict.path url_dict = url_dict._replace(path='') self.template_values["ngo_website"] = url_dict.geturl() else: raise except Exception, e: self.template_values["ngo_website"] = None
def get(self): # self.abort(404) self.template_values["title"] = "Asociatii" ngos = NgoEntity.query(NgoEntity.active == True).fetch() self.template_values["ngos"] = ngos self.template_values["DEFAULT_NGO_LOGO"] = DEFAULT_NGO_LOGO # render a response self.render()
def get(self): # get all the visible ngos ngos = NgoEntity.query(NgoEntity.active == True).fetch() response = [] for ngo in ngos: response.append({ "name": ngo.name, "url": self.uri_for('twopercent', ngo_url=ngo.key.id()), "logo": ngo.logo if ngo.logo else DEFAULT_NGO_LOGO }) self.return_json(response)
def get(self): # get all the ngos ngos = NgoEntity.query().fetch() info('Removing form_url from {0} ngos.'.format(len(ngos))) # to_save = [] # loop through them and remove the form_url # this will force an update on it when downloaded again for ngo in ngos: ngo.form_url = None to_save.append(ngo) ndb.put_multi(to_save)
def get(self, ngo_url): # TODO: readd admin login ngo = NgoEntity.get_by_id(ngo_url) if ngo is None: self.error(404) return self.template_values["ngo_upload_url"] = self.uri_for( "api-ngo-upload-url") self.template_values["counties"] = LIST_OF_COUNTIES self.template_values["ngo"] = ngo self.template_values["other_emails"] = ', '.join( str(x) for x in ngo.other_emails) if ngo.other_emails else "" # render a response self.render()
def post(self, ngo_url): post = self.request errors = { "fields": [], "server": False } self.ngo = NgoEntity.get_by_id(ngo_url) if self.ngo is None: self.error(404) return # if we have an ajax request, just return an answer self.is_ajax = self.request.get("ajax", False) def get_post_value(arg, add_to_error_list=True): value = post.get(arg) # if we received a value if value: # it should only contains alpha numeric, spaces and dash if re.match(r'^[\w\s.\-ăîâșț]+$', value, flags=re.I | re.UNICODE) is not None: # additional validation if arg == "cnp" and len(value) != 13: errors["fields"].append(arg) return "" return value # the email has the @ so the first regex will fail elif arg == 'email': # if we found a match if re.match(r'[^@]+@[^@]+\.[^@]+', value) is not None: return value errors["fields"].append(arg) return '' else: errors["fields"].append(arg) elif add_to_error_list: errors["fields"].append(arg) return "" donor_dict = {} # the donor's data donor_dict["first_name"] = get_post_value("nume").title() donor_dict["last_name"] = get_post_value("prenume").title() donor_dict["father"] = get_post_value("tatal").title() donor_dict["cnp"] = get_post_value("cnp", False) donor_dict["email"] = get_post_value("email").lower() donor_dict["tel"] = get_post_value("tel", False) donor_dict["street"] = get_post_value("strada").title() donor_dict["number"] = get_post_value("numar", False) # optional data donor_dict["bl"] = get_post_value("bloc", False) donor_dict["sc"] = get_post_value("scara", False) donor_dict["et"] = get_post_value("etaj", False) donor_dict["ap"] = get_post_value("ap", False) donor_dict["city"] = get_post_value("localitate").title() donor_dict["county"] = get_post_value("judet") # if he would like the ngo to see the donation donor_dict['anonymous'] = post.get('anonim') != 'on' # the ngo data ngo_data = { "name": self.ngo.name, "account": self.ngo.account.upper(), "cif": self.ngo.cif, "special_status": self.ngo.special_status } if len(errors["fields"]): self.return_error(errors) return captcha_response = submit(post.get(CAPTCHA_POST_PARAM), CAPTCHA_PRIVATE_KEY, self.request.remote_addr) # if the captcha is not valid return if not captcha_response.is_valid: errors["fields"].append("codul captcha") self.return_error(errors) return # the user's folder name, it's just his md5 hashed db id user_folder = security.hash_password('123', "md5") # a way to create unique file names # get the local time in iso format # run that through SHA1 hash # output a hex string filename = "{0}/{1}/{2}".format(USER_FORMS, str(user_folder), sha1( datetime.datetime.now().isoformat() ).hexdigest()) pdf = create_pdf(donor_dict, ngo_data) file_url = CloudStorage.save_file(pdf, filename) # close the file after it has been uploaded pdf.close() # create the donor and save it donor = Donor( first_name = donor_dict["first_name"], last_name = donor_dict["last_name"], city = donor_dict["city"], county = donor_dict["county"], email = donor_dict['email'], tel = donor_dict['tel'], anonymous = donor_dict['anonymous'], # make a request to get geo ip data for this user geoip = self.get_geoip_data(), ngo = self.ngo.key, pdf_url = file_url ) donor.put() # set the donor id in cookie self.session["donor_id"] = str(donor.key.id()) self.session["has_cnp"] = bool(donor_dict["cnp"]) # send and email to the donor with a link to the PDF file self.send_email("twopercent-form", donor) # if not an ajax request, redirect if self.is_ajax: self.response.set_status(200) response = { "url": self.uri_for("ngo-twopercent-success", ngo_url=ngo_url), "form_url": file_url } self.response.write(json.dumps(response)) else: self.redirect( self.uri_for("ngo-twopercent-success", ngo_url=ngo_url) )
def check_ngo_url(ngo_id=None): if not ngo_id: return False return NgoEntity.query(NgoEntity.key == Key("NgoEntity", ngo_id)).count( limit=1) == 0
def post(self): user = self.user if user is None: # TODO: readd admin login #if users.is_current_user_admin(): #user = users.get_current_user() if True: old_ngo_key = self.request.get( 'old-ong-url') if self.request.get('old-ong-url') else 1 user.ngo = Key(NgoEntity, old_ngo_key) else: self.abort(403) self.template_values["user"] = user self.template_values["ngo"] = {} self.template_values["counties"] = LIST_OF_COUNTIES # self.template_values["check_ngo_url"] = "/api/ngo/check-url/" # self.template_values["ngo_upload_url"] = self.uri_for("api-ngo-upload-url") ong_nume = self.request.get('ong-nume') ong_logo_url = self.request.get('ong-logo-url') # this file should be received only if js is disabled ong_logo = self.request.get( 'ong-logo') if ong_logo_url is None else None ong_descriere = self.request.get('ong-descriere') ong_tel = self.request.get('ong-tel', "") ong_email = self.request.get('ong-email', "") ong_website = self.request.get('ong-website', "") ong_adresa = self.request.get('ong-adresa') ong_judet = self.request.get('ong-judet', "") ong_cif = self.request.get('ong-cif') ong_account = self.request.get('ong-cont') ong_special_status = self.request.get('special-status') == "on" ong_url = self.request.get('ong-url') # validation if not ong_nume or not ong_descriere or not ong_adresa or not ong_url or not ong_cif or not ong_account: self.template_values["errors"] = incomplete_form_data self.render() return # if the user already has an ngo, update it if user.ngo: ngo = user.ngo.get() # if the user has an ngo attached but it's not found, skip this and create a new one if ngo is not None: # if the name, cif or bank account changed, remove the form url so we create it again if ong_nume != ngo.name or ong_cif != ngo.cif or ong_account != ngo.account: # if we encounter validation errors later, this will not get saved # so it's safe to do it here ngo.form_url = None ngo.name = ong_nume ngo.description = ong_descriere ngo.logo = ong_logo_url ngo.address = ong_adresa ngo.county = ong_judet ngo.email = ong_email ngo.website = ong_website ngo.tel = ong_tel ngo.special_status = ong_special_status # if no one uses this CIF if ong_cif != ngo.cif: cif_unique = NgoEntity.query( NgoEntity.cif == ong_cif).count(limit=1) == 0 if cif_unique: ngo.cif = ong_cif else: self.template_values["unique"] = False self.render() return # and no one uses this bank account if ong_account != ngo.account: acc_unique = NgoEntity.query( NgoEntity.account == ong_account).count(limit=1) == 0 if acc_unique: ngo.account = ong_account else: self.template_values["unique"] = False self.render() return # TODO: readd admin login #if users.is_current_user_admin(): if False: ngo.verified = self.request.get('ong-verificat') == "on" ngo.active = self.request.get('ong-activ') == "on" # if we want to change the url if ong_url != ngo.key.id(): is_ngo_url_available = check_ngo_url(ong_url) if is_ngo_url_available == False: self.template_values["errors"] = url_taken self.render() return new_key = Key(NgoEntity, ong_url) # replace all the donors key donors = Donor.query(Donor.ngo == ngo.key).fetch() if donors: for donor in donors: donor.ngo = new_key donor.put() # replace the users key ngos_user = User.query(Donor.ngo == ngo.key).get() if ngos_user: ngos_user.ngo = new_key ngos_user.put() # copy the old model new_ngo = ngo # delete the old model ngo.key.delete() # add a new key new_ngo.key = new_key ngo = new_ngo # save the changes ngo.put() # TODO: readd admin login #if users.is_current_user_admin(): #self.redirect(self.uri_for("admin-ong", ngo_url=ong_url)) #else: #self.redirect(self.uri_for("asociatia")) return # create a new ngo entity # do this before validating the url, cif and back account because if we have errors # to at least prepopulate the form on refresh new_ngo = NgoEntity(id=ong_url, name=ong_nume, description=ong_descriere, logo=ong_logo_url, email=ong_email, website=ong_website, tel=ong_tel, address=ong_adresa, county=ong_judet, cif=ong_cif, account=ong_account, special_status=ong_special_status) # check for unique url is_ngo_url_available = check_ngo_url(ong_url) if is_ngo_url_available == False: self.template_values["errors"] = url_taken new_ngo.key = None self.template_values["ngo"] = new_ngo self.render() return unique = NgoEntity.query( OR(NgoEntity.cif == ong_cif, NgoEntity.account == ong_account)).count(limit=1) == 0 if not unique: # asks if he represents the ngo self.template_values["errors"] = not_unique self.render() return else: self.template_values["errors"] = True # TODO: readd admin login #if users.is_current_user_admin(): if False: # a list of email addresses new_ngo.other_emails = [ s.strip() for s in self.request.get('alte-adrese-email', "").split(",") ] new_ngo.put() self.redirect(self.uri_for("admin-ong", ngo_url=ong_url)) else: # link this user with the ngo # the ngo has a key even though we haven't saved it, we offered it an unique id user.ngo = new_ngo.key # use put_multi to save rpc calls put_multi([new_ngo, user]) # do a refresh self.redirect(self.uri_for("contul-meu"))