def register_user(session, email, password, first_name, last_name): """ Arguments: - `session`: - `email`: - `password`: - `first_name`: - `last_name`: """ name = first_name + " " + last_name password = bcrypt.hashpw(password, bcrypt.gensalt()) default = "http://enforceapp.com/static/img/enforce-avatar-big.png" size = 75 gravatar_url = "http://www.gravatar.com/avatar/" + hashlib.md5(email.lower()).hexdigest() + "?" gravatar_url += urllib.urlencode({"d": default, "s": str(size)}) meta = db.metadata.find_one() user_count = int(meta["user_count"]) user_count = str(user_count + 1) user_slug = make_slug(name) + "-" + user_count user = { "email": email, "first_name": first_name, "last_name": last_name, "avatar": gravatar_url, "name": name, "complaints": [], "upvotes": [], "downvotes": [], "password": password, "user_type": "member", "user_slug": user_slug, "fb": 0, "register_date": datetime.now(), } if not (email or password): return {"error": "email or password is not given"}, 404 else: if check_mail(email): db.users.insert(user) user = cleanup_user_for_session(user) session["user"] = user session["logged_in"] = True user = serialize_user(user) db.metadata.update({"type": "statistics"}, {"$inc": {"user_count": 1}}) return user, 201 else: return {"error": "email address is not valid"}, 404
def notes_to_city(session, city, district, note_title, note_description, note_start_date, note_end_date, location, source, fields, tags, icon): """ Arguments: - `city`: - `note_title`: - `note_full`: - `note_start_date`: if the note is a planned trouble - `note_end_date`: planned solution time - `location`: average location of the problem - `source`: source of the knowledge (izsu, aski etc) - `fields`: effected fields. Eg. (balgat mah, sogutozu etc) - `tags`: tags. Eg. (su kesintisi, elektrik kesintisi) - `icon`: icon of the tag """ sender = None if "gov" in session: sender = session.get("gov")["govname"] sender_city = session.get("gov")["city_slug"] if city != sender_city: return {"error": "not allowed to send notifications"}, 404 elif "user" in session: if session.get("user")["user_type"] == "admin": sender = "enforce" else: return {"error": "not allowed to send notifications"}, 404 if sender is None: return {"error": "sender is null, can't accept"}, 404 slug_city = make_slug(city) slug_title = make_slug(note_title) number = db.metadata.find_one({"type": "statistics"}) number = int(number["notification_count"]) number += 1 slug_url = "/not/" + slug_city + "/" + slug_title + "-" + str(number) start_date = datetime.strptime( note_start_date, "%d-%m-%Y %H:%M" ) end_date = datetime.strptime( note_end_date, "%d-%m-%Y %H:%M" ) fields = fields.split(",") new_fields = [] for f in fields: new_fields.append(make_slug(f.strip())) tags = tags.split(",") new_tags = [] for t in tags: new_tags.append(make_slug(t.strip())) icon = "/static/img/not-icons/" + icon new_note = { "_id": ObjectId(), "city": city, "slug_city": slug_city, "district": district, "slug_district": make_slug(district), "title": note_title, "description": note_description, "insert_date": datetime.now(), "start_date": start_date, "end_date": end_date, "sender": sender, "source": source, "location": location, "slug_url": slug_url, "areas": new_fields, "tags": new_tags, "icon": icon } db.notes.insert(new_note) db.metadata.update( {"type": "statistics"}, {"$inc": {"notification_count": 1}} ) return json_util.dumps(new_note), 201
def post_new_complaint(session, location, title, pic_arr, category): """ Arguments: - `session`: - `location`: - `title`: - `pic_arr`: - `category`: """ user = session.get("user") address_info = get_city_and_address(location) city = address_info[0] address = address_info[1] slug_city = make_slug(city) slug_title = make_slug(title) number = db.metadata.find_one({"type": "statistics"}) number = int(number["complaint_count"]) number += 1 slug_url = "/" + slug_city + "/" + slug_title + "-" + str(number) public_url = "/" + slug_city + "/" + slug_title + "-" + str(number) filename = byte_array_to_file( pic_arr, slug_city, slug_title + "-" + str(number) ) new_complaint = { "_id": ObjectId(), "title": title, "user": ObjectId(user["_id"]), "pics": [filename], "slug_city": slug_city, "slug_url": slug_url, "public_url": public_url, "category": category, "comments": [], "upvoters": [ObjectId(user["_id"])], "downvoters": [], "upvote_count": 1, "downvote_count": 0, "location": location, "address": address, "city": city, "date": datetime.now() } user = session.get("user") db.complaint.insert(new_complaint) new_complaint = serialize_complaint(new_complaint) new_complaint["user"] = serialize_user(user) new_complaint["comments_count"] = 0 db.metadata.update( {"type": "statistics"}, {"$inc": {"complaint_count": 1}} ) db.users.update( {"_id": ObjectId(user["_id"])}, { "$addToSet": { "complaints": ObjectId(new_complaint["_id"]), "upvotes": ObjectId(new_complaint["_id"]) } } ) return new_complaint, 201
def login_user_with_facebook(session, email, access_token, android_id="", apple_id="", current_city=""): """ Arguments: - `session`: - `email`: - `access_token`: """ url = "https://graph.facebook.com/me?access_token=" + access_token r = requests.get(url) if r.status_code != 200: return {"error": "access token is not valid"}, 401 else: user = db.users.find_one({"email": email}) if not user: json_data = r.json() if "username" in json_data: username = json_data["username"] else: username = json_data["id"] if "email" in json_data: email = json_data["email"] else: email = username + "@facebook.com" avatar_url = "https://graph.facebook.com/" avatar_url += username avatar_url += "/picture?type=square&width=75&height=75" meta = db.metadata.find_one() user_count = int(meta["user_count"]) user_count = str(user_count + 1) user_slug = make_slug(json_data["name"]) + "-" + user_count try: db.users.insert( { "email": email, "first_name": json_data["first_name"], "last_name": json_data["last_name"], "name": json_data["name"], "devices": [], "current_city": "", "complaints": [], "upvotes": [], "downvotes": [], "fbusername": username, "avatar": avatar_url, "user_type": "member", "user_slug": user_slug, "fb": 1, "register_date": datetime.now(), } ) db.metadata.update({"type": "statistics"}, {"$inc": {"user_count": 1}}) user = db.users.find_one({"email": email}) user = cleanup_user_for_session(user) session["user"] = user session["logged_in"] = True user = serialize_user(user) return user, 200 except: return {"error": "cont login with facebook"}, 400 else: json_data = r.json() if user["fb"] == 0: if "username" in json_data: username = json_data["username"] else: username = json_data["id"] user["fbusername"] = username user["fb"] = 1 db.users.save(user) if android_id: db.users.update({"_id": user["_id"]}, {"$addToSet": {"devices": {"android": android_id}}}) elif apple_id: db.users.update({"_id": user["_id"]}, {"$addToSet": {"devices": {"apple": apple_id}}}) if current_city: db.users.update({"_id": user["_id"]}, {"$set": {"current_city": current_city}}) user = db.users.find_one({"email": email}) user = cleanup_user_for_session(user) session["user"] = user session["logged_in"] = True user = serialize_user(user) return user, 200