Keep track of already shared posts
(operation, postid, limit, logger)
| 152 | |
| 153 | |
| 154 | def comment_restriction(operation, postid, limit, logger): |
| 155 | """Keep track of already shared posts""" |
| 156 | conn = None |
| 157 | |
| 158 | try: |
| 159 | # get a DB and start a connection |
| 160 | db, id = get_database() |
| 161 | conn = sqlite3.connect(db) |
| 162 | |
| 163 | with conn: |
| 164 | conn.row_factory = sqlite3.Row |
| 165 | cur = conn.cursor() |
| 166 | |
| 167 | cur.execute( |
| 168 | "SELECT * FROM commentRestriction WHERE profile_id=:id_var " |
| 169 | "AND postid=:name_var", |
| 170 | {"id_var": id, "name_var": postid}, |
| 171 | ) |
| 172 | data = cur.fetchone() |
| 173 | share_data = dict(data) if data else None |
| 174 | |
| 175 | if operation == "write": |
| 176 | if share_data is None: |
| 177 | # write a new record |
| 178 | cur.execute( |
| 179 | "INSERT INTO commentRestriction (profile_id, " |
| 180 | "postid, times) VALUES (?, ?, ?)", |
| 181 | (id, postid, 1), |
| 182 | ) |
| 183 | else: |
| 184 | # update the existing record |
| 185 | share_data["times"] += 1 |
| 186 | sql = ( |
| 187 | "UPDATE commentRestriction set times = ? WHERE " |
| 188 | "profile_id=? AND postid = ?" |
| 189 | ) |
| 190 | cur.execute(sql, (share_data["times"], id, postid)) |
| 191 | |
| 192 | # commit the latest changes |
| 193 | conn.commit() |
| 194 | |
| 195 | elif operation == "read": |
| 196 | if share_data is None: |
| 197 | return False |
| 198 | |
| 199 | elif share_data["times"] < limit: |
| 200 | return False |
| 201 | |
| 202 | else: |
| 203 | exceed_msg = "" if share_data["times"] == limit else "more than " |
| 204 | logger.info( |
| 205 | "--> {} has been commented on {}{} times".format( |
| 206 | postid, exceed_msg, str(limit) |
| 207 | ) |
| 208 | ) |
| 209 | return True |
| 210 | |
| 211 | except Exception as exc: |
no test coverage detected