python – Is my Flask social site endpoint cleanly written?

What I have here is a relatively simple endpoint for a small site I’m making. Idea is to take:

and create an entry in the database. It works properly.

I threw this together as something that should work and tried to make it clean (like with the error list that gets returned) but clearly I’m doing this all by hand. Are there best practices for writing these API-style endpoints that I’m not following?

I looked under the hood at a few sites (such as SO) and noticed they are using JSON as encoding for the response. That’s why I used it too.

On the input validation front, is my system decently robust or is it too flimsy?

I also tried to make the code safe by catching any exceptions that might pop up. If there’s something I’ve overlooked please let me know.

@main.route('/createpost', methods=('POST'))
@login_required
def createpost():
resp = {
    'success': False
}

err = ()

u = current_user.id  # 2458017363
title = request.values.get('title')
_tags = request.values.get('tags')  # JSON btw
content = request.values.get('content')
# _attachments = request.files.getlist('file')
# attachments = ()

# for f in _attachments:
#     if f.filename.rsplit('.', 1)(1).lower() not in ALLOWED_EXTENSIONS:
#         filepath = os.path.join(UPLOAD_DIR, secure_filename(f.filename))
#         f.save(filepath)
#         attachments.append(filepath)
#     else:
#         err.append('File ' + f.filename + "is not permitted!")

if not title or len(title) > 100:
    err.append('Your title must exist and be less than 100 characters.')

try:
    tags = json.loads(_tags)
    if not tags or len(tags) > 3:
        err.append('Choose between 1-3 tags so people know what your post is about!')
except Exception:
    err.append('Choose between 1-3 tags so people know what your post is about!')

if not content or len(content) < 50:
    err.append('Your content must be at least 50 characters.')

if err:
    resp('error') = err
    print('err')
    return Response(json.dumps(resp), mimetype='text/json')

# PROVIDED EVERYTHING IS CORRECT

while True:
    try:
        dbentry = Post(id=snowflake(),
                       author_id=u,
                       title=bleach.clean(str(title)),
                       tags=bleach.clean(str(_tags)),
                       content=bleach.clean(str(content)).encode(),
                       )

        db.session.add(dbentry)
        db.session.commit()
    except IntegrityError:
        continue
    break

resp('success') = True
return Response(json.dumps(resp), mimetype='text/json')

imports are as follows:

# main.py
import json
import os

import bleach
from sqlalchemy.exc import IntegrityError

from .models import Post # sqlalchemy model for posts
from flask import Blueprint, render_template, request, Response
from flask_login import login_required, current_user
from werkzeug.utils import secure_filename

from . import db
from .utils import snowflake  # random number generator 
# (hence why i have a while loop around the db entry creation since there is a 
# miniscule chance it will give the same number again)

Clubhouse – a new era in social media?

Well, I think there's no need explaining what Clubhouse is..

So what I'm thinking, is that a new way of communication? People became so tired posting photos, writing texts, calling each other? I've entered few rooms and I wasn't really impressed with the idea as if I want to listen to someone, I'd better choose some podcast.

What do you think about idea of moving to audio format? Do you like this app or prefer something more conservative?

I will create and setup Facebook business page with SEO optimization for $3

I will create and setup Facebook business page with SEO optimization

A well designed page will make your business easier and more profitable. If you are looking to set up and manage your Facebook page then you are at the right place.My services

  • Set up and manage Facebook page based on your require.
  • Full Page SEO Friendly
  • Upload a cover photo
  • Customs tabs and settings
  • Add full details of your Business
  • Add social media profile and website link
  • Add custom URL
  • Chatbot installation

Why hire me

  • On time delivery
  • Fluent in English
  • 100% customers satisfaction
  • Unlimited revision

Try not to miss this extraordinary Deal! Thank you…

.

YouTube Organic All Package Video Promotions Social Media Marketing for $5

YouTube Organic All Package Video Promotions Social Media Marketing

I will send 2000 To 2500 Youtube video view 5 like 2 subscriber $5
Quality Of My Service :

(1) Very cheap and lower prices to my service.

(2) High Quality Views.

(3) Split available (2 link allowed).

(4) Instant start and complete within 24-48 hours.

(5) 24 Hour Online Support.

(6) Fully Cooperation if any issue.

(7) You will get some extra views also.

(8) Very quick delivery and fast complete.

.(tagsToTranslate)Youtube(t)Video(t)Likes(t)Views(t)Subscribe(t)Comments

What Is New Rule For Social Media In India..?

 

database design – Service architecture for social media with global users

We are building a social media application,akin to twitter or facebook, on AWS where a user can say make a post which would then be surfaced to his followers/friends who can then like or comment.

I wonder what should be the software architecture design to support cases where say author of post was in Europe but has followers in other geographies(say USA). In this case, how could I minimise latency(my users would be on an interactive mobile app, so i don’t want them to notice so sonmething in 100s of ms for p99) for read/write operations on the post across geographies.
Some things that I have in mind:

  • Always serve read/write from local service and db replica. This would mean some kind of replication across all regions to keep them in eventual sync. This can lead to conflicts which I believe is a beast of problem to handle.
  • Keep multiple service as in above case in each geography, but keep the db geographically sharded so that posts created by used in say Europe is stored in Europe. This cannot solve all cases but would solve majority.
  • Keep just 1 service geography say Europe and keep an API gateway in each region where we have user and this gateway just routes it to the service using AWS dedicated network link which has low latency than the WAN.
    Does anybody share some insights on this?

Disadvantages Of Social Media

Usually, social media presents everything nicely wrapped in a sparkling sheet which attracts users towards its false depiction of perfection. They remain in their imaginary picture-perfect world and don’t make any efforts to change their condition. Moreover, social media is also a source of promoting violence, racism, extremism, and sectarianism among users. The authenticity of most of the news circulating on social media is not confirmed, and most of the time, the viral news is fake. With…

Disadvantages Of Social Media

What Are 3 Risks Of Social Media?

 

What Rules Guidelines Should You Follow When Using Social Media?

 

Manually Top 15 Social Bookmarking for Rank your Sites for $1

Manually Top 15 Social Bookmarking for Rank your Sites

Manually Top 15 Social Bookmarking for Rank your Sites

Hello sir,I’ve been working with the SEO Industry for 5 years. Specially providing here Off Page Seo – Link building services. So, I hope it will provide you with a service and good experience. Actually our main goal is to satisfy our clients … not only make money.It is true that everyone should earn and we are the same. However, our goal is to provide the best quality service to our client.What you’ll get-

  • Manually done 15 Social Bookmarking.
  • An Easy working Report by Google sheet.

DIFFERENCE FROM OTHER-

  1. All links to be done 100% manually .
  2. Registered with Regular mail.
  3. Lifetime replacement- Support.
  4. Clickable and easy Delivery Report.
  5. Fast Delivery and No extra Charge.
  6. Re-Checked for our and 100 % Customer Satisfaction .
  7. No Use of Auto Software’s or Tools .
  8. Full Report and All Login Details by Google Sheet

YOU SHOULD SEND ME-Send your Website- Product/ Page Link OnlyIf there is any question, let me know.Best Regards Jugol Haldar

.